+static gboolean
+decompose_hangul_step (gunichar ch,
+ gunichar *a,
+ gunichar *b)
+{
+ gint SIndex, TIndex;
+
+ if (ch < SBase || ch >= SBase + SCount)
+ return FALSE; /* not a hangul syllable */
+
+ SIndex = ch - SBase;
+ TIndex = SIndex % TCount;
+
+ if (TIndex)
+ {
+ /* split LVT -> LV,T */
+ *a = ch - TIndex;
+ *b = TBase + TIndex;
+ }
+ else
+ {
+ /* split LV -> L,V */
+ *a = LBase + SIndex / NCount;
+ *b = VBase + (SIndex % NCount) / TCount;
+ }
+
+ return TRUE;
+}
+
+/**
+ * g_unichar_decompose:
+ * @ch: a Unicode character
+ * @a: return location for the first component of @ch
+ * @b: return location for the second component of @ch
+ *
+ * Performs a single decomposition step of the
+ * Unicode canonical decomposition algorithm.
+ *
+ * This function does not include compatibility
+ * decompositions. It does, however, include algorithmic
+ * Hangul Jamo decomposition, as well as 'singleton'
+ * decompositions which replace a character by a single
+ * other character. In the case of singletons *@b will
+ * be set to zero.
+ *
+ * If @ch is not decomposable, *@a is set to @ch and *@b
+ * is set to zero.
+ *
+ * Note that the way Unicode decomposition pairs are
+ * defined, it is guaranteed that @b would not decompose
+ * further, but @a may itself decompose. To get the full
+ * canonical decomposition for @ch, one would need to
+ * recursively call this function on @a. Or use
+ * g_unichar_fully_decompose().
+ *
+ * See <ulink url="http://unicode.org/reports/tr15/">UAX#15</ulink>
+ * for details.
+ *
+ * Returns: %TRUE if the character could be decomposed
+ *
+ * Since: 2.30
+ */
+gboolean
+g_unichar_decompose (gunichar ch,
+ gunichar *a,
+ gunichar *b)
+{
+ gint start = 0;
+ gint end = G_N_ELEMENTS (decomp_step_table);
+
+ if (decompose_hangul_step (ch, a, b))
+ return TRUE;
+
+ /* TODO use bsearch() */
+ if (ch >= decomp_step_table[start].ch &&
+ ch <= decomp_step_table[end - 1].ch)
+ {
+ while (TRUE)
+ {
+ gint half = (start + end) / 2;
+ const decomposition_step *p = &(decomp_step_table[half]);
+ if (ch == p->ch)
+ {
+ *a = p->a;
+ *b = p->b;
+ return TRUE;
+ }
+ else if (half == start)
+ break;
+ else if (ch > p->ch)
+ start = half;
+ else
+ end = half;
+ }
+ }
+
+ *a = ch;
+ *b = 0;
+
+ return FALSE;
+}
+
+/**
+ * g_unichar_compose:
+ * @a: a Unicode character
+ * @b: a Unicode character
+ * @ch: return location for the composed character
+ *
+ * Performs a single composition step of the
+ * Unicode canonical composition algorithm.
+ *
+ * This function includes algorithmic Hangul Jamo composition,
+ * but it is not exactly the inverse of g_unichar_decompose().
+ * No composition can have either of @a or @b equal to zero.
+ * To be precise, this function composes if and only if
+ * there exists a Primary Composite P which is canonically
+ * equivalent to the sequence <@a,@b>. See the Unicode
+ * Standard for the definition of Primary Composite.
+ *
+ * If @a and @b do not compose a new character, @ch is set to zero.
+ *
+ * See <ulink url="http://unicode.org/reports/tr15/">UAX#15</ulink>
+ * for details.
+ *
+ * Returns: %TRUE if the characters could be composed
+ *
+ * Since: 2.30
+ */
+gboolean
+g_unichar_compose (gunichar a,
+ gunichar b,
+ gunichar *ch)
+{
+ if (combine (a, b, ch))
+ return TRUE;
+
+ *ch = 0;
+ return FALSE;
+}
+
+/**
+ * g_unichar_fully_decompose:
+ * @ch: a Unicode character.
+ * @compat: whether perform canonical or compatibility decomposition
+ * @result: (allow-none): location to store decomposed result, or %NULL
+ * @result_len: length of @result
+ *
+ * Computes the canonical or compatibility decomposition of a
+ * Unicode character. For compatibility decomposition,
+ * pass %TRUE for @compat; for canonical decomposition
+ * pass %FALSE for @compat.
+ *
+ * The decomposed sequence is placed in @result. Only up to
+ * @result_len characters are written into @result. The length
+ * of the full decomposition (irrespective of @result_len) is
+ * returned by the function. For canonical decomposition,
+ * currently all decompositions are of length at most 4, but
+ * this may change in the future (very unlikely though).
+ * At any rate, Unicode does guarantee that a buffer of length
+ * 18 is always enough for both compatibility and canonical
+ * decompositions, so that is the size recommended. This is provided
+ * as %G_UNICHAR_MAX_DECOMPOSITION_LENGTH.
+ *
+ * See <ulink url="http://unicode.org/reports/tr15/">UAX#15</ulink>
+ * for details.
+ *
+ * Return value: the length of the full decomposition.
+ *
+ * Since: 2.30
+ **/
+gsize
+g_unichar_fully_decompose (gunichar ch,
+ gboolean compat,
+ gunichar *result,
+ gsize result_len)
+{
+ const gchar *decomp;
+ const gchar *p;
+
+ /* Hangul syllable */
+ if (ch >= SBase && ch < SBase + SCount)
+ {
+ gsize len, i;
+ gunichar buffer[3];
+ decompose_hangul (ch, result ? buffer : NULL, &len);
+ if (result)
+ for (i = 0; i < len && i < result_len; i++)
+ result[i] = buffer[i];
+ return len;
+ }
+ else if ((decomp = find_decomposition (ch, compat)) != NULL)
+ {
+ /* Found it. */
+ gsize len, i;
+
+ len = g_utf8_strlen (decomp, -1);
+
+ for (p = decomp, i = 0; i < len && i < result_len; p = g_utf8_next_char (p), i++)
+ result[i] = g_utf8_get_char (p);
+
+ return len;
+ }
+
+ /* Does not decompose */
+ if (result && result_len >= 1)
+ *result = ch;
+ return 1;
+}