Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,14 @@ def test_combining(self):
empty_string = ""
with self.assertRaisesRegex(TypeError, r"combining\(\) argument must be a unicode character, not str"):
unicodedata.combining(empty_string)

def test_decomposition(self):
# the mapping of the decomposition field, not applied recursively
self.assertEqual(unicodedata.decomposition('DŽ'), '<compat> 0044 017D')
self.assertEqual(unicodedata.decomposition('ẛ'), '017F 0307')
self.assertEqual(unicodedata.decomposition('Ǖ'), '00DC 0304')
self.assertEqual(unicodedata.decomposition('¼'), '<fraction> 0031 2044 0034')
self.assertEqual(unicodedata.decomposition('a'), '')
# Hangul syllables decompose into their jamo in one step
self.assertEqual(unicodedata.decomposition('가'), '1100 1161')
self.assertEqual(unicodedata.decomposition('각'), '1100 1161 11A8')
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,10 @@ abstract static class DecompositionNode extends PythonUnaryClinicBuiltinNode {
static TruffleString decomposition(int codepoint) {
int type = UCharacter.getIntPropertyValue(codepoint, UProperty.DECOMPOSITION_TYPE);
String prefix = getDecompositionPrefix(type);
String decomposition = Normalizer2.getNFKDInstance().getDecomposition(codepoint);
// Like CPython: the mapping of the UCD decomposition field, not applied recursively,
// and the full L V T decomposition for Hangul syllables
Normalizer2 nfkd = Normalizer2.getNFKDInstance();
String decomposition = isHangulSyllable(codepoint) ? nfkd.getDecomposition(codepoint) : nfkd.getRawDecomposition(codepoint);

StringBuilder sb = new StringBuilder();
if (prefix != null) {
Expand All @@ -447,6 +450,10 @@ static TruffleString decomposition(int codepoint) {
return FromJavaStringNode.getUncached().execute(sb.toString(), TS_ENCODING);
}

private static boolean isHangulSyllable(int codepoint) {
return codepoint >= 0xAC00 && codepoint <= 0xD7A3;
}

private static String getDecompositionPrefix(int type) {
return switch (type) {
case DecompositionType.NOBREAK -> "<noBreak>";
Expand Down