summaryrefslogtreecommitdiffstats
path: root/Objects/stringlib
diff options
context:
space:
mode:
authorFredrik Lundh <fredrik@pythonware.com>2006-05-30 17:11:48 (GMT)
committerFredrik Lundh <fredrik@pythonware.com>2006-05-30 17:11:48 (GMT)
commit93eff6fecd6563c04ab6a15c546f00852ef8e639 (patch)
treee61ea46401c978e927367439685fc996d351c9b5 /Objects/stringlib
parent9faa3eda6b767ddfae8ff2627d50edd6ad072490 (diff)
downloadcpython-93eff6fecd6563c04ab6a15c546f00852ef8e639.zip
cpython-93eff6fecd6563c04ab6a15c546f00852ef8e639.tar.gz
cpython-93eff6fecd6563c04ab6a15c546f00852ef8e639.tar.bz2
changed find/rfind to return -1 for matches outside the source string
Diffstat (limited to 'Objects/stringlib')
-rw-r--r--Objects/stringlib/find.h21
1 files changed, 11 insertions, 10 deletions
diff --git a/Objects/stringlib/find.h b/Objects/stringlib/find.h
index 4cea2db..4cdbb09 100644
--- a/Objects/stringlib/find.h
+++ b/Objects/stringlib/find.h
@@ -14,8 +14,11 @@ stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
{
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 @@ stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_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 + 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)