mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue #19424: Optimize PyUnicode_CompareWithASCIIString()
Use fast memcmp() instead of a loop using the slow PyUnicode_READ() macro. strlen() is still necessary to check Unicode string containing null bytes.
This commit is contained in:
parent
ab457a2193
commit
602f7cf0b9
1 changed files with 30 additions and 13 deletions
|
@ -10573,14 +10573,30 @@ PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str)
|
||||||
{
|
{
|
||||||
Py_ssize_t i;
|
Py_ssize_t i;
|
||||||
int kind;
|
int kind;
|
||||||
void *data;
|
|
||||||
Py_UCS4 chr;
|
Py_UCS4 chr;
|
||||||
|
|
||||||
assert(_PyUnicode_CHECK(uni));
|
assert(_PyUnicode_CHECK(uni));
|
||||||
if (PyUnicode_READY(uni) == -1)
|
if (PyUnicode_READY(uni) == -1)
|
||||||
return -1;
|
return -1;
|
||||||
kind = PyUnicode_KIND(uni);
|
kind = PyUnicode_KIND(uni);
|
||||||
data = PyUnicode_DATA(uni);
|
if (kind == PyUnicode_1BYTE_KIND) {
|
||||||
|
char *data = PyUnicode_1BYTE_DATA(uni);
|
||||||
|
Py_ssize_t len1 = PyUnicode_GET_LENGTH(uni);
|
||||||
|
size_t len, len2 = strlen(str);
|
||||||
|
int cmp;
|
||||||
|
|
||||||
|
len = Py_MIN(len1, len2);
|
||||||
|
cmp = memcmp(data, str, len);
|
||||||
|
if (cmp != 0)
|
||||||
|
return cmp;
|
||||||
|
if (len1 > len2)
|
||||||
|
return 1; /* uni is longer */
|
||||||
|
if (len2 > len1)
|
||||||
|
return -1; /* str is longer */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
void *data = PyUnicode_DATA(uni);
|
||||||
/* Compare Unicode string and source character set string */
|
/* Compare Unicode string and source character set string */
|
||||||
for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++)
|
for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++)
|
||||||
if (chr != str[i])
|
if (chr != str[i])
|
||||||
|
@ -10592,6 +10608,7 @@ PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str)
|
||||||
if (str[i])
|
if (str[i])
|
||||||
return -1; /* str is longer */
|
return -1; /* str is longer */
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue