Commit 602f7cf0 authored by Victor Stinner's avatar Victor Stinner

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.
parent ab457a21
...@@ -10573,14 +10573,30 @@ PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) ...@@ -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) ...@@ -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;
}
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment