summaryrefslogtreecommitdiffstats
path: root/Objects/stringlib/find.h
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-01-02 21:12:58 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-01-02 21:12:58 (GMT)
commit5b7139aab41becad7ad736bd9ff2332960bf67f9 (patch)
tree9d3d3e7da0c0073af1fd2784fa7892d3222f9e88 /Objects/stringlib/find.h
parentd3e323215c6d9f303bf42875f98e365e2ff1734f (diff)
downloadcpython-5b7139aab41becad7ad736bd9ff2332960bf67f9.zip
cpython-5b7139aab41becad7ad736bd9ff2332960bf67f9.tar.gz
cpython-5b7139aab41becad7ad736bd9ff2332960bf67f9.tar.bz2
Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
`rindex`, `rsplit` and `rpartition` methods. Patch by Florent Xicluna.
Diffstat (limited to 'Objects/stringlib/find.h')
-rw-r--r--Objects/stringlib/find.h32
1 files changed, 14 insertions, 18 deletions
diff --git a/Objects/stringlib/find.h b/Objects/stringlib/find.h
index fbe99c7..b5bace7 100644
--- a/Objects/stringlib/find.h
+++ b/Objects/stringlib/find.h
@@ -32,20 +32,19 @@ stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
Py_ssize_t offset)
{
- /* XXX - create reversefastsearch helper! */
- 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;
- }
+ Py_ssize_t pos;
+
+ if (str_len < 0)
+ return -1;
+ if (sub_len == 0)
+ return str_len + offset;
+
+ pos = fastsearch(str, str_len, sub, sub_len, FAST_RSEARCH);
+
+ if (pos >= 0)
+ pos += offset;
+
+ return pos;
}
Py_LOCAL_INLINE(Py_ssize_t)
@@ -64,10 +63,7 @@ stringlib_find_slice(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
if (end < 0)
end = 0;
- return stringlib_find(
- str + start, end - start,
- sub, sub_len, start
- );
+ return stringlib_find(str + start, end - start, sub, sub_len, start);
}
Py_LOCAL_INLINE(Py_ssize_t)