summaryrefslogtreecommitdiff
path: root/Objects
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-11-16 10:19:20 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2016-11-16 10:19:20 +0200
commitc0797803c0d4303489709f02078c8c6adc4eed36 (patch)
treed3b5afe679e3030eecf9372a6c6bdee5bc5f39e0 /Objects
parentcf0a090abb7d7c2a3baf33a5183c0842dd316608 (diff)
parent78779b765c2d7f89fab3868b77b6a6522c55ee4c (diff)
downloadcpython-c0797803c0d4303489709f02078c8c6adc4eed36.tar.gz
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.c8
-rw-r--r--Objects/moduleobject.c2
-rw-r--r--Objects/typeobject.c4
-rw-r--r--Objects/unicodeobject.c35
4 files changed, 42 insertions, 7 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c
index a453241b3f..ad239ce84e 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -5180,9 +5180,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,
@@ -5263,9 +5263,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 dcae1c4da5..b6a2d6fe9c 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -586,7 +586,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 9836961bc0..ac3df88168 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -2412,7 +2412,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: "
@@ -2436,7 +2436,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 7c383628a6..2b9828ada0 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)