-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-110289: C API: Add PyUnicode_EqualToUTF8() function #110297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
d39945e
4793161
8b24911
c55f9ac
bdf2f1e
7223c14
b271327
6f26ad6
dd124b8
76b9177
ee5781d
1a4eb7b
b124377
029f1a0
be2ffe8
8000
29b26f7
78de49d
fc79d5e
19ad126
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1297,6 +1297,37 @@ def test_comparewithasciistring(self): | |||||
# CRASHES comparewithasciistring([], b'abc') | ||||||
# CRASHES comparewithasciistring(NULL, b'abc') | ||||||
|
||||||
@support.cpython_only | ||||||
@unittest.skipIf(_testcapi is None, 'need _testcapi module') | ||||||
def test_equaltoutf8(self): | ||||||
"""Test PyUnicode_EqualToUTF8()""" | ||||||
erlend-aasland marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
from _testcapi import unicode_equaltoutf8 as equaltoutf8 | ||||||
|
||||||
strings = [ | ||||||
'abc', '\xa1\xa2\xa3', '\u4f60\u597d\u4e16', | ||||||
'\U0001f600\U0001f601\U0001f602' | ||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
] | ||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
for s in strings: | ||||||
b = s.encode() | ||||||
self.assertEqual(equaltoutf8(s, b), 1) | ||||||
self.assertEqual(equaltoutf8(b.decode(), b), 1) | ||||||
self.assertEqual(equaltoutf8(s + 'x', b + b'x'), 1) | ||||||
self.assertEqual(equaltoutf8(s + 'x', b + b'y'), 0) | ||||||
self.assertEqual(equaltoutf8(s + '\0', b + b'\0'), 0) | ||||||
self.assertEqual(equaltoutf8(s, b + b'x'), 0) | ||||||
self.assertEqual(equaltoutf8(s, b[:-1]), 0) | ||||||
self.assertEqual(equaltoutf8(s, b[:-1] + b'x'), 0) | ||||||
|
||||||
# surrogateescape | ||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
self.assertEqual(equaltoutf8('\udcfe', b'\xfe'), 0) | ||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
# surrogatepass | ||||||
self.assertEqual(equaltoutf8('\udcfe', b'\xed\xb3\xbe'), 0) | ||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
# CRASHES equaltoutf8(b'abc', b'abc') | ||||||
# CRASHES equaltoutf8([], b'abc') | ||||||
# CRASHES equaltoutf8(NULL, b'abc') | ||||||
# CRASHES equaltoutf8('abc') # NULL | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it does not work so. NULL is defined as None, and If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh ok, I thought that they were just pseudo-code as comments. Sure, you can leave There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I copied this pattern from the test for |
||||||
|
||||||
@support.cpython_only | ||||||
@unittest.skipIf(_testcapi is None, 'need _testcapi module') | ||||||
def test_richcompare(self): | ||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add :c:func:`PyUnicode_EqualToUTF8` function. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10673,6 +10673,67 @@ PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) | |
} | ||
} | ||
|
||
int | ||
PyUnicode_EqualToUTF8(PyObject *unicode, const char *str) | ||
{ | ||
assert(_PyUnicode_CHECK(unicode)); | ||
assert(str); | ||
if (PyUnicode_IS_ASCII(unicode)) { | ||
size_t len = (size_t)PyUnicode_GET_LENGTH(unicode); | ||
return strlen(str) == len && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to test the length first, to make the code more readable. Like:
Same below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is the same in How if (!a) {
return 0;
}
return b; is more readable than simple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For me, it's easier to reason about a single test per line when I review code. Keep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The readability problem as I see it, is that your There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Fortunately, every condition here is already on a separate line.
It is how |
||
memcmp(PyUnicode_1BYTE_DATA(unicode), str, len) == 0; | ||
} | ||
if (PyUnicode_UTF8(unicode) != NULL) { | ||
size_t len = (size_t)PyUnicode_UTF8_LENGTH(unicode); | ||
return strlen(str) == len && | ||
memcmp(PyUnicode_UTF8(unicode), str, len) == 0; | ||
} | ||
|
||
Py_UCS4 ch; | ||
Py_ssize_t len = PyUnicode_GET_LENGTH(unicode); | ||
int kind = PyUnicode_KIND(unicode); | ||
const void *data = PyUnicode_DATA(unicode); | ||
/* Compare Unicode string and UTF-8 string */ | ||
for (Py_ssize_t i = 0; i < len; i++) { | ||
ch = PyUnicode_READ(kind, data, i); | ||
if (ch == 0) { | ||
return 0; | ||
} | ||
else if (ch < 0x80) { | ||
if (ch != (unsigned char)*str++) { | ||
return 0; | ||
} | ||
} | ||
else if (ch < 0x800) { | ||
if ((0xc0 | (ch >> 6)) != (unsigned char)*str++ || | ||
(0x80 | (ch & 0x3f)) != (unsigned char)*str++) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
And declare a str variable as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the first comparison fails, you do not need to calculate the second byte. The code looks more compact and uniform in the way it is written right now. All expressions I copied from the UTF-8 encoder which I wrote 11 years ago, so no need to recheck them. Casting to unsigned char is not a large burden, but if you prefer, I can introduce a new |
||
{ | ||
return 0; | ||
} | ||
} | ||
else if (ch < 0x10000) { | ||
if (Py_UNICODE_IS_SURROGATE(ch) || | ||
(0xe0 | (ch >> 12)) != (unsigned char)*str++ || | ||
(0x80 | ((ch >> 6) & 0x3f)) != (unsigned char)*str++ || | ||
(0x80 | (ch & 0x3f)) != (unsigned char)*str++) | ||
{ | ||
return 0; | ||
} | ||
} | ||
else { | ||
assert(ch <= MAX_UNICODE); | ||
if ((0xf0 | (ch >> 18)) != (unsigned char)*str++ || | ||
(0x80 | ((ch >> 12) & 0x3f)) != (unsigned char)*str++ || | ||
(0x80 | ((ch >> 6) & 0x3f)) != (unsigned char)*str++ || | ||
(0x80 | (ch & 0x3f)) != (unsigned char)*str++) | ||
{ | ||
return 0; | ||
} | ||
} | ||
} | ||
return *str == 0; | ||
} | ||
|
||
int | ||
_PyUnicode_EqualToASCIIString(PyObject *unicode, const char *str) | ||
{ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.