diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-11-16 08:17:58 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-11-16 08:17:58 (GMT) |
commit | f4934ea77da38516731a75fbf9458b248d26dd81 (patch) | |
tree | 9fe19276fac7661f433f86673ff7862663ed7693 /Objects | |
parent | 5ebff7b300448db36d0d0eda7d265caa06fce6d2 (diff) | |
download | cpython-f4934ea77da38516731a75fbf9458b248d26dd81.zip cpython-f4934ea77da38516731a75fbf9458b248d26dd81.tar.gz cpython-f4934ea77da38516731a75fbf9458b248d26dd81.tar.bz2 |
Issue #28701: Replace PyUnicode_CompareWithASCIIString with _PyUnicode_EqualToASCIIString.
The latter function is more readable, faster and doesn't raise exceptions.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/longobject.c | 8 | ||||
-rw-r--r-- | Objects/moduleobject.c | 2 | ||||
-rw-r--r-- | Objects/typeobject.c | 4 | ||||
-rw-r--r-- | Objects/unicodeobject.c | 35 |
4 files changed, 42 insertions, 7 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c index 0cc850e..8510e90 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -4936,9 +4936,9 @@ long_to_bytes(PyLongObject *v, PyObject *args, PyObject *kwds) return NULL; } - if (!PyUnicode_CompareWithASCIIString(byteorder_str, "little")) + if (_PyUnicode_EqualToASCIIString(byteorder_str, "little")) little_endian = 1; - else if (!PyUnicode_CompareWithASCIIString(byteorder_str, "big")) + else if (_PyUnicode_EqualToASCIIString(byteorder_str, "big")) little_endian = 0; else { PyErr_SetString(PyExc_ValueError, @@ -5019,9 +5019,9 @@ long_from_bytes(PyTypeObject *type, PyObject *args, PyObject *kwds) return NULL; } - if (!PyUnicode_CompareWithASCIIString(byteorder_str, "little")) + if (_PyUnicode_EqualToASCIIString(byteorder_str, "little")) little_endian = 1; - else if (!PyUnicode_CompareWithASCIIString(byteorder_str, "big")) + else if (_PyUnicode_EqualToASCIIString(byteorder_str, "big")) little_endian = 0; else { PyErr_SetString(PyExc_ValueError, diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index ac07642..ae72da6 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -587,7 +587,7 @@ _PyModule_ClearDict(PyObject *d) while (PyDict_Next(d, &pos, &key, &value)) { if (value != Py_None && PyUnicode_Check(key)) { if (PyUnicode_READ_CHAR(key, 0) != '_' || - PyUnicode_CompareWithASCIIString(key, "__builtins__") != 0) + !_PyUnicode_EqualToASCIIString(key, "__builtins__")) { if (Py_VerboseFlag > 1) { const char *s = _PyUnicode_AsString(key); diff --git a/Objects/typeobject.c b/Objects/typeobject.c index d65fcde..28a2db1 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2395,7 +2395,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) } add_dict++; } - if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) { + if (_PyUnicode_EqualToASCIIString(tmp, "__weakref__")) { if (!may_add_weak || add_weak) { PyErr_SetString(PyExc_TypeError, "__weakref__ slot disallowed: " @@ -2419,7 +2419,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) if ((add_dict && _PyUnicode_CompareWithId(tmp, &PyId___dict__) == 0) || (add_weak && - PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0)) + _PyUnicode_EqualToASCIIString(tmp, "__weakref__"))) continue; tmp =_Py_Mangle(name, tmp); if (!tmp) { diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index b7a3fa5..86485bd 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -10834,6 +10834,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) |