Mercurial > cpython-lru_cache-weakref
diff Objects/stringobject.c @ 24598:12e146171f3d legacy-trunk
Committing patch #591250 which provides "str1 in str2" when str1 is a
string of longer than 1 character.
| author | Barry Warsaw <barry@python.org> |
|---|---|
| date | Tue, 06 Aug 2002 16:58:21 +0000 |
| parents | b25c31e008c8 |
| children | ff05eef3a6d1 |
line wrap: on
line diff
--- a/Objects/stringobject.c Tue Aug 06 16:20:26 2002 +0000 +++ b/Objects/stringobject.c Tue Aug 06 16:58:21 2002 +0000 @@ -803,24 +803,31 @@ static int string_contains(PyObject *a, PyObject *el) { - register char *s, *end; - register char c; + const char *lhs, *rhs, *end; + int size; #ifdef Py_USING_UNICODE if (PyUnicode_Check(el)) return PyUnicode_Contains(a, el); #endif - if (!PyString_Check(el) || PyString_Size(el) != 1) { + if (!PyString_Check(el)) { PyErr_SetString(PyExc_TypeError, - "'in <string>' requires character as left operand"); + "'in <string>' requires string as left operand"); return -1; } - c = PyString_AsString(el)[0]; - s = PyString_AsString(a); - end = s + PyString_Size(a); - while (s < end) { - if (c == *s++) + size = PyString_Size(el); + rhs = PyString_AS_STRING(el); + lhs = PyString_AS_STRING(a); + + /* optimize for a single character */ + if (size == 1) + return memchr(lhs, *rhs, PyString_Size(a)) != NULL; + + end = lhs + (PyString_Size(a) - size); + while (lhs <= end) { + if (memcmp(lhs++, rhs, size) == 0) return 1; } + return 0; }
