[Python-checkins] r46560 - in python/trunk: Lib/test/string_tests.py Objects/stringlib/find.h

fredrik.lundh python-checkins at python.org
Tue May 30 19:11:49 CEST 2006


Author: fredrik.lundh
Date: Tue May 30 19:11:48 2006
New Revision: 46560
Modified:
 python/trunk/Lib/test/string_tests.py
 python/trunk/Objects/stringlib/find.h
Log:
changed find/rfind to return -1 for matches outside the source string
Modified: python/trunk/Lib/test/string_tests.py
==============================================================================
--- python/trunk/Lib/test/string_tests.py	(original)
+++ python/trunk/Lib/test/string_tests.py	Tue May 30 19:11:48 2006
@@ -154,6 +154,10 @@
 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
 
+ self.checkequal(0, 'abc', 'find', '', 0)
+ self.checkequal(3, 'abc', 'find', '', 3)
+ self.checkequal(-1, 'abc', 'find', '', 4)
+
 self.checkraises(TypeError, 'hello', 'find')
 self.checkraises(TypeError, 'hello', 'find', 42)
 
@@ -188,6 +192,10 @@
 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
 
+ self.checkequal(3, 'abc', 'rfind', '', 0)
+ self.checkequal(3, 'abc', 'rfind', '', 3)
+ self.checkequal(-1, 'abc', 'rfind', '', 4)
+
 self.checkraises(TypeError, 'hello', 'rfind')
 self.checkraises(TypeError, 'hello', 'rfind', 42)
 
Modified: python/trunk/Objects/stringlib/find.h
==============================================================================
--- python/trunk/Objects/stringlib/find.h	(original)
+++ python/trunk/Objects/stringlib/find.h	Tue May 30 19:11:48 2006
@@ -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)


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /