1 /* decomp.c - Character decomposition.
3 * Copyright (C) 1999, 2000 Tom Tromey
4 * Copyright 2000 Red Hat, Inc.
6 * SPDX-License-Identifier: LGPL-2.1-or-later
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, see <http://www.gnu.org/licenses/>.
24 * @Title: Unicode Manipulation
25 * @Short_description: functions operating on Unicode characters and
27 * @See_also: g_locale_to_utf8(), g_locale_from_utf8()
29 * This section describes a number of functions for dealing with
30 * Unicode characters and strings. There are analogues of the
31 * traditional `ctype.h` character classification and case conversion
32 * functions, UTF-8 analogues of some string utility functions,
33 * functions to perform normalization, case conversion and collation
34 * on UTF-8 strings and finally functions to convert between the UTF-8,
35 * UTF-16 and UCS-4 encodings of Unicode.
37 * The implementations of the Unicode functions in GLib are based
38 * on the Unicode Character Data tables, which are available from
39 * [www.unicode.org](http://www.unicode.org/).
41 * * Unicode 4.0 was added in GLib 2.8
42 * * Unicode 4.1 was added in GLib 2.10
43 * * Unicode 5.0 was added in GLib 2.12
44 * * Unicode 5.1 was added in GLib 2.16.3
45 * * Unicode 6.0 was added in GLib 2.30
46 * * Unicode 6.1 was added in GLib 2.32
47 * * Unicode 6.2 was added in GLib 2.36
48 * * Unicode 6.3 was added in GLib 2.40
49 * * Unicode 7.0 was added in GLib 2.42
50 * * Unicode 8.0 was added in GLib 2.48
51 * * Unicode 9.0 was added in GLib 2.50.1
52 * * Unicode 10.0 was added in GLib 2.54
53 * * Unicode 11.10 was added in GLib 2.58
54 * * Unicode 12.0 was added in GLib 2.62
55 * * Unicode 12.1 was added in GLib 2.62
56 * * Unicode 13.0 was added in GLib 2.66
64 #include "gunidecomp.h"
67 #include "gunicodeprivate.h"
70 #define CC_PART1(Page, Char) \
71 ((combining_class_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
72 ? (combining_class_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
73 : (cclass_data[combining_class_table_part1[Page]][Char]))
75 #define CC_PART2(Page, Char) \
76 ((combining_class_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
77 ? (combining_class_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
78 : (cclass_data[combining_class_table_part2[Page]][Char]))
80 #define COMBINING_CLASS(Char) \
81 (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
82 ? CC_PART1 ((Char) >> 8, (Char) & 0xff) \
83 : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
84 ? CC_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
88 * g_unichar_combining_class:
89 * @uc: a Unicode character
91 * Determines the canonical combining class of a Unicode character.
93 * Returns: the combining class of the character
98 g_unichar_combining_class (gunichar uc)
100 return COMBINING_CLASS (uc);
103 /* constants for hangul syllable [de]composition */
111 #define NCount (VCount * TCount)
112 #define SCount (LCount * NCount)
115 * g_unicode_canonical_ordering:
116 * @string: a UCS-4 encoded string.
117 * @len: the maximum length of @string to use.
119 * Computes the canonical ordering of a string in-place.
120 * This rearranges decomposed characters in the string
121 * according to their combining classes. See the Unicode
122 * manual for more information.
125 g_unicode_canonical_ordering (gunichar *string,
135 last = COMBINING_CLASS (string[0]);
136 for (i = 0; i < len - 1; ++i)
138 int next = COMBINING_CLASS (string[i + 1]);
139 if (next != 0 && last > next)
142 /* Percolate item leftward through string. */
143 for (j = i + 1; j > 0; --j)
146 if (COMBINING_CLASS (string[j - 1]) <= next)
149 string[j] = string[j - 1];
153 /* We're re-entering the loop looking at the old
162 /* http://www.unicode.org/unicode/reports/tr15/#Hangul
163 * r should be null or have sufficient space. Calling with r == NULL will
164 * only calculate the result_len; however, a buffer with space for three
165 * characters will always be big enough. */
167 decompose_hangul (gunichar s,
171 gint SIndex = s - SBase;
172 gint TIndex = SIndex % TCount;
176 r[0] = LBase + SIndex / NCount;
177 r[1] = VBase + (SIndex % NCount) / TCount;
183 r[2] = TBase + TIndex;
190 /* returns a pointer to a null-terminated UTF-8 string */
192 find_decomposition (gunichar ch,
196 int end = G_N_ELEMENTS (decomp_table);
198 if (ch >= decomp_table[start].ch &&
199 ch <= decomp_table[end - 1].ch)
203 int half = (start + end) / 2;
204 if (ch == decomp_table[half].ch)
210 offset = decomp_table[half].compat_offset;
211 if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
212 offset = decomp_table[half].canon_offset;
216 offset = decomp_table[half].canon_offset;
217 if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
221 return &(decomp_expansion_string[offset]);
223 else if (half == start)
225 else if (ch > decomp_table[half].ch)
236 * g_unicode_canonical_decomposition:
237 * @ch: a Unicode character.
238 * @result_len: location to store the length of the return value.
240 * Computes the canonical decomposition of a Unicode character.
242 * Returns: a newly allocated string of Unicode characters.
243 * @result_len is set to the resulting length of the string.
245 * Deprecated: 2.30: Use the more flexible g_unichar_fully_decompose()
249 g_unicode_canonical_decomposition (gunichar ch,
256 /* Hangul syllable */
257 if (ch >= SBase && ch < SBase + SCount)
259 decompose_hangul (ch, NULL, result_len);
260 r = g_malloc (*result_len * sizeof (gunichar));
261 decompose_hangul (ch, r, result_len);
263 else if ((decomp = find_decomposition (ch, FALSE)) != NULL)
268 *result_len = g_utf8_strlen (decomp, -1);
269 r = g_malloc (*result_len * sizeof (gunichar));
271 for (p = decomp, i = 0; *p != '\0'; p = g_utf8_next_char (p), i++)
272 r[i] = g_utf8_get_char (p);
276 /* Not in our table. */
277 r = g_malloc (sizeof (gunichar));
285 /* L,V => LV and LV,T => LVT */
287 combine_hangul (gunichar a,
291 gint LIndex = a - LBase;
292 gint SIndex = a - SBase;
294 gint VIndex = b - VBase;
295 gint TIndex = b - TBase;
297 if (0 <= LIndex && LIndex < LCount
298 && 0 <= VIndex && VIndex < VCount)
300 *result = SBase + (LIndex * VCount + VIndex) * TCount;
303 else if (0 <= SIndex && SIndex < SCount && (SIndex % TCount) == 0
304 && 0 < TIndex && TIndex < TCount)
306 *result = a + TIndex;
313 #define CI(Page, Char) \
314 ((compose_table[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
315 ? (compose_table[Page] - G_UNICODE_MAX_TABLE_INDEX) \
316 : (compose_data[compose_table[Page]][Char]))
318 #define COMPOSE_INDEX(Char) \
319 (((Char >> 8) > (COMPOSE_TABLE_LAST)) ? 0 : CI((Char) >> 8, (Char) & 0xff))
326 gushort index_a, index_b;
328 if (combine_hangul (a, b, result))
331 index_a = COMPOSE_INDEX(a);
333 if (index_a >= COMPOSE_FIRST_SINGLE_START && index_a < COMPOSE_SECOND_START)
335 if (b == compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][0])
337 *result = compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][1];
344 index_b = COMPOSE_INDEX(b);
346 if (index_b >= COMPOSE_SECOND_SINGLE_START)
348 if (a == compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][0])
350 *result = compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][1];
357 if (index_a >= COMPOSE_FIRST_START && index_a < COMPOSE_FIRST_SINGLE_START &&
358 index_b >= COMPOSE_SECOND_START && index_b < COMPOSE_SECOND_SINGLE_START)
360 gunichar res = compose_array[index_a - COMPOSE_FIRST_START][index_b - COMPOSE_SECOND_START];
373 _g_utf8_normalize_wc (const gchar *str,
381 gboolean do_compat = (mode == G_NORMALIZE_NFKC ||
382 mode == G_NORMALIZE_NFKD);
383 gboolean do_compose = (mode == G_NORMALIZE_NFC ||
384 mode == G_NORMALIZE_NFKC);
388 while ((max_len < 0 || p < str + max_len) && *p)
391 gunichar wc = g_utf8_get_char (p);
393 if (wc >= SBase && wc < SBase + SCount)
396 decompose_hangul (wc, NULL, &result_len);
401 decomp = find_decomposition (wc, do_compat);
404 n_wc += g_utf8_strlen (decomp, -1);
409 p = g_utf8_next_char (p);
412 wc_buffer = g_new (gunichar, n_wc + 1);
417 while ((max_len < 0 || p < str + max_len) && *p)
419 gunichar wc = g_utf8_get_char (p);
422 gsize old_n_wc = n_wc;
424 if (wc >= SBase && wc < SBase + SCount)
427 decompose_hangul (wc, wc_buffer + n_wc, &result_len);
432 decomp = find_decomposition (wc, do_compat);
437 for (pd = decomp; *pd != '\0'; pd = g_utf8_next_char (pd))
438 wc_buffer[n_wc++] = g_utf8_get_char (pd);
441 wc_buffer[n_wc++] = wc;
446 cc = COMBINING_CLASS (wc_buffer[old_n_wc]);
450 g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
451 last_start = old_n_wc;
455 p = g_utf8_next_char (p);
460 g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
467 /* All decomposed and reordered */
469 if (do_compose && n_wc > 0)
475 for (i = 0; i < n_wc; i++)
477 int cc = COMBINING_CLASS (wc_buffer[i]);
480 (last_cc == 0 || last_cc < cc) &&
481 combine (wc_buffer[last_start], wc_buffer[i],
482 &wc_buffer[last_start]))
484 for (j = i + 1; j < n_wc; j++)
485 wc_buffer[j-1] = wc_buffer[j];
492 last_cc = COMBINING_CLASS (wc_buffer[i-1]);
511 * @str: a UTF-8 encoded string.
512 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
513 * @mode: the type of normalization to perform.
515 * Converts a string into canonical form, standardizing
516 * such issues as whether a character with an accent
517 * is represented as a base character and combining
518 * accent or as a single precomposed character. The
519 * string has to be valid UTF-8, otherwise %NULL is
520 * returned. You should generally call g_utf8_normalize()
521 * before comparing two Unicode strings.
523 * The normalization mode %G_NORMALIZE_DEFAULT only
524 * standardizes differences that do not affect the
525 * text content, such as the above-mentioned accent
526 * representation. %G_NORMALIZE_ALL also standardizes
527 * the "compatibility" characters in Unicode, such
528 * as SUPERSCRIPT THREE to the standard forms
529 * (in this case DIGIT THREE). Formatting information
530 * may be lost but for most text operations such
531 * characters should be considered the same.
533 * %G_NORMALIZE_DEFAULT_COMPOSE and %G_NORMALIZE_ALL_COMPOSE
534 * are like %G_NORMALIZE_DEFAULT and %G_NORMALIZE_ALL,
535 * but returned a result with composed forms rather
536 * than a maximally decomposed form. This is often
537 * useful if you intend to convert the string to
538 * a legacy encoding or pass it to a system with
539 * less capable Unicode handling.
541 * Returns: (nullable): a newly allocated string, that
542 * is the normalized form of @str, or %NULL if @str
543 * is not valid UTF-8.
546 g_utf8_normalize (const gchar *str,
550 gunichar *result_wc = _g_utf8_normalize_wc (str, len, mode);
553 result = g_ucs4_to_utf8 (result_wc, -1, NULL, NULL, NULL);
560 decompose_hangul_step (gunichar ch,
566 if (ch < SBase || ch >= SBase + SCount)
567 return FALSE; /* not a hangul syllable */
570 TIndex = SIndex % TCount;
574 /* split LVT -> LV,T */
580 /* split LV -> L,V */
581 *a = LBase + SIndex / NCount;
582 *b = VBase + (SIndex % NCount) / TCount;
589 * g_unichar_decompose:
590 * @ch: a Unicode character
591 * @a: (out) (not optional): return location for the first component of @ch
592 * @b: (out) (not optional): return location for the second component of @ch
594 * Performs a single decomposition step of the
595 * Unicode canonical decomposition algorithm.
597 * This function does not include compatibility
598 * decompositions. It does, however, include algorithmic
599 * Hangul Jamo decomposition, as well as 'singleton'
600 * decompositions which replace a character by a single
601 * other character. In the case of singletons *@b will
604 * If @ch is not decomposable, *@a is set to @ch and *@b
607 * Note that the way Unicode decomposition pairs are
608 * defined, it is guaranteed that @b would not decompose
609 * further, but @a may itself decompose. To get the full
610 * canonical decomposition for @ch, one would need to
611 * recursively call this function on @a. Or use
612 * g_unichar_fully_decompose().
615 * [UAX#15](http://unicode.org/reports/tr15/)
618 * Returns: %TRUE if the character could be decomposed
623 g_unichar_decompose (gunichar ch,
628 gint end = G_N_ELEMENTS (decomp_step_table);
630 if (decompose_hangul_step (ch, a, b))
633 /* TODO use bsearch() */
634 if (ch >= decomp_step_table[start].ch &&
635 ch <= decomp_step_table[end - 1].ch)
639 gint half = (start + end) / 2;
640 const decomposition_step *p = &(decomp_step_table[half]);
647 else if (half == start)
664 * @a: a Unicode character
665 * @b: a Unicode character
666 * @ch: (out) (not optional): return location for the composed character
668 * Performs a single composition step of the
669 * Unicode canonical composition algorithm.
671 * This function includes algorithmic Hangul Jamo composition,
672 * but it is not exactly the inverse of g_unichar_decompose().
673 * No composition can have either of @a or @b equal to zero.
674 * To be precise, this function composes if and only if
675 * there exists a Primary Composite P which is canonically
676 * equivalent to the sequence <@a,@b>. See the Unicode
677 * Standard for the definition of Primary Composite.
679 * If @a and @b do not compose a new character, @ch is set to zero.
682 * [UAX#15](http://unicode.org/reports/tr15/)
685 * Returns: %TRUE if the characters could be composed
690 g_unichar_compose (gunichar a,
694 if (combine (a, b, ch))
702 * g_unichar_fully_decompose:
703 * @ch: a Unicode character.
704 * @compat: whether perform canonical or compatibility decomposition
705 * @result: (optional) (out caller-allocates): location to store decomposed result, or %NULL
706 * @result_len: length of @result
708 * Computes the canonical or compatibility decomposition of a
709 * Unicode character. For compatibility decomposition,
710 * pass %TRUE for @compat; for canonical decomposition
711 * pass %FALSE for @compat.
713 * The decomposed sequence is placed in @result. Only up to
714 * @result_len characters are written into @result. The length
715 * of the full decomposition (irrespective of @result_len) is
716 * returned by the function. For canonical decomposition,
717 * currently all decompositions are of length at most 4, but
718 * this may change in the future (very unlikely though).
719 * At any rate, Unicode does guarantee that a buffer of length
720 * 18 is always enough for both compatibility and canonical
721 * decompositions, so that is the size recommended. This is provided
722 * as %G_UNICHAR_MAX_DECOMPOSITION_LENGTH.
725 * [UAX#15](http://unicode.org/reports/tr15/)
728 * Returns: the length of the full decomposition.
733 g_unichar_fully_decompose (gunichar ch,
741 /* Hangul syllable */
742 if (ch >= SBase && ch < SBase + SCount)
746 decompose_hangul (ch, result ? buffer : NULL, &len);
748 for (i = 0; i < len && i < result_len; i++)
749 result[i] = buffer[i];
752 else if ((decomp = find_decomposition (ch, compat)) != NULL)
757 len = g_utf8_strlen (decomp, -1);
759 for (p = decomp, i = 0; i < len && i < result_len; p = g_utf8_next_char (p), i++)
760 result[i] = g_utf8_get_char (p);
765 /* Does not decompose */
766 if (result && result_len >= 1)