1 /* decomp.c - Character decomposition.
3 * Copyright (C) 1999, 2000 Tom Tromey
4 * Copyright 2000 Red Hat, Inc.
6 * The Gnome Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * The Gnome Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with the Gnome Library; see the file COPYING.LIB. If not,
18 * see <http://www.gnu.org/licenses/>.
23 * @Title: Unicode Manipulation
24 * @Short_description: functions operating on Unicode characters and
26 * @See_also: g_locale_to_utf8(), g_locale_from_utf8()
28 * This section describes a number of functions for dealing with
29 * Unicode characters and strings. There are analogues of the
30 * traditional <filename>ctype.h</filename> character classification
31 * and case conversion functions, UTF-8 analogues of some string utility
32 * functions, functions to perform normalization, case conversion and
33 * collation on UTF-8 strings and finally functions to convert between
34 * the UTF-8, UTF-16 and UCS-4 encodings of Unicode.
36 * The implementations of the Unicode functions in GLib are based
37 * on the Unicode Character Data tables, which are available from
38 * <ulink url="http://www.unicode.org/">www.unicode.org</ulink>.
39 * GLib 2.8 supports Unicode 4.0, GLib 2.10 supports Unicode 4.1,
40 * GLib 2.12 supports Unicode 5.0, GLib 2.16.3 supports Unicode 5.1,
41 * GLib 2.30 supports Unicode 6.0.
49 #include "gunidecomp.h"
52 #include "gunicodeprivate.h"
55 #define CC_PART1(Page, Char) \
56 ((combining_class_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
57 ? (combining_class_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
58 : (cclass_data[combining_class_table_part1[Page]][Char]))
60 #define CC_PART2(Page, Char) \
61 ((combining_class_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
62 ? (combining_class_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
63 : (cclass_data[combining_class_table_part2[Page]][Char]))
65 #define COMBINING_CLASS(Char) \
66 (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
67 ? CC_PART1 ((Char) >> 8, (Char) & 0xff) \
68 : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
69 ? CC_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
73 * g_unichar_combining_class:
74 * @uc: a Unicode character
76 * Determines the canonical combining class of a Unicode character.
78 * Return value: the combining class of the character
83 g_unichar_combining_class (gunichar uc)
85 return COMBINING_CLASS (uc);
88 /* constants for hangul syllable [de]composition */
96 #define NCount (VCount * TCount)
97 #define SCount (LCount * NCount)
100 * g_unicode_canonical_ordering:
101 * @string: a UCS-4 encoded string.
102 * @len: the maximum length of @string to use.
104 * Computes the canonical ordering of a string in-place.
105 * This rearranges decomposed characters in the string
106 * according to their combining classes. See the Unicode
107 * manual for more information.
110 g_unicode_canonical_ordering (gunichar *string,
120 last = COMBINING_CLASS (string[0]);
121 for (i = 0; i < len - 1; ++i)
123 int next = COMBINING_CLASS (string[i + 1]);
124 if (next != 0 && last > next)
127 /* Percolate item leftward through string. */
128 for (j = i + 1; j > 0; --j)
131 if (COMBINING_CLASS (string[j - 1]) <= next)
134 string[j] = string[j - 1];
138 /* We're re-entering the loop looking at the old
147 /* http://www.unicode.org/unicode/reports/tr15/#Hangul
148 * r should be null or have sufficient space. Calling with r == NULL will
149 * only calculate the result_len; however, a buffer with space for three
150 * characters will always be big enough. */
152 decompose_hangul (gunichar s,
156 gint SIndex = s - SBase;
157 gint TIndex = SIndex % TCount;
161 r[0] = LBase + SIndex / NCount;
162 r[1] = VBase + (SIndex % NCount) / TCount;
168 r[2] = TBase + TIndex;
175 /* returns a pointer to a null-terminated UTF-8 string */
177 find_decomposition (gunichar ch,
181 int end = G_N_ELEMENTS (decomp_table);
183 if (ch >= decomp_table[start].ch &&
184 ch <= decomp_table[end - 1].ch)
188 int half = (start + end) / 2;
189 if (ch == decomp_table[half].ch)
195 offset = decomp_table[half].compat_offset;
196 if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
197 offset = decomp_table[half].canon_offset;
201 offset = decomp_table[half].canon_offset;
202 if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
206 return &(decomp_expansion_string[offset]);
208 else if (half == start)
210 else if (ch > decomp_table[half].ch)
221 * g_unicode_canonical_decomposition:
222 * @ch: a Unicode character.
223 * @result_len: location to store the length of the return value.
225 * Computes the canonical decomposition of a Unicode character.
227 * Return value: a newly allocated string of Unicode characters.
228 * @result_len is set to the resulting length of the string.
230 * Deprecated: 2.30: Use the more flexible g_unichar_fully_decompose()
234 g_unicode_canonical_decomposition (gunichar ch,
241 /* Hangul syllable */
242 if (ch >= SBase && ch < SBase + SCount)
244 decompose_hangul (ch, NULL, result_len);
245 r = g_malloc (*result_len * sizeof (gunichar));
246 decompose_hangul (ch, r, result_len);
248 else if ((decomp = find_decomposition (ch, FALSE)) != NULL)
253 *result_len = g_utf8_strlen (decomp, -1);
254 r = g_malloc (*result_len * sizeof (gunichar));
256 for (p = decomp, i = 0; *p != '\0'; p = g_utf8_next_char (p), i++)
257 r[i] = g_utf8_get_char (p);
261 /* Not in our table. */
262 r = g_malloc (sizeof (gunichar));
270 /* L,V => LV and LV,T => LVT */
272 combine_hangul (gunichar a,
276 gint LIndex = a - LBase;
277 gint SIndex = a - SBase;
279 gint VIndex = b - VBase;
280 gint TIndex = b - TBase;
282 if (0 <= LIndex && LIndex < LCount
283 && 0 <= VIndex && VIndex < VCount)
285 *result = SBase + (LIndex * VCount + VIndex) * TCount;
288 else if (0 <= SIndex && SIndex < SCount && (SIndex % TCount) == 0
289 && 0 < TIndex && TIndex < TCount)
291 *result = a + TIndex;
298 #define CI(Page, Char) \
299 ((compose_table[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
300 ? (compose_table[Page] - G_UNICODE_MAX_TABLE_INDEX) \
301 : (compose_data[compose_table[Page]][Char]))
303 #define COMPOSE_INDEX(Char) \
304 (((Char >> 8) > (COMPOSE_TABLE_LAST)) ? 0 : CI((Char) >> 8, (Char) & 0xff))
311 gushort index_a, index_b;
313 if (combine_hangul (a, b, result))
316 index_a = COMPOSE_INDEX(a);
318 if (index_a >= COMPOSE_FIRST_SINGLE_START && index_a < COMPOSE_SECOND_START)
320 if (b == compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][0])
322 *result = compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][1];
329 index_b = COMPOSE_INDEX(b);
331 if (index_b >= COMPOSE_SECOND_SINGLE_START)
333 if (a == compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][0])
335 *result = compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][1];
342 if (index_a >= COMPOSE_FIRST_START && index_a < COMPOSE_FIRST_SINGLE_START &&
343 index_b >= COMPOSE_SECOND_START && index_b < COMPOSE_SECOND_SINGLE_START)
345 gunichar res = compose_array[index_a - COMPOSE_FIRST_START][index_b - COMPOSE_SECOND_START];
358 _g_utf8_normalize_wc (const gchar *str,
366 gboolean do_compat = (mode == G_NORMALIZE_NFKC ||
367 mode == G_NORMALIZE_NFKD);
368 gboolean do_compose = (mode == G_NORMALIZE_NFC ||
369 mode == G_NORMALIZE_NFKC);
373 while ((max_len < 0 || p < str + max_len) && *p)
376 gunichar wc = g_utf8_get_char (p);
378 if (wc >= SBase && wc < SBase + SCount)
381 decompose_hangul (wc, NULL, &result_len);
386 decomp = find_decomposition (wc, do_compat);
389 n_wc += g_utf8_strlen (decomp, -1);
394 p = g_utf8_next_char (p);
397 wc_buffer = g_new (gunichar, n_wc + 1);
402 while ((max_len < 0 || p < str + max_len) && *p)
404 gunichar wc = g_utf8_get_char (p);
407 gsize old_n_wc = n_wc;
409 if (wc >= SBase && wc < SBase + SCount)
412 decompose_hangul (wc, wc_buffer + n_wc, &result_len);
417 decomp = find_decomposition (wc, do_compat);
422 for (pd = decomp; *pd != '\0'; pd = g_utf8_next_char (pd))
423 wc_buffer[n_wc++] = g_utf8_get_char (pd);
426 wc_buffer[n_wc++] = wc;
431 cc = COMBINING_CLASS (wc_buffer[old_n_wc]);
435 g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
436 last_start = old_n_wc;
440 p = g_utf8_next_char (p);
445 g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
451 /* All decomposed and reordered */
453 if (do_compose && n_wc > 0)
459 for (i = 0; i < n_wc; i++)
461 int cc = COMBINING_CLASS (wc_buffer[i]);
464 (last_cc == 0 || last_cc < cc) &&
465 combine (wc_buffer[last_start], wc_buffer[i],
466 &wc_buffer[last_start]))
468 for (j = i + 1; j < n_wc; j++)
469 wc_buffer[j-1] = wc_buffer[j];
476 last_cc = COMBINING_CLASS (wc_buffer[i-1]);
495 * @str: a UTF-8 encoded string.
496 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
497 * @mode: the type of normalization to perform.
499 * Converts a string into canonical form, standardizing
500 * such issues as whether a character with an accent
501 * is represented as a base character and combining
502 * accent or as a single precomposed character. The
503 * string has to be valid UTF-8, otherwise %NULL is
504 * returned. You should generally call g_utf8_normalize()
505 * before comparing two Unicode strings.
507 * The normalization mode %G_NORMALIZE_DEFAULT only
508 * standardizes differences that do not affect the
509 * text content, such as the above-mentioned accent
510 * representation. %G_NORMALIZE_ALL also standardizes
511 * the "compatibility" characters in Unicode, such
512 * as SUPERSCRIPT THREE to the standard forms
513 * (in this case DIGIT THREE). Formatting information
514 * may be lost but for most text operations such
515 * characters should be considered the same.
517 * %G_NORMALIZE_DEFAULT_COMPOSE and %G_NORMALIZE_ALL_COMPOSE
518 * are like %G_NORMALIZE_DEFAULT and %G_NORMALIZE_ALL,
519 * but returned a result with composed forms rather
520 * than a maximally decomposed form. This is often
521 * useful if you intend to convert the string to
522 * a legacy encoding or pass it to a system with
523 * less capable Unicode handling.
525 * Return value: a newly allocated string, that is the
526 * normalized form of @str, or %NULL if @str is not
530 g_utf8_normalize (const gchar *str,
534 gunichar *result_wc = _g_utf8_normalize_wc (str, len, mode);
537 result = g_ucs4_to_utf8 (result_wc, -1, NULL, NULL, NULL);
544 decompose_hangul_step (gunichar ch,
550 if (ch < SBase || ch >= SBase + SCount)
551 return FALSE; /* not a hangul syllable */
554 TIndex = SIndex % TCount;
558 /* split LVT -> LV,T */
564 /* split LV -> L,V */
565 *a = LBase + SIndex / NCount;
566 *b = VBase + (SIndex % NCount) / TCount;
573 * g_unichar_decompose:
574 * @ch: a Unicode character
575 * @a: return location for the first component of @ch
576 * @b: return location for the second component of @ch
578 * Performs a single decomposition step of the
579 * Unicode canonical decomposition algorithm.
581 * This function does not include compatibility
582 * decompositions. It does, however, include algorithmic
583 * Hangul Jamo decomposition, as well as 'singleton'
584 * decompositions which replace a character by a single
585 * other character. In the case of singletons *@b will
588 * If @ch is not decomposable, *@a is set to @ch and *@b
591 * Note that the way Unicode decomposition pairs are
592 * defined, it is guaranteed that @b would not decompose
593 * further, but @a may itself decompose. To get the full
594 * canonical decomposition for @ch, one would need to
595 * recursively call this function on @a. Or use
596 * g_unichar_fully_decompose().
598 * See <ulink url="http://unicode.org/reports/tr15/">UAX#15</ulink>
601 * Returns: %TRUE if the character could be decomposed
606 g_unichar_decompose (gunichar ch,
611 gint end = G_N_ELEMENTS (decomp_step_table);
613 if (decompose_hangul_step (ch, a, b))
616 /* TODO use bsearch() */
617 if (ch >= decomp_step_table[start].ch &&
618 ch <= decomp_step_table[end - 1].ch)
622 gint half = (start + end) / 2;
623 const decomposition_step *p = &(decomp_step_table[half]);
630 else if (half == start)
647 * @a: a Unicode character
648 * @b: a Unicode character
649 * @ch: return location for the composed character
651 * Performs a single composition step of the
652 * Unicode canonical composition algorithm.
654 * This function includes algorithmic Hangul Jamo composition,
655 * but it is not exactly the inverse of g_unichar_decompose().
656 * No composition can have either of @a or @b equal to zero.
657 * To be precise, this function composes if and only if
658 * there exists a Primary Composite P which is canonically
659 * equivalent to the sequence <@a,@b>. See the Unicode
660 * Standard for the definition of Primary Composite.
662 * If @a and @b do not compose a new character, @ch is set to zero.
664 * See <ulink url="http://unicode.org/reports/tr15/">UAX#15</ulink>
667 * Returns: %TRUE if the characters could be composed
672 g_unichar_compose (gunichar a,
676 if (combine (a, b, ch))
684 * g_unichar_fully_decompose:
685 * @ch: a Unicode character.
686 * @compat: whether perform canonical or compatibility decomposition
687 * @result: (allow-none): location to store decomposed result, or %NULL
688 * @result_len: length of @result
690 * Computes the canonical or compatibility decomposition of a
691 * Unicode character. For compatibility decomposition,
692 * pass %TRUE for @compat; for canonical decomposition
693 * pass %FALSE for @compat.
695 * The decomposed sequence is placed in @result. Only up to
696 * @result_len characters are written into @result. The length
697 * of the full decomposition (irrespective of @result_len) is
698 * returned by the function. For canonical decomposition,
699 * currently all decompositions are of length at most 4, but
700 * this may change in the future (very unlikely though).
701 * At any rate, Unicode does guarantee that a buffer of length
702 * 18 is always enough for both compatibility and canonical
703 * decompositions, so that is the size recommended. This is provided
704 * as %G_UNICHAR_MAX_DECOMPOSITION_LENGTH.
706 * See <ulink url="http://unicode.org/reports/tr15/">UAX#15</ulink>
709 * Return value: the length of the full decomposition.
714 g_unichar_fully_decompose (gunichar ch,
722 /* Hangul syllable */
723 if (ch >= SBase && ch < SBase + SCount)
727 decompose_hangul (ch, result ? buffer : NULL, &len);
729 for (i = 0; i < len && i < result_len; i++)
730 result[i] = buffer[i];
733 else if ((decomp = find_decomposition (ch, compat)) != NULL)
738 len = g_utf8_strlen (decomp, -1);
740 for (p = decomp, i = 0; i < len && i < result_len; p = g_utf8_next_char (p), i++)
741 result[i] = g_utf8_get_char (p);
746 /* Does not decompose */
747 if (result && result_len >= 1)