Skip to content

Commit ac9e9e2

Browse files
[3.14] gh-80667: Fix case-sensitivity of some Unicode literal escapes (GH-107281) (GH-144753)
Lookup for CJK ideograms and Hangul syllables is now case-insensitive, as is the case for other character names. (cherry picked from commit e66f4a5) Co-authored-by: James <snoopjedi@gmail.com>
1 parent 7f5a3ac commit ac9e9e2

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

Lib/test/test_ucn.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ def test_hangul_syllables(self):
8888
self.checkletter("HANGUL SYLLABLE HWEOK", "\ud6f8")
8989
self.checkletter("HANGUL SYLLABLE HIH", "\ud7a3")
9090

91+
self.checkletter("haNGul SYllABle WAe", '\uc65c')
92+
self.checkletter("HAngUL syLLabLE waE", '\uc65c')
93+
9194
self.assertRaises(ValueError, unicodedata.name, "\ud7a4")
9295

9396
def test_cjk_unified_ideographs(self):
@@ -103,6 +106,11 @@ def test_cjk_unified_ideographs(self):
103106
self.checkletter("CJK UNIFIED IDEOGRAPH-2B81D", "\U0002B81D")
104107
self.checkletter("CJK UNIFIED IDEOGRAPH-3134A", "\U0003134A")
105108

109+
self.checkletter("cjK UniFIeD idEogRAph-3aBc", "\u3abc")
110+
self.checkletter("CJk uNIfiEd IDeOGraPH-3AbC", "\u3abc")
111+
self.checkletter("cjK UniFIeD idEogRAph-2aBcD", "\U0002abcd")
112+
self.checkletter("CJk uNIfiEd IDeOGraPH-2AbCd", "\U0002abcd")
113+
106114
def test_bmp_characters(self):
107115
for code in range(0x10000):
108116
char = chr(code)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Literals using the ``\N{name}`` escape syntax can now construct CJK
2+
ideographs and Hangul syllables using case-insensitive names.

Modules/unicodedata.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@ find_syllable(const char *str, int *len, int *pos, int count, int column)
13621362
len1 = Py_SAFE_DOWNCAST(strlen(s), size_t, int);
13631363
if (len1 <= *len)
13641364
continue;
1365-
if (strncmp(str, s, len1) == 0) {
1365+
if (PyOS_strnicmp(str, s, len1) == 0) {
13661366
*len = len1;
13671367
*pos = i;
13681368
}
@@ -1394,7 +1394,7 @@ _getcode(const char* name, int namelen, Py_UCS4* code)
13941394
* PUA */
13951395

13961396
/* Check for hangul syllables. */
1397-
if (strncmp(name, "HANGUL SYLLABLE ", 16) == 0) {
1397+
if (PyOS_strnicmp(name, "HANGUL SYLLABLE ", 16) == 0) {
13981398
int len, L = -1, V = -1, T = -1;
13991399
const char *pos = name + 16;
14001400
find_syllable(pos, &len, &L, LCount, 0);
@@ -1412,7 +1412,7 @@ _getcode(const char* name, int namelen, Py_UCS4* code)
14121412
}
14131413

14141414
/* Check for unified ideographs. */
1415-
if (strncmp(name, "CJK UNIFIED IDEOGRAPH-", 22) == 0) {
1415+
if (PyOS_strnicmp(name, "CJK UNIFIED IDEOGRAPH-", 22) == 0) {
14161416
/* Four or five hexdigits must follow. */
14171417
unsigned int v;
14181418
v = 0;
@@ -1422,10 +1422,11 @@ _getcode(const char* name, int namelen, Py_UCS4* code)
14221422
return 0;
14231423
while (namelen--) {
14241424
v *= 16;
1425-
if (*name >= '0' && *name <= '9')
1426-
v += *name - '0';
1427-
else if (*name >= 'A' && *name <= 'F')
1428-
v += *name - 'A' + 10;
1425+
Py_UCS1 c = Py_TOUPPER(*name);
1426+
if (c >= '0' && c <= '9')
1427+
v += c - '0';
1428+
else if (c >= 'A' && c <= 'F')
1429+
v += c - 'A' + 10;
14291430
else
14301431
return 0;
14311432
name++;

0 commit comments

Comments
 (0)