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: (array length=len) (element-type gunichar): 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 const char *next, *between;
394 next = g_utf8_next_char (p);
395 /* Avoid reading truncated multibyte characters
396 which run past the end of the buffer */
399 /* Does the character contain a NUL terminator? */
400 for (between = &p[1]; between < next; between++)
402 if (G_UNLIKELY (!*between))
408 if (G_UNLIKELY (next > str + max_len))
411 wc = g_utf8_get_char (p);
413 if (G_UNLIKELY (wc == (gunichar) -1))
417 else if (wc >= SBase && wc < SBase + SCount)
420 decompose_hangul (wc, NULL, &result_len);
425 decomp = find_decomposition (wc, do_compat);
428 n_wc += g_utf8_strlen (decomp, -1);
436 wc_buffer = g_new (gunichar, n_wc + 1);
441 while ((max_len < 0 || p < str + max_len) && *p)
443 gunichar wc = g_utf8_get_char (p);
446 gsize old_n_wc = n_wc;
448 if (wc >= SBase && wc < SBase + SCount)
451 decompose_hangul (wc, wc_buffer + n_wc, &result_len);
456 decomp = find_decomposition (wc, do_compat);
461 for (pd = decomp; *pd != '\0'; pd = g_utf8_next_char (pd))
462 wc_buffer[n_wc++] = g_utf8_get_char (pd);
465 wc_buffer[n_wc++] = wc;
470 cc = COMBINING_CLASS (wc_buffer[old_n_wc]);
474 g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
475 last_start = old_n_wc;
479 p = g_utf8_next_char (p);
484 g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
491 /* All decomposed and reordered */
493 if (do_compose && n_wc > 0)
499 for (i = 0; i < n_wc; i++)
501 int cc = COMBINING_CLASS (wc_buffer[i]);
504 (last_cc == 0 || last_cc < cc) &&
505 combine (wc_buffer[last_start], wc_buffer[i],
506 &wc_buffer[last_start]))
508 for (j = i + 1; j < n_wc; j++)
509 wc_buffer[j-1] = wc_buffer[j];
516 last_cc = COMBINING_CLASS (wc_buffer[i-1]);
535 * @str: a UTF-8 encoded string.
536 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
537 * @mode: the type of normalization to perform.
539 * Converts a string into canonical form, standardizing
540 * such issues as whether a character with an accent
541 * is represented as a base character and combining
542 * accent or as a single precomposed character. The
543 * string has to be valid UTF-8, otherwise %NULL is
544 * returned. You should generally call g_utf8_normalize()
545 * before comparing two Unicode strings.
547 * The normalization mode %G_NORMALIZE_DEFAULT only
548 * standardizes differences that do not affect the
549 * text content, such as the above-mentioned accent
550 * representation. %G_NORMALIZE_ALL also standardizes
551 * the "compatibility" characters in Unicode, such
552 * as SUPERSCRIPT THREE to the standard forms
553 * (in this case DIGIT THREE). Formatting information
554 * may be lost but for most text operations such
555 * characters should be considered the same.
557 * %G_NORMALIZE_DEFAULT_COMPOSE and %G_NORMALIZE_ALL_COMPOSE
558 * are like %G_NORMALIZE_DEFAULT and %G_NORMALIZE_ALL,
559 * but returned a result with composed forms rather
560 * than a maximally decomposed form. This is often
561 * useful if you intend to convert the string to
562 * a legacy encoding or pass it to a system with
563 * less capable Unicode handling.
565 * Returns: (nullable): a newly allocated string, that
566 * is the normalized form of @str, or %NULL if @str
567 * is not valid UTF-8.
570 g_utf8_normalize (const gchar *str,
574 gunichar *result_wc = _g_utf8_normalize_wc (str, len, mode);
575 gchar *result = NULL;
577 if (G_LIKELY (result_wc != NULL))
579 result = g_ucs4_to_utf8 (result_wc, -1, NULL, NULL, NULL);
587 decompose_hangul_step (gunichar ch,
593 if (ch < SBase || ch >= SBase + SCount)
594 return FALSE; /* not a hangul syllable */
597 TIndex = SIndex % TCount;
601 /* split LVT -> LV,T */
607 /* split LV -> L,V */
608 *a = LBase + SIndex / NCount;
609 *b = VBase + (SIndex % NCount) / TCount;
616 * g_unichar_decompose:
617 * @ch: a Unicode character
618 * @a: (out) (not optional): return location for the first component of @ch
619 * @b: (out) (not optional): return location for the second component of @ch
621 * Performs a single decomposition step of the
622 * Unicode canonical decomposition algorithm.
624 * This function does not include compatibility
625 * decompositions. It does, however, include algorithmic
626 * Hangul Jamo decomposition, as well as 'singleton'
627 * decompositions which replace a character by a single
628 * other character. In the case of singletons *@b will
631 * If @ch is not decomposable, *@a is set to @ch and *@b
634 * Note that the way Unicode decomposition pairs are
635 * defined, it is guaranteed that @b would not decompose
636 * further, but @a may itself decompose. To get the full
637 * canonical decomposition for @ch, one would need to
638 * recursively call this function on @a. Or use
639 * g_unichar_fully_decompose().
642 * [UAX#15](http://unicode.org/reports/tr15/)
645 * Returns: %TRUE if the character could be decomposed
650 g_unichar_decompose (gunichar ch,
655 gint end = G_N_ELEMENTS (decomp_step_table);
657 if (decompose_hangul_step (ch, a, b))
660 /* TODO use bsearch() */
661 if (ch >= decomp_step_table[start].ch &&
662 ch <= decomp_step_table[end - 1].ch)
666 gint half = (start + end) / 2;
667 const decomposition_step *p = &(decomp_step_table[half]);
674 else if (half == start)
691 * @a: a Unicode character
692 * @b: a Unicode character
693 * @ch: (out) (not optional): return location for the composed character
695 * Performs a single composition step of the
696 * Unicode canonical composition algorithm.
698 * This function includes algorithmic Hangul Jamo composition,
699 * but it is not exactly the inverse of g_unichar_decompose().
700 * No composition can have either of @a or @b equal to zero.
701 * To be precise, this function composes if and only if
702 * there exists a Primary Composite P which is canonically
703 * equivalent to the sequence <@a,@b>. See the Unicode
704 * Standard for the definition of Primary Composite.
706 * If @a and @b do not compose a new character, @ch is set to zero.
709 * [UAX#15](http://unicode.org/reports/tr15/)
712 * Returns: %TRUE if the characters could be composed
717 g_unichar_compose (gunichar a,
721 if (combine (a, b, ch))
729 * g_unichar_fully_decompose:
730 * @ch: a Unicode character.
731 * @compat: whether perform canonical or compatibility decomposition
732 * @result: (optional) (out caller-allocates): location to store decomposed result, or %NULL
733 * @result_len: length of @result
735 * Computes the canonical or compatibility decomposition of a
736 * Unicode character. For compatibility decomposition,
737 * pass %TRUE for @compat; for canonical decomposition
738 * pass %FALSE for @compat.
740 * The decomposed sequence is placed in @result. Only up to
741 * @result_len characters are written into @result. The length
742 * of the full decomposition (irrespective of @result_len) is
743 * returned by the function. For canonical decomposition,
744 * currently all decompositions are of length at most 4, but
745 * this may change in the future (very unlikely though).
746 * At any rate, Unicode does guarantee that a buffer of length
747 * 18 is always enough for both compatibility and canonical
748 * decompositions, so that is the size recommended. This is provided
749 * as %G_UNICHAR_MAX_DECOMPOSITION_LENGTH.
752 * [UAX#15](http://unicode.org/reports/tr15/)
755 * Returns: the length of the full decomposition.
760 g_unichar_fully_decompose (gunichar ch,
768 /* Hangul syllable */
769 if (ch >= SBase && ch < SBase + SCount)
773 decompose_hangul (ch, result ? buffer : NULL, &len);
775 for (i = 0; i < len && i < result_len; i++)
776 result[i] = buffer[i];
779 else if ((decomp = find_decomposition (ch, compat)) != NULL)
784 len = g_utf8_strlen (decomp, -1);
786 for (p = decomp, i = 0; i < len && i < result_len; p = g_utf8_next_char (p), i++)
787 result[i] = g_utf8_get_char (p);
792 /* Does not decompose */
793 if (result && result_len >= 1)