Skip to content

Commit def5f8b

Browse files
author
fredrik.lundh
committed
needforspeed: stringlib refactoring: use stringlib/find for string find
git-svn-id: http://svn.python.org/projects/python/trunk@46406 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 21558f1 commit def5f8b

3 files changed

Lines changed: 25 additions & 30 deletions

File tree

Objects/stringlib/find.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,38 @@
99

1010
Py_LOCAL(Py_ssize_t)
1111
stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
12-
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len)
12+
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
13+
Py_ssize_t offset)
1314
{
15+
Py_ssize_t pos;
16+
1417
if (sub_len == 0)
15-
return 0;
18+
return offset;
19+
20+
pos = fastsearch(str, str_len, sub, sub_len, FAST_SEARCH);
1621

17-
return fastsearch(str, str_len, sub, sub_len, FAST_SEARCH);
22+
if (pos >= 0)
23+
pos += offset;
24+
25+
return pos;
1826
}
1927

2028
Py_LOCAL(Py_ssize_t)
2129
stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
22-
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len)
30+
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
31+
Py_ssize_t offset)
2332
{
2433
Py_ssize_t pos;
2534

2635
/* XXX - create reversefastsearch helper! */
2736
if (sub_len == 0)
28-
pos = str_len;
37+
pos = str_len + offset;
2938
else {
3039
Py_ssize_t j;
3140
pos = -1;
3241
for (j = str_len - sub_len; j >= 0; --j)
3342
if (STRINGLIB_CMP(str+j, sub, sub_len) == 0) {
34-
pos = j;
43+
pos = j + offset;
3544
break;
3645
}
3746
}

Objects/stringobject.c

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,8 @@ PyString_AsStringAndSize(register PyObject *obj,
775775
#define STRINGLIB_EMPTY nullstring
776776

777777
#include "stringlib/fastsearch.h"
778+
779+
#include "stringlib/find.h"
778780
#include "stringlib/partition.h"
779781

780782
/* -------------------------------------------------------------------- */
@@ -1876,25 +1878,10 @@ string_find_internal(PyStringObject *self, PyObject *args, int dir)
18761878

18771879
string_adjust_indices(&i, &last, len);
18781880

1879-
if (n == 0)
1880-
return (dir > 0) ? i : last;
1881-
if (dir > 0) {
1882-
Py_ssize_t pos = fastsearch(s + i, last - i, sub, n,
1883-
FAST_SEARCH);
1884-
if (pos < 0)
1885-
return pos;
1886-
return pos + i;
1887-
} else {
1888-
Py_ssize_t j;
1889-
1890-
if (n == 0 && i <= last)
1891-
return last;
1892-
for (j = last-n; j >= i; --j)
1893-
if (s[j] == sub[0] && memcmp(&s[j], sub, n) == 0)
1894-
return j;
1895-
}
1896-
1897-
return -1;
1881+
if (dir > 0)
1882+
return stringlib_find(s+i, last-i, sub, n, i);
1883+
else
1884+
return stringlib_rfind(s+i, last-i, sub, n, i);
18981885
}
18991886

19001887

Objects/unicodeobject.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3967,16 +3967,15 @@ Py_ssize_t PyUnicode_Find(PyObject *str,
39673967

39683968
if (direction > 0)
39693969
result = stringlib_find(
3970-
str_obj->str + start, end - start, sub_obj->str, sub_obj->length
3970+
str_obj->str + start, end - start, sub_obj->str, sub_obj->length,
3971+
start
39713972
);
39723973
else
39733974
result = stringlib_rfind(
3974-
str_obj->str + start, end - start, sub_obj->str, sub_obj->length
3975+
str_obj->str + start, end - start, sub_obj->str, sub_obj->length,
3976+
start
39753977
);
39763978

3977-
if (result >= 0)
3978-
result += start;
3979-
39803979
Py_DECREF(str_obj);
39813980
Py_DECREF(sub_obj);
39823981
return result;

0 commit comments

Comments
 (0)