Skip to content

Commit 8cc70dc

Browse files
author
Victor Stinner
committed
Fix fastsearch for UCS2 and UCS4
* If needle is 0, try (p[0] >> 16) & 0xff for UCS4 * Disable fastsearch_memchr_1char() if needle is zero for UCS2 and UCS4
1 parent c5af773 commit 8cc70dc

File tree

8 files changed

+15
-2
lines changed

8 files changed

+15
-2
lines changed

Objects/stringlib/asciilib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define FASTSEARCH asciilib_fastsearch
77
#define STRINGLIB(F) asciilib_##F
88
#define STRINGLIB_OBJECT PyUnicodeObject
9+
#define STRINGLIB_SIZEOF_CHAR 1
910
#define STRINGLIB_CHAR Py_UCS1
1011
#define STRINGLIB_TYPE_NAME "unicode"
1112
#define STRINGLIB_PARSE_CODE "U"

Objects/stringlib/fastsearch.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,17 @@ FASTSEARCH(const STRINGLIB_CHAR* s, Py_ssize_t n,
115115
unsigned char needle;
116116
int use_needle = 1;
117117
needle = p[0] & 0xff;
118-
if (needle == 0 && sizeof(STRINGLIB_CHAR) > 1) {
118+
#if STRINGLIB_SIZEOF_CHAR > 1
119+
if (needle == 0) {
119120
needle = (p[0] >> 8) & 0xff;
120-
if (needle >= 32)
121+
#if STRINGLIB_SIZEOF_CHAR > 2
122+
if (needle == 0)
123+
needle = (p[0] >> 16) & 0xff;
124+
#endif
125+
if (needle >= 32 || needle == 0)
121126
use_needle = 0;
122127
}
128+
#endif
123129
if (use_needle)
124130
return STRINGLIB(fastsearch_memchr_1char)
125131
(s, n, p[0], needle, maxcount, mode);

Objects/stringlib/stringdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define FASTSEARCH fastsearch
1010
#define STRINGLIB(F) stringlib_##F
1111
#define STRINGLIB_OBJECT PyBytesObject
12+
#define STRINGLIB_SIZEOF_CHAR 1
1213
#define STRINGLIB_CHAR char
1314
#define STRINGLIB_TYPE_NAME "string"
1415
#define STRINGLIB_PARSE_CODE "S"

Objects/stringlib/ucs1lib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define FASTSEARCH ucs1lib_fastsearch
77
#define STRINGLIB(F) ucs1lib_##F
88
#define STRINGLIB_OBJECT PyUnicodeObject
9+
#define STRINGLIB_SIZEOF_CHAR 1
910
#define STRINGLIB_CHAR Py_UCS1
1011
#define STRINGLIB_TYPE_NAME "unicode"
1112
#define STRINGLIB_PARSE_CODE "U"

Objects/stringlib/ucs2lib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define FASTSEARCH ucs2lib_fastsearch
77
#define STRINGLIB(F) ucs2lib_##F
88
#define STRINGLIB_OBJECT PyUnicodeObject
9+
#define STRINGLIB_SIZEOF_CHAR 2
910
#define STRINGLIB_CHAR Py_UCS2
1011
#define STRINGLIB_TYPE_NAME "unicode"
1112
#define STRINGLIB_PARSE_CODE "U"

Objects/stringlib/ucs4lib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define FASTSEARCH ucs4lib_fastsearch
77
#define STRINGLIB(F) ucs4lib_##F
88
#define STRINGLIB_OBJECT PyUnicodeObject
9+
#define STRINGLIB_SIZEOF_CHAR 4
910
#define STRINGLIB_CHAR Py_UCS4
1011
#define STRINGLIB_TYPE_NAME "unicode"
1112
#define STRINGLIB_PARSE_CODE "U"

Objects/stringlib/undef.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#undef FASTSEARCH
22
#undef STRINGLIB
3+
#undef STRINGLIB_SIZEOF_CHAR
34
#undef STRINGLIB_CHAR
45
#undef STRINGLIB_STR
56
#undef STRINGLIB_LEN

Objects/stringlib/unicodedefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define FASTSEARCH fastsearch
1010
#define STRINGLIB(F) stringlib_##F
1111
#define STRINGLIB_OBJECT PyUnicodeObject
12+
#define STRINGLIB_SIZEOF_CHAR Py_UNICODE_SIZE
1213
#define STRINGLIB_CHAR Py_UNICODE
1314
#define STRINGLIB_TYPE_NAME "unicode"
1415
#define STRINGLIB_PARSE_CODE "U"

0 commit comments

Comments
 (0)