mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00
* Beef-up tests for str.count().
* Speed-up str.count() by using memchr() to fly between first char matches.
This commit is contained in:
parent
7cbf1bcb3e
commit
57e7447c44
2 changed files with 35 additions and 2 deletions
|
@ -2145,7 +2145,7 @@ interpreted as in slice notation.");
|
|||
static PyObject *
|
||||
string_count(PyStringObject *self, PyObject *args)
|
||||
{
|
||||
const char *s = PyString_AS_STRING(self), *sub;
|
||||
const char *s = PyString_AS_STRING(self), *sub, *t;
|
||||
int len = PyString_GET_SIZE(self), n;
|
||||
int i = 0, last = INT_MAX;
|
||||
int m, r;
|
||||
|
@ -2186,11 +2186,16 @@ string_count(PyStringObject *self, PyObject *args)
|
|||
} else {
|
||||
i++;
|
||||
}
|
||||
if (i >= m)
|
||||
break;
|
||||
t = memchr(s+i, sub[0], m-i);
|
||||
if (t == NULL)
|
||||
break;
|
||||
i = t - s;
|
||||
}
|
||||
return PyInt_FromLong((long) r);
|
||||
}
|
||||
|
||||
|
||||
PyDoc_STRVAR(swapcase__doc__,
|
||||
"S.swapcase() -> string\n\
|
||||
\n\
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue