-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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 1 commit
d39945e
4793161
8b24911
c55f9ac
bdf2f1e
7223c14
b271327
6f26ad6
dd124b8
76b9177
ee5781d
1a4eb7b
b124377
029f1a0
be2ffe8
29b26f7
78de49d
fc79d5e
19ad126
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10678,6 +10678,7 @@ 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 |
||
|
@@ -10689,49 +10690,53 @@ PyUnicode_EqualToUTF8(PyObject *unicode, const char *str) | |
memcmp(PyUnicode_UTF8(unicode), str, len) == 0; | ||
} | ||
|
||
Py_UCS4 ch; | ||
const unsigned char *s = (const unsigned char *)str; | ||
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); | ||
Py_UCS4 ch = PyUnicode_READ(kind, data, i); | ||
if (ch == 0) { | ||
return 0; | ||
} | ||
else if (ch < 0x80) { | ||
if (ch != (unsigned char)*str++) { | ||
if (s[0] != ch) { | ||
return 0; | ||
} | ||
s += 1; | ||
} | ||
else if (ch < 0x800) { | ||
if ((0xc0 | (ch >> 6)) != (unsigned char)*str++ || | ||
(0x80 | (ch & 0x3f)) != (unsigned char)*str++) | ||
if (s[0] != (0xc0 | (ch >> 6)) || | ||
s[1] != (0x80 | (ch & 0x3f))) | ||
{ | ||
return 0; | ||
} | ||
s += 2; | ||
} | ||
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++) | ||
s[0] != (0xe0 | (ch >> 12)) || | ||
s[1] != (0x80 | ((ch >> 6) & 0x3f)) || | ||
s[2] != (0x80 | (ch & 0x3f))) | ||
{ | ||
return 0; | ||
} | ||
s += 3; | ||
} | ||
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++) | ||
if (s[0] != (0xf0 | (ch >> 18)) || | ||
s[1] != (0x80 | ((ch >> 12) & 0x3f)) || | ||
s[2] != (0x80 | ((ch >> 6) & 0x3f)) || | ||
s[3] != (0x80 | (ch & 0x3f))) | ||
{ | ||
return 0; | ||
} | ||
s += 4; | ||
} | ||
} | ||
return *str == 0; | ||
return *s == 0; | ||
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 suppose that if we return true at this point then we know that 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 needs a separate research and discussion. The disadvantage is that it increases the consumed memory size, also it consumes some CPU time, so the benefit will be only if the UTF-8 cache is used in future. If the idea turned out to be good, it can simply be implemented in the future. 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. Makes total sense. I guess this also sits in an awkward place where it's likely that the user is best suited to know whether or not they want the utf-8 cache populated, but it's also an implementation detail that we don't really want to expose to users. 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. PyUnicode_EqualToUTF8() doesn't raise exception and cannot fail. Trying to allocate memory should not raise memory, but it sounds like a non-trivial side effect. Worst case: 1 GB string, you call PyUnicode_EqualToUTF8() and suddenly, Python allocates 1 GB more. I would be surprised by this behavior. 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. Maybe it's worth it to add a comment explaining why we don't cache the UTF-8 encoded string. |
||
} | ||
|
||
int | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe "ASCII encoded"?