@@ -429,6 +429,17 @@ unicodedata_UCD_east_asian_width_impl(PyObject *self, int chr)
429429 return PyUnicode_FromString (_PyUnicode_EastAsianWidthNames [index ]);
430430}
431431
432+ // For Hangul decomposition
433+ #define SBase 0xAC00
434+ #define LBase 0x1100
435+ #define VBase 0x1161
436+ #define TBase 0x11A7
437+ #define LCount 19
438+ #define VCount 21
439+ #define TCount 28
440+ #define NCount (VCount*TCount)
441+ #define SCount (LCount*NCount)
442+
432443/*[clinic input]
433444@permit_long_summary
434445unicodedata.UCD.decomposition
@@ -460,6 +471,25 @@ unicodedata_UCD_decomposition_impl(PyObject *self, int chr)
460471 return Py_GetConstant (Py_CONSTANT_EMPTY_STR ); /* unassigned */
461472 }
462473
474+ // Hangul Decomposition.
475+ // See section 3.12.2, "Hangul Syllable Decomposition"
476+ // https://www.unicode.org/versions/latest/core-spec/chapter-3/#G56669
477+ if (SBase <= code && code < (SBase + SCount )) {
478+ int SIndex = code - SBase ;
479+ int L = LBase + SIndex / NCount ;
480+ int V = VBase + (SIndex % NCount ) / TCount ;
481+ int T = TBase + SIndex % TCount ;
482+ if (T != TBase ) {
483+ PyOS_snprintf (decomp , sizeof (decomp ),
484+ "%04X %04X %04X" , L , V , T );
485+ }
486+ else {
487+ PyOS_snprintf (decomp , sizeof (decomp ),
488+ "%04X %04X" , L , V );
489+ }
490+ return PyUnicode_FromString (decomp );
491+ }
492+
463493 if (code < 0 || code >= 0x110000 )
464494 index = 0 ;
465495 else {
@@ -522,16 +552,6 @@ get_decomp_record(PyObject *self, Py_UCS4 code,
522552 (* index )++ ;
523553}
524554
525- #define SBase 0xAC00
526- #define LBase 0x1100
527- #define VBase 0x1161
528- #define TBase 0x11A7
529- #define LCount 19
530- #define VCount 21
531- #define TCount 28
532- #define NCount (VCount*TCount)
533- #define SCount (LCount*NCount)
534-
535555static PyObject *
536556nfd_nfkd (PyObject * self , PyObject * input , int k )
537557{
@@ -585,7 +605,9 @@ nfd_nfkd(PyObject *self, PyObject *input, int k)
585605 }
586606 output = new_output ;
587607 }
588- /* Hangul Decomposition. */
608+ // Hangul Decomposition.
609+ // See section 3.12.2, "Hangul Syllable Decomposition"
610+ // https://www.unicode.org/versions/latest/core-spec/chapter-3/#G56669
589611 if (SBase <= code && code < (SBase + SCount )) {
590612 int SIndex = code - SBase ;
591613 int L = LBase + SIndex / NCount ;
0 commit comments