summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorFredrik Lundh <fredrik@pythonware.com>2006-05-26 19:29:05 (GMT)
committerFredrik Lundh <fredrik@pythonware.com>2006-05-26 19:29:05 (GMT)
commitce4eccb0c465c4814881ec7f4eb41dba685cabd0 (patch)
tree4fc8de39f8e0b7c266b4358f185a6536cd38083f /Objects
parent58b5e84d52bc15ff1cae26ecff36e5a99d6715b6 (diff)
downloadcpython-ce4eccb0c465c4814881ec7f4eb41dba685cabd0.zip
cpython-ce4eccb0c465c4814881ec7f4eb41dba685cabd0.tar.gz
cpython-ce4eccb0c465c4814881ec7f4eb41dba685cabd0.tar.bz2
needforspeed: stringlib refactoring: use stringlib/find for unicode
find
Diffstat (limited to 'Objects')
-rw-r--r--Objects/unicodeobject.c50
1 files changed, 36 insertions, 14 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 596bd10..0f791c0 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -3951,27 +3951,49 @@ static Py_ssize_t findstring(PyUnicodeObject *self,
}
Py_ssize_t PyUnicode_Find(PyObject *str,
- PyObject *substr,
- Py_ssize_t start,
- Py_ssize_t end,
- int direction)
+ PyObject *substr,
+ Py_ssize_t start,
+ Py_ssize_t end,
+ int direction)
{
Py_ssize_t result;
+ PyUnicodeObject* str_obj;
+ PyUnicodeObject* sub_obj;
- str = PyUnicode_FromObject(str);
- if (str == NULL)
+ str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str);
+ if (!str)
return -2;
- substr = PyUnicode_FromObject(substr);
- if (substr == NULL) {
- Py_DECREF(str);
+ sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr);
+ if (!sub_obj) {
+ Py_DECREF(str_obj);
return -2;
}
- result = findstring((PyUnicodeObject *)str,
- (PyUnicodeObject *)substr,
- start, end, direction);
- Py_DECREF(str);
- Py_DECREF(substr);
+ if (start < 0)
+ start += str_obj->length;
+ if (start < 0)
+ start = 0;
+ if (end > str_obj->length)
+ end = str_obj->length;
+ if (end < 0)
+ end += str_obj->length;
+ if (end < 0)
+ end = 0;
+
+ if (direction > 0)
+ result = stringlib_find(
+ str_obj->str + start, end - start, sub_obj->str, sub_obj->length
+ );
+ else
+ result = stringlib_rfind(
+ str_obj->str + start, end - start, sub_obj->str, sub_obj->length
+ );
+
+ if (result >= 0)
+ result += start;
+
+ Py_DECREF(str_obj);
+ Py_DECREF(sub_obj);
return result;
}