diff options
author | Raymond Hettinger <python@rcn.com> | 2005-02-20 09:54:53 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2005-02-20 09:54:53 (GMT) |
commit | 57e7447c447249a33a13da853f8c1bfec546842a (patch) | |
tree | b1c4d48fef4139e44d09df84134bd8596c9f22bf /Objects | |
parent | 7cbf1bcb3e55c61617352ec1b20176603dacbafe (diff) | |
download | cpython-57e7447c447249a33a13da853f8c1bfec546842a.zip cpython-57e7447c447249a33a13da853f8c1bfec546842a.tar.gz cpython-57e7447c447249a33a13da853f8c1bfec546842a.tar.bz2 |
* Beef-up tests for str.count().
* Speed-up str.count() by using memchr() to fly between first char matches.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/stringobject.c | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 0cbf439..176e0d2b 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -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\ |