mirror of
https://github.com/python/cpython.git
synced 2025-08-26 19:55:24 +00:00
Issue #28701: Replace PyUnicode_CompareWithASCIIString with _PyUnicode_EqualToASCIIString.
The latter function is more readable, faster and doesn't raise exceptions.
This commit is contained in:
commit
1a73bf365e
22 changed files with 125 additions and 80 deletions
|
@ -11048,6 +11048,41 @@ PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str)
|
|||
}
|
||||
}
|
||||
|
||||
static int
|
||||
non_ready_unicode_equal_to_ascii_string(PyObject *unicode, const char *str)
|
||||
{
|
||||
size_t i, len;
|
||||
const wchar_t *p;
|
||||
len = (size_t)_PyUnicode_WSTR_LENGTH(unicode);
|
||||
if (strlen(str) != len)
|
||||
return 0;
|
||||
p = _PyUnicode_WSTR(unicode);
|
||||
assert(p);
|
||||
for (i = 0; i < len; i++) {
|
||||
unsigned char c = (unsigned char)str[i];
|
||||
if (c > 128 || p[i] != (wchar_t)c)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
_PyUnicode_EqualToASCIIString(PyObject *unicode, const char *str)
|
||||
{
|
||||
size_t len;
|
||||
assert(_PyUnicode_CHECK(unicode));
|
||||
if (PyUnicode_READY(unicode) == -1) {
|
||||
/* Memory error or bad data */
|
||||
PyErr_Clear();
|
||||
return non_ready_unicode_equal_to_ascii_string(unicode, str);
|
||||
}
|
||||
if (!PyUnicode_IS_ASCII(unicode))
|
||||
return 0;
|
||||
len = (size_t)PyUnicode_GET_LENGTH(unicode);
|
||||
return strlen(str) == len &&
|
||||
memcmp(PyUnicode_1BYTE_DATA(unicode), str, len) == 0;
|
||||
}
|
||||
|
||||
|
||||
#define TEST_COND(cond) \
|
||||
((cond) ? Py_True : Py_False)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue