From: Fredrik Lundh Date: Thu, 25 May 2006 18:44:29 +0000 (+0000) Subject: needforspeed: use fastsearch also for find/index and contains. the X-Git-Tag: v2.5b1~525 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c3434b3834a5cf7e7ce14c775c82d0cc70bce424;p=python needforspeed: use fastsearch also for find/index and contains. the related tests are now about 10x faster. --- diff --git a/Objects/stringobject.c b/Objects/stringobject.c index e74744d369..33cbf07406 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -1149,10 +1149,14 @@ string_contains(PyObject *a, PyObject *el) { char *s = PyString_AS_STRING(a); const char *sub = PyString_AS_STRING(el); - char *last; Py_ssize_t len_sub = PyString_GET_SIZE(el); +#ifdef USE_FAST + Py_ssize_t pos; +#else + char *last; Py_ssize_t shortsub; char firstchar, lastchar; +#endif if (!PyString_CheckExact(el)) { #ifdef Py_USING_UNICODE @@ -1168,6 +1172,14 @@ string_contains(PyObject *a, PyObject *el) if (len_sub == 0) return 1; + +#ifdef USE_FAST + pos = fastsearch( + s, PyString_GET_SIZE(a), + sub, len_sub, FAST_SEARCH + ); + return (pos != -1); +#else /* last points to one char beyond the start of the rightmost substring. When s 0) ? i : last; + if (dir > 0) { + Py_ssize_t pos = fastsearch(s + i, last - i, sub, n, + FAST_SEARCH); + if (pos < 0) + return pos; + return pos + i; + } +#endif if (dir > 0) { if (n == 0 && i <= last) return (long)i;