diff Objects/stringlib/find.h @ 38514:ecf7173e79ff legacy-trunk

changed find/rfind to return -1 for matches outside the source string
author Fredrik Lundh <fredrik@pythonware.com>
date Tue, 30 May 2006 17:11:48 +0000
parents 72327f8f4a79
children e0c06caefbff 25a5f6a30292
line wrap: on
line diff
--- a/Objects/stringlib/find.h	Tue May 30 15:53:34 2006 +0000
+++ b/Objects/stringlib/find.h	Tue May 30 17:11:48 2006 +0000
@@ -14,8 +14,11 @@
 {
     Py_ssize_t pos;
 
-    if (sub_len == 0)
+    if (sub_len == 0) {
+        if (str_len < 0)
+            return -1;
         return offset;
+    }
 
     pos = fastsearch(str, str_len, sub, sub_len, FAST_SEARCH);
 
@@ -30,22 +33,20 @@
                 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 + offset;
-    else {
-	Py_ssize_t j;
-        pos = -1;
+    if (sub_len == 0) {
+        if (str_len < 0)
+            return -1;
+	return 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 + offset;
                 break;
             }
+        return pos;
     }
-
-    return pos;
 }
 
 Py_LOCAL_INLINE(Py_ssize_t)