-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
gh-66802: Add unicodedata.block() function
#145042
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Add :func:`unicodedata.block` function to return the `Unicode block | ||
| <https://www.unicode.org/versions/Unicode17.0.0/core-spec/chapter-3/#G64189>`_ of a | ||
| character. |
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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1493,7 +1493,7 @@ | |||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (i < (int)Py_ARRAY_LENGTH(derived_name_prefixes)) { | ||||||||||||||
| Py_UCS4 v = parse_hex_code(name + prefixlen, namelen - prefixlen); | ||||||||||||||
|
Check warning on line 1496 in Modules/unicodedata.c
|
||||||||||||||
| if (find_prefix_id(v) != i) { | ||||||||||||||
| return 0; | ||||||||||||||
| } | ||||||||||||||
|
|
@@ -2066,6 +2066,39 @@ | |||||||||||||
| return (PyObject*)gbi; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /*[clinic input] | ||||||||||||||
| unicodedata.block | ||||||||||||||
|
|
||||||||||||||
| chr: int(accept={str}) | ||||||||||||||
| / | ||||||||||||||
|
|
||||||||||||||
| Return block assigned to the character chr. | ||||||||||||||
| [clinic start generated code]*/ | ||||||||||||||
|
|
||||||||||||||
| static PyObject * | ||||||||||||||
| unicodedata_block_impl(PyObject *module, int chr) | ||||||||||||||
|
Member
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. Have you tried other approaches for this, and compared the performances?
Member
Author
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 lookup difference is minor, and we save quite a bit of memory (~17%), so I think this approach is better. |
||||||||||||||
| /*[clinic end generated code: output=5f8b40c49eaec75a input=0834cf2642d6eaae]*/ | ||||||||||||||
| { | ||||||||||||||
| Py_UCS4 c = (Py_UCS4)chr; | ||||||||||||||
| int lo = 0, hi = BLOCK_COUNT - 1; | ||||||||||||||
| while (lo <= hi) { | ||||||||||||||
| int mid = (lo + hi) / 2; | ||||||||||||||
| if (c < _PyUnicode_Blocks[mid].start) { | ||||||||||||||
| hi = mid - 1; | ||||||||||||||
| } | ||||||||||||||
| else if (c > _PyUnicode_Blocks[mid].end) { | ||||||||||||||
| lo = mid + 1; | ||||||||||||||
| } | ||||||||||||||
| else { | ||||||||||||||
| size_t name = _PyUnicode_Blocks[mid].name; | ||||||||||||||
| return PyUnicode_FromString(_PyUnicode_BlockNames[name]); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| // Otherwise, return the default value per | ||||||||||||||
| // https://www.unicode.org/versions/latest/core-spec/chapter-3/#G64189 | ||||||||||||||
| return PyUnicode_FromString("No_Block"); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /*[clinic input] | ||||||||||||||
| unicodedata.grapheme_cluster_break | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -2128,6 +2161,7 @@ | |||||||||||||
| // an UCD instance. | ||||||||||||||
| static PyMethodDef unicodedata_functions[] = { | ||||||||||||||
| // Module only functions. | ||||||||||||||
| UNICODEDATA_BLOCK_METHODDEF | ||||||||||||||
| UNICODEDATA_GRAPHEME_CLUSTER_BREAK_METHODDEF | ||||||||||||||
| UNICODEDATA_INDIC_CONJUNCT_BREAK_METHODDEF | ||||||||||||||
| UNICODEDATA_EXTENDED_PICTOGRAPHIC_METHODDEF | ||||||||||||||
|
|
@@ -2137,7 +2171,7 @@ | |||||||||||||
|
|
||||||||||||||
| // The following definitions are shared between the module | ||||||||||||||
| // and the UCD class. | ||||||||||||||
| #define DB_methods (unicodedata_functions + 6) | ||||||||||||||
| #define DB_methods (unicodedata_functions + 7) | ||||||||||||||
|
|
||||||||||||||
| UNICODEDATA_UCD_DECIMAL_METHODDEF | ||||||||||||||
| UNICODEDATA_UCD_DIGIT_METHODDEF | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.