diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2016-11-16 15:41:31 +0200 |
|---|---|---|
| committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-11-16 15:41:31 +0200 |
| commit | 29a54473600bc69975fd84b67570f5b04c447d84 (patch) | |
| tree | 4c98ac7ff2948dc9b933dfcd72ef25f0d0227214 /Objects/unicodeobject.c | |
| parent | 1a73bf365e9664f8dd3357ebbb78ceaa79df2172 (diff) | |
| parent | fab6acd9f58584d2f4b361418e4d17f9c23db2f9 (diff) | |
| download | cpython-git-29a54473600bc69975fd84b67570f5b04c447d84.tar.gz | |
Issue #28701: Replace _PyUnicode_CompareWithId with _PyUnicode_EqualToASCIIId.
The latter function is more readable, faster and doesn't raise exceptions.
Based on patch by Xiang Zhang.
Diffstat (limited to 'Objects/unicodeobject.c')
| -rw-r--r-- | Objects/unicodeobject.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index b036435493..2c1f5c34dc 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -11083,6 +11083,44 @@ _PyUnicode_EqualToASCIIString(PyObject *unicode, const char *str) memcmp(PyUnicode_1BYTE_DATA(unicode), str, len) == 0; } +int +_PyUnicode_EqualToASCIIId(PyObject *left, _Py_Identifier *right) +{ + PyObject *right_uni; + Py_hash_t hash; + + assert(_PyUnicode_CHECK(left)); + assert(right->string); + + if (PyUnicode_READY(left) == -1) { + /* memory error or bad data */ + PyErr_Clear(); + return non_ready_unicode_equal_to_ascii_string(left, right->string); + } + + if (!PyUnicode_IS_ASCII(left)) + return 0; + + right_uni = _PyUnicode_FromId(right); /* borrowed */ + if (right_uni == NULL) { + /* memory error or bad data */ + PyErr_Clear(); + return _PyUnicode_EqualToASCIIString(left, right->string); + } + + if (left == right_uni) + return 1; + + if (PyUnicode_CHECK_INTERNED(left)) + return 0; + + assert(_PyUnicode_HASH(right_uni) != 1); + hash = _PyUnicode_HASH(left); + if (hash != -1 && hash != _PyUnicode_HASH(right_uni)) + return 0; + + return unicode_compare_eq(left, right_uni); +} #define TEST_COND(cond) \ ((cond) ? Py_True : Py_False) |
