Skip to content

Commit bb848a7

Browse files
author
fredrik.lundh
committed
needforspeed: stringlib refactoring, continued. added count and
find helpers; updated unicodeobject to use stringlib_count git-svn-id: http://svn.python.org/projects/python/trunk@46398 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 14c3a4d commit bb848a7

File tree

5 files changed

+132
-53
lines changed

5 files changed

+132
-53
lines changed

Objects/stringlib/count.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* stringlib: count implementation */
2+
3+
#ifndef STRINGLIB_COUNT_H
4+
#define STRINGLIB_COUNT_H
5+
6+
#ifndef STRINGLIB_FASTSEARCH_H
7+
#error must include "stringlib/fastsearch.h" before including this module
8+
#endif
9+
10+
Py_LOCAL(Py_ssize_t)
11+
stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
12+
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len)
13+
{
14+
Py_ssize_t count;
15+
16+
if (sub_len == 0)
17+
return str_len + 1;
18+
19+
count = fastsearch(str, str_len, sub, sub_len, FAST_COUNT);
20+
21+
if (count < 0)
22+
count = 0; /* no match */
23+
24+
return count;
25+
}
26+
27+
#endif
28+
29+
/*
30+
Local variables:
31+
c-basic-offset: 4
32+
indent-tabs-mode: nil
33+
End:
34+
*/

Objects/stringlib/find.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* stringlib: find/index implementation */
2+
3+
#ifndef STRINGLIB_FIND_H
4+
#define STRINGLIB_FIND_H
5+
6+
#ifndef STRINGLIB_FASTSEARCH_H
7+
#error must include "stringlib/fastsearch.h" before including this module
8+
#endif
9+
10+
Py_LOCAL(Py_ssize_t)
11+
stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
12+
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len)
13+
{
14+
if (sub_len == 0)
15+
return 0;
16+
17+
return fastsearch(str, str_len, sub, sub_len, FAST_SEARCH);
18+
}
19+
20+
Py_LOCAL(Py_ssize_t)
21+
stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
22+
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len)
23+
{
24+
Py_ssize_t pos;
25+
26+
/* XXX - create reversefastsearch helper! */
27+
if (sub_len == 0)
28+
pos = str_len;
29+
else {
30+
Py_ssize_t j;
31+
pos = -1;
32+
for (j = str_len - sub_len; j >= 0; --j)
33+
if (STRINGLIB_CMP(str+j, sub, sub_len) == 0) {
34+
pos = j;
35+
break;
36+
}
37+
}
38+
39+
return pos;
40+
}
41+
42+
#endif
43+
44+
/*
45+
Local variables:
46+
c-basic-offset: 4
47+
indent-tabs-mode: nil
48+
End:
49+
*/

Objects/stringlib/partition.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
#ifndef STRINGLIB_PARTITION_H
44
#define STRINGLIB_PARTITION_H
55

6+
#ifndef STRINGLIB_FASTSEARCH_H
7+
#error must include "stringlib/fastsearch.h" before including this module
8+
#endif
9+
610
Py_LOCAL(PyObject*)
7-
partition(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
8-
PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
11+
stringlib_partition(
12+
PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
13+
PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len
14+
)
915
{
1016
PyObject* out;
1117
Py_ssize_t pos;
@@ -46,8 +52,10 @@ partition(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
4652
}
4753

4854
Py_LOCAL(PyObject*)
49-
rpartition(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
50-
PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
55+
stringlib_rpartition(
56+
PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
57+
PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len
58+
)
5159
{
5260
PyObject* out;
5361
Py_ssize_t pos;

Objects/stringobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,7 @@ string_partition(PyStringObject *self, PyObject *sep_obj)
15481548
else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len))
15491549
return NULL;
15501550

1551-
return partition(
1551+
return stringlib_partition(
15521552
(PyObject*) self,
15531553
PyString_AS_STRING(self), PyString_GET_SIZE(self),
15541554
sep_obj, sep, sep_len
@@ -1579,7 +1579,7 @@ string_rpartition(PyStringObject *self, PyObject *sep_obj)
15791579
else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len))
15801580
return NULL;
15811581

1582-
return rpartition(
1582+
return stringlib_rpartition(
15831583
(PyObject*) self,
15841584
PyString_AS_STRING(self), PyString_GET_SIZE(self),
15851585
sep_obj, sep, sep_len

Objects/unicodeobject.c

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3869,63 +3869,47 @@ STRINGLIB_CMP(const Py_UNICODE* str, const Py_UNICODE* other, Py_ssize_t len)
38693869
#define STRINGLIB_EMPTY unicode_empty
38703870

38713871
#include "stringlib/fastsearch.h"
3872-
#include "stringlib/partition.h"
38733872

3873+
#include "stringlib/count.h"
3874+
#include "stringlib/find.h"
3875+
#include "stringlib/partition.h"
38743876

3875-
Py_LOCAL(Py_ssize_t) count(PyUnicodeObject *self,
3877+
Py_ssize_t PyUnicode_Count(PyObject *str,
3878+
PyObject *substr,
38763879
Py_ssize_t start,
3877-
Py_ssize_t end,
3878-
PyUnicodeObject *substring)
3880+
Py_ssize_t end)
38793881
{
3880-
Py_ssize_t count = 0;
3882+
Py_ssize_t result;
3883+
PyUnicodeObject* str_obj;
3884+
PyUnicodeObject* sub_obj;
3885+
3886+
str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str);
3887+
if (!str_obj)
3888+
return -1;
3889+
sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr);
3890+
if (!sub_obj) {
3891+
Py_DECREF(str_obj);
3892+
return -1;
3893+
}
38813894

38823895
if (start < 0)
3883-
start += self->length;
3896+
start += str_obj->length;
38843897
if (start < 0)
38853898
start = 0;
3886-
if (end > self->length)
3887-
end = self->length;
3899+
if (end > str_obj->length)
3900+
end = str_obj->length;
38883901
if (end < 0)
3889-
end += self->length;
3902+
end += str_obj->length;
38903903
if (end < 0)
38913904
end = 0;
38923905

3893-
if (substring->length == 0)
3894-
return (end - start + 1);
3895-
3896-
count = fastsearch(
3897-
PyUnicode_AS_UNICODE(self) + start, end - start,
3898-
substring->str, substring->length, FAST_COUNT
3906+
result = stringlib_count(
3907+
str_obj->str + start, end - start, sub_obj->str, sub_obj->length
38993908
);
39003909

3901-
if (count < 0)
3902-
count = 0; /* no match */
3903-
3904-
return count;
3905-
}
3906-
3907-
Py_ssize_t PyUnicode_Count(PyObject *str,
3908-
PyObject *substr,
3909-
Py_ssize_t start,
3910-
Py_ssize_t end)
3911-
{
3912-
Py_ssize_t result;
3913-
3914-
str = PyUnicode_FromObject(str);
3915-
if (str == NULL)
3916-
return -1;
3917-
substr = PyUnicode_FromObject(substr);
3918-
if (substr == NULL) {
3919-
Py_DECREF(str);
3920-
return -1;
3921-
}
3922-
3923-
result = count((PyUnicodeObject *)str,
3924-
start, end,
3925-
(PyUnicodeObject *)substr);
3910+
Py_DECREF(sub_obj);
3911+
Py_DECREF(str_obj);
39263912

3927-
Py_DECREF(str);
3928-
Py_DECREF(substr);
39293913
return result;
39303914
}
39313915

@@ -4767,7 +4751,7 @@ PyObject *replace(PyUnicodeObject *self,
47674751
Py_UNICODE *p;
47684752

47694753
/* replace strings */
4770-
n = count(self, 0, self->length, str1);
4754+
n = stringlib_count(self->str, self->length, str1->str, str1->length);
47714755
if (n > maxcount)
47724756
n = maxcount;
47734757
if (n == 0)
@@ -5162,7 +5146,7 @@ unicode_count(PyUnicodeObject *self, PyObject *args)
51625146
return NULL;
51635147

51645148
substring = (PyUnicodeObject *)PyUnicode_FromObject(
5165-
(PyObject *)substring);
5149+
(PyObject *)substring);
51665150
if (substring == NULL)
51675151
return NULL;
51685152

@@ -5177,9 +5161,13 @@ unicode_count(PyUnicodeObject *self, PyObject *args)
51775161
if (end < 0)
51785162
end = 0;
51795163

5180-
result = PyInt_FromSsize_t(count(self, start, end, substring));
5164+
result = PyInt_FromSsize_t(
5165+
stringlib_count(self->str + start, end - start,
5166+
substring->str, substring->length)
5167+
);
51815168

51825169
Py_DECREF(substring);
5170+
51835171
return result;
51845172
}
51855173

@@ -6222,7 +6210,7 @@ PyUnicode_Partition(PyObject *str_in, PyObject *sep_in)
62226210
return NULL;
62236211
}
62246212

6225-
out = partition(
6213+
out = stringlib_partition(
62266214
str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj),
62276215
sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj)
62286216
);
@@ -6250,7 +6238,7 @@ PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in)
62506238
return NULL;
62516239
}
62526240

6253-
out = rpartition(
6241+
out = stringlib_rpartition(
62546242
str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj),
62556243
sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj)
62566244
);

0 commit comments

Comments
 (0)