1 /* decomp.c - Character decomposition.
3 * Copyright (C) 1999, 2000 Tom Tromey
4 * Copyright 2000 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This 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 License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
22 * @Title: Unicode Manipulation
23 * @Short_description: functions operating on Unicode characters and
25 * @See_also: g_locale_to_utf8(), g_locale_from_utf8()
27 * This section describes a number of functions for dealing with
28 * Unicode characters and strings. There are analogues of the
29 * traditional `ctype.h` character classification and case conversion
30 * functions, UTF-8 analogues of some string utility functions,
31 * functions to perform normalization, case conversion and collation
32 * on UTF-8 strings and finally functions to convert between the UTF-8,
33 * UTF-16 and UCS-4 encodings of Unicode.
35 * The implementations of the Unicode functions in GLib are based
36 * on the Unicode Character Data tables, which are available from
37 * [www.unicode.org](http://www.unicode.org/).
38 * GLib 2.8 supports Unicode 4.0, GLib 2.10 supports Unicode 4.1,
39 * GLib 2.12 supports Unicode 5.0, GLib 2.16.3 supports Unicode 5.1,
40 * GLib 2.30 supports Unicode 6.0.
48 #include "gunidecomp.h"
51 #include "gunicodeprivate.h"
54 #define CC_PART1(Page, Char) \
55 ((combining_class_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
56 ? (combining_class_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
57 : (cclass_data[combining_class_table_part1[Page]][Char]))
59 #define CC_PART2(Page, Char) \
60 ((combining_class_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
61 ? (combining_class_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
62 : (cclass_data[combining_class_table_part2[Page]][Char]))
64 #define COMBINING_CLASS(Char) \
65 (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
66 ? CC_PART1 ((Char) >> 8, (Char) & 0xff) \
67 : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
68 ? CC_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
72 * g_unichar_combining_class:
73 * @uc: a Unicode character
75 * Determines the canonical combining class of a Unicode character.
77 * Returns: the combining class of the character
82 g_unichar_combining_class (gunichar uc
)
84 return COMBINING_CLASS (uc
);
87 /* constants for hangul syllable [de]composition */
95 #define NCount (VCount * TCount)
96 #define SCount (LCount * NCount)
99 * g_unicode_canonical_ordering:
100 * @string: a UCS-4 encoded string.
101 * @len: the maximum length of @string to use.
103 * Computes the canonical ordering of a string in-place.
104 * This rearranges decomposed characters in the string
105 * according to their combining classes. See the Unicode
106 * manual for more information.
109 g_unicode_canonical_ordering (gunichar
*string
,
119 last
= COMBINING_CLASS (string
[0]);
120 for (i
= 0; i
< len
- 1; ++i
)
122 int next
= COMBINING_CLASS (string
[i
+ 1]);
123 if (next
!= 0 && last
> next
)
126 /* Percolate item leftward through string. */
127 for (j
= i
+ 1; j
> 0; --j
)
130 if (COMBINING_CLASS (string
[j
- 1]) <= next
)
133 string
[j
] = string
[j
- 1];
137 /* We're re-entering the loop looking at the old
146 /* http://www.unicode.org/unicode/reports/tr15/#Hangul
147 * r should be null or have sufficient space. Calling with r == NULL will
148 * only calculate the result_len; however, a buffer with space for three
149 * characters will always be big enough. */
151 decompose_hangul (gunichar s
,
155 gint SIndex
= s
- SBase
;
156 gint TIndex
= SIndex
% TCount
;
160 r
[0] = LBase
+ SIndex
/ NCount
;
161 r
[1] = VBase
+ (SIndex
% NCount
) / TCount
;
167 r
[2] = TBase
+ TIndex
;
174 /* returns a pointer to a null-terminated UTF-8 string */
176 find_decomposition (gunichar ch
,
180 int end
= G_N_ELEMENTS (decomp_table
);
182 if (ch
>= decomp_table
[start
].ch
&&
183 ch
<= decomp_table
[end
- 1].ch
)
187 int half
= (start
+ end
) / 2;
188 if (ch
== decomp_table
[half
].ch
)
194 offset
= decomp_table
[half
].compat_offset
;
195 if (offset
== G_UNICODE_NOT_PRESENT_OFFSET
)
196 offset
= decomp_table
[half
].canon_offset
;
200 offset
= decomp_table
[half
].canon_offset
;
201 if (offset
== G_UNICODE_NOT_PRESENT_OFFSET
)
205 return &(decomp_expansion_string
[offset
]);
207 else if (half
== start
)
209 else if (ch
> decomp_table
[half
].ch
)
220 * g_unicode_canonical_decomposition:
221 * @ch: a Unicode character.
222 * @result_len: location to store the length of the return value.
224 * Computes the canonical decomposition of a Unicode character.
226 * Returns: a newly allocated string of Unicode characters.
227 * @result_len is set to the resulting length of the string.
229 * Deprecated: 2.30: Use the more flexible g_unichar_fully_decompose()
233 g_unicode_canonical_decomposition (gunichar ch
,
240 /* Hangul syllable */
241 if (ch
>= SBase
&& ch
< SBase
+ SCount
)
243 decompose_hangul (ch
, NULL
, result_len
);
244 r
= g_malloc (*result_len
* sizeof (gunichar
));
245 decompose_hangul (ch
, r
, result_len
);
247 else if ((decomp
= find_decomposition (ch
, FALSE
)) != NULL
)
252 *result_len
= g_utf8_strlen (decomp
, -1);
253 r
= g_malloc (*result_len
* sizeof (gunichar
));
255 for (p
= decomp
, i
= 0; *p
!= '\0'; p
= g_utf8_next_char (p
), i
++)
256 r
[i
] = g_utf8_get_char (p
);
260 /* Not in our table. */
261 r
= g_malloc (sizeof (gunichar
));
269 /* L,V => LV and LV,T => LVT */
271 combine_hangul (gunichar a
,
275 gint LIndex
= a
- LBase
;
276 gint SIndex
= a
- SBase
;
278 gint VIndex
= b
- VBase
;
279 gint TIndex
= b
- TBase
;
281 if (0 <= LIndex
&& LIndex
< LCount
282 && 0 <= VIndex
&& VIndex
< VCount
)
284 *result
= SBase
+ (LIndex
* VCount
+ VIndex
) * TCount
;
287 else if (0 <= SIndex
&& SIndex
< SCount
&& (SIndex
% TCount
) == 0
288 && 0 < TIndex
&& TIndex
< TCount
)
290 *result
= a
+ TIndex
;
297 #define CI(Page, Char) \
298 ((compose_table[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
299 ? (compose_table[Page] - G_UNICODE_MAX_TABLE_INDEX) \
300 : (compose_data[compose_table[Page]][Char]))
302 #define COMPOSE_INDEX(Char) \
303 (((Char >> 8) > (COMPOSE_TABLE_LAST)) ? 0 : CI((Char) >> 8, (Char) & 0xff))
310 gushort index_a
, index_b
;
312 if (combine_hangul (a
, b
, result
))
315 index_a
= COMPOSE_INDEX(a
);
317 if (index_a
>= COMPOSE_FIRST_SINGLE_START
&& index_a
< COMPOSE_SECOND_START
)
319 if (b
== compose_first_single
[index_a
- COMPOSE_FIRST_SINGLE_START
][0])
321 *result
= compose_first_single
[index_a
- COMPOSE_FIRST_SINGLE_START
][1];
328 index_b
= COMPOSE_INDEX(b
);
330 if (index_b
>= COMPOSE_SECOND_SINGLE_START
)
332 if (a
== compose_second_single
[index_b
- COMPOSE_SECOND_SINGLE_START
][0])
334 *result
= compose_second_single
[index_b
- COMPOSE_SECOND_SINGLE_START
][1];
341 if (index_a
>= COMPOSE_FIRST_START
&& index_a
< COMPOSE_FIRST_SINGLE_START
&&
342 index_b
>= COMPOSE_SECOND_START
&& index_b
< COMPOSE_SECOND_SINGLE_START
)
344 gunichar res
= compose_array
[index_a
- COMPOSE_FIRST_START
][index_b
- COMPOSE_SECOND_START
];
357 _g_utf8_normalize_wc (const gchar
*str
,
365 gboolean do_compat
= (mode
== G_NORMALIZE_NFKC
||
366 mode
== G_NORMALIZE_NFKD
);
367 gboolean do_compose
= (mode
== G_NORMALIZE_NFC
||
368 mode
== G_NORMALIZE_NFKC
);
372 while ((max_len
< 0 || p
< str
+ max_len
) && *p
)
375 gunichar wc
= g_utf8_get_char (p
);
377 if (wc
>= SBase
&& wc
< SBase
+ SCount
)
380 decompose_hangul (wc
, NULL
, &result_len
);
385 decomp
= find_decomposition (wc
, do_compat
);
388 n_wc
+= g_utf8_strlen (decomp
, -1);
393 p
= g_utf8_next_char (p
);
396 wc_buffer
= g_new (gunichar
, n_wc
+ 1);
401 while ((max_len
< 0 || p
< str
+ max_len
) && *p
)
403 gunichar wc
= g_utf8_get_char (p
);
406 gsize old_n_wc
= n_wc
;
408 if (wc
>= SBase
&& wc
< SBase
+ SCount
)
411 decompose_hangul (wc
, wc_buffer
+ n_wc
, &result_len
);
416 decomp
= find_decomposition (wc
, do_compat
);
421 for (pd
= decomp
; *pd
!= '\0'; pd
= g_utf8_next_char (pd
))
422 wc_buffer
[n_wc
++] = g_utf8_get_char (pd
);
425 wc_buffer
[n_wc
++] = wc
;
430 cc
= COMBINING_CLASS (wc_buffer
[old_n_wc
]);
434 g_unicode_canonical_ordering (wc_buffer
+ last_start
, n_wc
- last_start
);
435 last_start
= old_n_wc
;
439 p
= g_utf8_next_char (p
);
444 g_unicode_canonical_ordering (wc_buffer
+ last_start
, n_wc
- last_start
);
450 /* All decomposed and reordered */
452 if (do_compose
&& n_wc
> 0)
458 for (i
= 0; i
< n_wc
; i
++)
460 int cc
= COMBINING_CLASS (wc_buffer
[i
]);
463 (last_cc
== 0 || last_cc
< cc
) &&
464 combine (wc_buffer
[last_start
], wc_buffer
[i
],
465 &wc_buffer
[last_start
]))
467 for (j
= i
+ 1; j
< n_wc
; j
++)
468 wc_buffer
[j
-1] = wc_buffer
[j
];
475 last_cc
= COMBINING_CLASS (wc_buffer
[i
-1]);
494 * @str: a UTF-8 encoded string.
495 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
496 * @mode: the type of normalization to perform.
498 * Converts a string into canonical form, standardizing
499 * such issues as whether a character with an accent
500 * is represented as a base character and combining
501 * accent or as a single precomposed character. The
502 * string has to be valid UTF-8, otherwise %NULL is
503 * returned. You should generally call g_utf8_normalize()
504 * before comparing two Unicode strings.
506 * The normalization mode %G_NORMALIZE_DEFAULT only
507 * standardizes differences that do not affect the
508 * text content, such as the above-mentioned accent
509 * representation. %G_NORMALIZE_ALL also standardizes
510 * the "compatibility" characters in Unicode, such
511 * as SUPERSCRIPT THREE to the standard forms
512 * (in this case DIGIT THREE). Formatting information
513 * may be lost but for most text operations such
514 * characters should be considered the same.
516 * %G_NORMALIZE_DEFAULT_COMPOSE and %G_NORMALIZE_ALL_COMPOSE
517 * are like %G_NORMALIZE_DEFAULT and %G_NORMALIZE_ALL,
518 * but returned a result with composed forms rather
519 * than a maximally decomposed form. This is often
520 * useful if you intend to convert the string to
521 * a legacy encoding or pass it to a system with
522 * less capable Unicode handling.
524 * Returns: a newly allocated string, that is the
525 * normalized form of @str, or %NULL if @str is not
529 g_utf8_normalize (const gchar
*str
,
533 gunichar
*result_wc
= _g_utf8_normalize_wc (str
, len
, mode
);
536 result
= g_ucs4_to_utf8 (result_wc
, -1, NULL
, NULL
, NULL
);
543 decompose_hangul_step (gunichar ch
,
549 if (ch
< SBase
|| ch
>= SBase
+ SCount
)
550 return FALSE
; /* not a hangul syllable */
553 TIndex
= SIndex
% TCount
;
557 /* split LVT -> LV,T */
563 /* split LV -> L,V */
564 *a
= LBase
+ SIndex
/ NCount
;
565 *b
= VBase
+ (SIndex
% NCount
) / TCount
;
572 * g_unichar_decompose:
573 * @ch: a Unicode character
574 * @a: return location for the first component of @ch
575 * @b: return location for the second component of @ch
577 * Performs a single decomposition step of the
578 * Unicode canonical decomposition algorithm.
580 * This function does not include compatibility
581 * decompositions. It does, however, include algorithmic
582 * Hangul Jamo decomposition, as well as 'singleton'
583 * decompositions which replace a character by a single
584 * other character. In the case of singletons *@b will
587 * If @ch is not decomposable, *@a is set to @ch and *@b
590 * Note that the way Unicode decomposition pairs are
591 * defined, it is guaranteed that @b would not decompose
592 * further, but @a may itself decompose. To get the full
593 * canonical decomposition for @ch, one would need to
594 * recursively call this function on @a. Or use
595 * g_unichar_fully_decompose().
598 * [UAX#15](http://unicode.org/reports/tr15/)
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.
665 * [UAX#15](http://unicode.org/reports/tr15/)
668 * Returns: %TRUE if the characters could be composed
673 g_unichar_compose (gunichar a
,
677 if (combine (a
, b
, ch
))
685 * g_unichar_fully_decompose:
686 * @ch: a Unicode character.
687 * @compat: whether perform canonical or compatibility decomposition
688 * @result: (nullable): location to store decomposed result, or %NULL
689 * @result_len: length of @result
691 * Computes the canonical or compatibility decomposition of a
692 * Unicode character. For compatibility decomposition,
693 * pass %TRUE for @compat; for canonical decomposition
694 * pass %FALSE for @compat.
696 * The decomposed sequence is placed in @result. Only up to
697 * @result_len characters are written into @result. The length
698 * of the full decomposition (irrespective of @result_len) is
699 * returned by the function. For canonical decomposition,
700 * currently all decompositions are of length at most 4, but
701 * this may change in the future (very unlikely though).
702 * At any rate, Unicode does guarantee that a buffer of length
703 * 18 is always enough for both compatibility and canonical
704 * decompositions, so that is the size recommended. This is provided
705 * as %G_UNICHAR_MAX_DECOMPOSITION_LENGTH.
708 * [UAX#15](http://unicode.org/reports/tr15/)
711 * Returns: the length of the full decomposition.
716 g_unichar_fully_decompose (gunichar ch
,
724 /* Hangul syllable */
725 if (ch
>= SBase
&& ch
< SBase
+ SCount
)
729 decompose_hangul (ch
, result
? buffer
: NULL
, &len
);
731 for (i
= 0; i
< len
&& i
< result_len
; i
++)
732 result
[i
] = buffer
[i
];
735 else if ((decomp
= find_decomposition (ch
, compat
)) != NULL
)
740 len
= g_utf8_strlen (decomp
, -1);
742 for (p
= decomp
, i
= 0; i
< len
&& i
< result_len
; p
= g_utf8_next_char (p
), i
++)
743 result
[i
] = g_utf8_get_char (p
);
748 /* Does not decompose */
749 if (result
&& result_len
>= 1)