summaryrefslogtreecommitdiffstats
path: root/Objects/unicodeobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-11-16 08:19:20 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-11-16 08:19:20 (GMT)
commit3b73ea127892d0e1f9d8f12f88e4f9c0ba0b89b1 (patch)
treea1a58a83f9d60f7d5d02e5d116bb76ee13c42254 /Objects/unicodeobject.c
parent21060105d99a9153db44dd88eb750965392fd966 (diff)
parentf4934ea77da38516731a75fbf9458b248d26dd81 (diff)
downloadcpython-3b73ea127892d0e1f9d8f12f88e4f9c0ba0b89b1.zip
cpython-3b73ea127892d0e1f9d8f12f88e4f9c0ba0b89b1.tar.gz
cpython-3b73ea127892d0e1f9d8f12f88e4f9c0ba0b89b1.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/unicodeobject.c')
-rw-r--r--Objects/unicodeobject.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 7c38362..2b9828a 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -11067,6 +11067,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)