+/** @brief Magic utf32__compositions() return value for Hangul Choseong */
+static const uint32_t utf32__hangul_L[1];
+
+/** @brief Return the list of compositions that @p c starts
+ * @param c Starter code point
+ * @return Composition list or NULL
+ *
+ * For Hangul leading (Choseong) jamo we return the special value
+ * utf32__hangul_L. These code points are not listed as the targets of
+ * canonical decompositions (make-unidata checks) so there is no confusion with
+ * real decompositions here.
+ */
+static const uint32_t *utf32__compositions(uint32_t c) {
+ const uint32_t *compositions = utf32__unidata(c)->composed;
+
+ if(compositions)
+ return compositions;
+ /* Special-casing for Hangul */
+ switch(utf32__grapheme_break(c)) {
+ default:
+ return 0;
+ case unicode_Grapheme_Break_L:
+ return utf32__hangul_L;
+ }
+}
+
+/** @brief Composition step
+ * @param s Start of string
+ * @param ns Length of string
+ * @return New length of string
+ *
+ * This is called from utf32__decompose_generic() to compose the result string
+ * in place.
+ */
+static size_t utf32__compose(uint32_t *s, size_t ns) {
+ const uint32_t *compositions;
+ uint32_t *start = s, *t = s, *tt, cc;
+
+ while(ns > 0) {
+ uint32_t starter = *s++;
+ int block_starters = 0;
+ --ns;
+ /* We don't attempt to compose the following things:
+ * - final characters whatever kind they are
+ * - non-starter characters
+ * - starters that don't take part in a canonical decomposition mapping
+ */
+ if(ns == 0
+ || utf32__combining_class(starter)
+ || !(compositions = utf32__compositions(starter))) {
+ *t++ = starter;
+ continue;
+ }
+ if(compositions != utf32__hangul_L) {
+ /* Where we'll put the eventual starter */
+ tt = t++;
+ do {
+ /* See if we can find composition of starter+*s */
+ const uint32_t cchar = *s, *cp = compositions;
+ while((cc = *cp++)) {
+ const uint32_t *decomp = utf32__decomposition_canon(cc);
+ /* We know decomp[0] == starter */
+ if(decomp[1] == cchar)
+ break;
+ }
+ if(cc) {
+ /* Found a composition: cc decomposes to starter,*s */
+ starter = cc;
+ compositions = utf32__compositions(starter);
+ ++s;
+ --ns;
+ } else {
+ /* No composition found. */
+ const int class = utf32__combining_class(*s);
+ if(class) {
+ /* Transfer the uncomposable combining character to the output */
+ *t++ = *s++;
+ --ns;
+ /* All the combining characters of the same class of the
+ * uncomposable character are blocked by it, but there may be
+ * others of higher class later. We eat the uncomposable and
+ * blocked characters and go back round the loop for that higher
+ * class. */
+ while(ns > 0 && utf32__combining_class(*s) == class) {
+ *t++ = *s++;
+ --ns;
+ }
+ /* Block any subsequent starters */
+ block_starters = 1;
+ } else {
+ /* The uncombinable character is itself a starter, so we don't
+ * transfer it to the output but instead go back round the main
+ * loop. */
+ break;
+ }
+ }
+ /* Keep going while there are still characters and the starter takes
+ * part in some composition */
+ } while(ns > 0 && compositions
+ && (!block_starters || utf32__combining_class(*s)));
+ /* Store any remaining combining characters */
+ while(ns > 0 && utf32__combining_class(*s)) {
+ *t++ = *s++;
+ --ns;
+ }
+ /* Store the resulting starter */
+ *tt = starter;
+ } else {
+ /* Special-casing for Hangul
+ *
+ * If there are combining characters between the L and the V then they
+ * will block the V and so no composition happens. Similarly combining
+ * characters between V and T will block the T and so we only get as far
+ * as LV.
+ */
+ if(utf32__grapheme_break(*s) == unicode_Grapheme_Break_V) {
+ const uint32_t V = *s++;
+ const uint32_t LIndex = starter - LBase;
+ const uint32_t VIndex = V - VBase;
+ uint32_t TIndex;
+ --ns;
+ if(ns > 0
+ && utf32__grapheme_break(*s) == unicode_Grapheme_Break_T) {
+ /* We have an L V T sequence */
+ const uint32_t T = *s++;
+ TIndex = T - TBase;
+ --ns;
+ } else
+ /* It's just L V */
+ TIndex = 0;
+ /* Compose to LVT or LV as appropriate */
+ starter = (LIndex * VCount + VIndex) * TCount + TIndex + SBase;
+ } /* else we only have L or LV and no V or T */
+ *t++ = starter;
+ /* There could be some combining characters that belong to the V or T.
+ * These will be treated as non-starter characters at the top of the loop
+ * and thuss transferred to the output. */
+ }
+ }
+ return t - start;
+}
+
+/** @brief Guts of the composition and decomposition functions
+ * @param WHICH @c canon or @c compat to choose decomposition
+ * @param COMPOSE @c 0 or @c 1 to compose
+ */
+#define utf32__decompose_generic(WHICH, COMPOSE) do { \