From 833bf9422ea436774396f435e04fc3b927a16b88 Mon Sep 17 00:00:00 2001 From: Fredrik Lundh Date: Tue, 23 May 2006 10:12:21 +0000 Subject: [PATCH] needforspeed: fixed unicode "in" operator to use same implementation approach as find/index --- Objects/unicodeobject.c | 56 +++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 9a76d3583e..60b8cd91cd 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -4982,54 +4982,56 @@ onError: int PyUnicode_Contains(PyObject *container, PyObject *element) { - PyUnicodeObject *u = NULL, *v = NULL; + PyUnicodeObject *u, *v; int result; Py_ssize_t size; - register const Py_UNICODE *lhs, *end, *rhs; /* Coerce the two arguments */ - v = (PyUnicodeObject *)PyUnicode_FromObject(element); - if (v == NULL) { + v = (PyUnicodeObject *) PyUnicode_FromObject(element); + if (!v) { PyErr_SetString(PyExc_TypeError, "'in ' requires string as left operand"); - goto onError; + return -1; + } + + u = (PyUnicodeObject *) PyUnicode_FromObject(container); + if (!u) { + Py_DECREF(v); + return -1; } - u = (PyUnicodeObject *)PyUnicode_FromObject(container); - if (u == NULL) - goto onError; size = PyUnicode_GET_SIZE(v); - rhs = PyUnicode_AS_UNICODE(v); - lhs = PyUnicode_AS_UNICODE(u); + if (!size) { + result = 1; + goto done; + } result = 0; + if (size == 1) { - end = lhs + PyUnicode_GET_SIZE(u); - while (lhs < end) { - if (*lhs++ == *rhs) { - result = 1; - break; - } - } - } - else { - end = lhs + (PyUnicode_GET_SIZE(u) - size); - while (lhs <= end) { - if (memcmp(lhs++, rhs, size * sizeof(Py_UNICODE)) == 0) { + Py_UNICODE chr = PyUnicode_AS_UNICODE(v)[0]; + Py_UNICODE* ptr = PyUnicode_AS_UNICODE(u); + Py_UNICODE* end = ptr + PyUnicode_GET_SIZE(u); + for (; ptr < end; ptr++) { + if (*ptr == chr) { result = 1; break; } } + } else { + int start = 0; + int end = PyUnicode_GET_SIZE(u) - size; + for (; start <= end; start++) + if (Py_UNICODE_MATCH(u, start, v)) { + result = 1; + break; + } } +done: Py_DECREF(u); Py_DECREF(v); return result; - -onError: - Py_XDECREF(u); - Py_XDECREF(v); - return -1; } /* Concat to string or Unicode object giving a new Unicode object. */ -- 2.40.0