Skip to content

Commit e66f4a5

Browse files
authored
gh-80667: Fix case-sensitivity of some Unicode literal escapes (GH-107281)
Lookup for CJK ideograms and Hangul syllables is now case-insensitive, as is the case for other character names.
1 parent 9e5e1f9 commit e66f4a5

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
@@ -1405,7 +1405,7 @@ find_syllable(const char *str, int *len, int *pos, int count, int column)
14051405
len1 = Py_SAFE_DOWNCAST(strlen(s), size_t, int);
14061406
if (len1 <= *len)
14071407
continue;
1408-
if (strncmp(str, s, len1) == 0) {
1408+
if (PyOS_strnicmp(str, s, len1) == 0) {
14091409
*len = len1;
14101410
*pos = i;
14111411
}
@@ -1437,7 +1437,7 @@ _getcode(const char* name, int namelen, Py_UCS4* code)
14371437
* PUA */
14381438

14391439
/* Check for hangul syllables. */
1440-
if (strncmp(name, "HANGUL SYLLABLE ", 16) == 0) {
1440+
if (PyOS_strnicmp(name, "HANGUL SYLLABLE ", 16) == 0) {
14411441
int len, L = -1, V = -1, T = -1;
14421442
const char *pos = name + 16;
14431443
find_syllable(pos, &len, &L, LCount, 0);
@@ -1455,7 +1455,7 @@ _getcode(const char* name, int namelen, Py_UCS4* code)
14551455
}
14561456

14571457
/* Check for unified ideographs. */
1458-
if (strncmp(name, "CJK UNIFIED IDEOGRAPH-", 22) == 0) {
1458+
if (PyOS_strnicmp(name, "CJK UNIFIED IDEOGRAPH-", 22) == 0) {
14591459
/* Four or five hexdigits must follow. */
14601460
unsigned int v;
14611461
v = 0;
@@ -1465,10 +1465,11 @@ _getcode(const char* name, int namelen, Py_UCS4* code)
14651465
return 0;
14661466
while (namelen--) {
14671467
v *= 16;
1468-
if (*name >= '0' && *name <= '9')
1469-
v += *name - '0';
1470-
else if (*name >= 'A' && *name <= 'F')
1471-
v += *name - 'A' + 10;
1468+
Py_UCS1 c = Py_TOUPPER(*name);
1469+
if (c >= '0' && c <= '9')
1470+
v += c - '0';
1471+
else if (c >= 'A' && c <= 'F')
1472+
v += c - 'A' + 10;
14721473
else
14731474
return 0;
14741475
name++;

0 commit comments

Comments
 (0)