]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
needforspeed: stringlib refactoring: use stringlib/find for string find
authorFredrik Lundh <fredrik@pythonware.com>
Fri, 26 May 2006 19:48:07 +0000 (19:48 +0000)
committerFredrik Lundh <fredrik@pythonware.com>
Fri, 26 May 2006 19:48:07 +0000 (19:48 +0000)
Objects/stringlib/find.h
Objects/stringobject.c
Objects/unicodeobject.c

index c2399ad176858d913130c9109e44dd8f6264f981..d5394b4a31020cb980ef9799dcb6c76214f8f0fe 100644 (file)
@@ -9,29 +9,38 @@
 
 Py_LOCAL(Py_ssize_t)
 stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
-               const STRINGLIB_CHAR* sub, Py_ssize_t sub_len)
+               const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
+               Py_ssize_t offset)
 {
+    Py_ssize_t pos;
+
     if (sub_len == 0)
-        return 0;
+        return offset;
+
+    pos = fastsearch(str, str_len, sub, sub_len, FAST_SEARCH);
 
-    return fastsearch(str, str_len, sub, sub_len, FAST_SEARCH);
+    if (pos >= 0)
+        pos += offset;
+
+    return pos;
 }
 
 Py_LOCAL(Py_ssize_t)
 stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
-                const STRINGLIB_CHAR* sub, Py_ssize_t sub_len)
+                const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
+                Py_ssize_t offset)
 {
     Py_ssize_t pos;
 
     /* XXX - create reversefastsearch helper! */
     if (sub_len == 0)
-       pos = str_len;
+       pos = str_len + offset;
     else {
        Py_ssize_t j;
         pos = -1;
        for (j = str_len - sub_len; j >= 0; --j)
             if (STRINGLIB_CMP(str+j, sub, sub_len) == 0) {
-                pos = j;
+                pos = j + offset;
                 break;
             }
     }
index 75325ab84a1dcff0445ec147c27980e864b5d6bd..8d2a0be2256db0c1a2d154f1ecd76cd2f4b78040 100644 (file)
@@ -775,6 +775,8 @@ PyString_AsStringAndSize(register PyObject *obj,
 #define STRINGLIB_EMPTY nullstring
 
 #include "stringlib/fastsearch.h"
+
+#include "stringlib/find.h"
 #include "stringlib/partition.h"
 
 /* -------------------------------------------------------------------- */
@@ -1876,25 +1878,10 @@ string_find_internal(PyStringObject *self, PyObject *args, int dir)
 
        string_adjust_indices(&i, &last, len);
 
-       if (n == 0)
-               return (dir > 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;
-       } else {
-               Py_ssize_t j;
-
-               if (n == 0 && i <= last)
-                       return last;
-               for (j = last-n; j >= i; --j)
-                       if (s[j] == sub[0] && memcmp(&s[j], sub, n) == 0)
-                               return j;
-       }
-
-       return -1;
+       if (dir > 0)
+               return stringlib_find(s+i, last-i, sub, n, i);
+       else
+               return stringlib_rfind(s+i, last-i, sub, n, i);
 }
 
 
index 2732c2ccaf1a319bb6300948ada47707e4324860..20f943f9a404a8ddef97daf87af132d9a4419ba6 100644 (file)
@@ -3967,16 +3967,15 @@ Py_ssize_t PyUnicode_Find(PyObject *str,
 
     if (direction > 0)
         result = stringlib_find(
-            str_obj->str + start, end - start, sub_obj->str, sub_obj->length
+            str_obj->str + start, end - start, sub_obj->str, sub_obj->length,
+            start
             );
     else
         result = stringlib_rfind(
-            str_obj->str + start, end - start, sub_obj->str, sub_obj->length
+            str_obj->str + start, end - start, sub_obj->str, sub_obj->length,
+            start
             );
 
-    if (result >= 0)
-        result += start;
-
     Py_DECREF(str_obj);
     Py_DECREF(sub_obj);
     return result;