Imported Upstream version 2.74.3
[platform/upstream/glib.git] / glib / gunidecomp.c
1 /* decomp.c - Character decomposition.
2  *
3  *  Copyright (C) 1999, 2000 Tom Tromey
4  *  Copyright 2000 Red Hat, Inc.
5  *
6  * SPDX-License-Identifier: LGPL-2.1-or-later
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 /**
23  * SECTION:unicode
24  * @Title: Unicode Manipulation
25  * @Short_description: functions operating on Unicode characters and
26  *     UTF-8 strings
27  * @See_also: g_locale_to_utf8(), g_locale_from_utf8()
28  *
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.
36  *
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/).
40  *
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
57  */
58
59 #include "config.h"
60
61 #include <stdlib.h>
62
63 #include "gunicode.h"
64 #include "gunidecomp.h"
65 #include "gmem.h"
66 #include "gunicomp.h"
67 #include "gunicodeprivate.h"
68
69
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]))
74
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]))
79
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) \
85       : 0))
86
87 /**
88  * g_unichar_combining_class:
89  * @uc: a Unicode character
90  * 
91  * Determines the canonical combining class of a Unicode character.
92  * 
93  * Returns: the combining class of the character
94  *
95  * Since: 2.14
96  **/
97 gint
98 g_unichar_combining_class (gunichar uc)
99 {
100   return COMBINING_CLASS (uc);
101 }
102
103 /* constants for hangul syllable [de]composition */
104 #define SBase 0xAC00 
105 #define LBase 0x1100 
106 #define VBase 0x1161 
107 #define TBase 0x11A7
108 #define LCount 19 
109 #define VCount 21
110 #define TCount 28
111 #define NCount (VCount * TCount)
112 #define SCount (LCount * NCount)
113
114 /**
115  * g_unicode_canonical_ordering:
116  * @string: a UCS-4 encoded string.
117  * @len: the maximum length of @string to use.
118  *
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. 
123  **/
124 void
125 g_unicode_canonical_ordering (gunichar *string,
126                               gsize     len)
127 {
128   gsize i;
129   int swap = 1;
130
131   while (swap)
132     {
133       int last;
134       swap = 0;
135       last = COMBINING_CLASS (string[0]);
136       for (i = 0; i < len - 1; ++i)
137         {
138           int next = COMBINING_CLASS (string[i + 1]);
139           if (next != 0 && last > next)
140             {
141               gsize j;
142               /* Percolate item leftward through string.  */
143               for (j = i + 1; j > 0; --j)
144                 {
145                   gunichar t;
146                   if (COMBINING_CLASS (string[j - 1]) <= next)
147                     break;
148                   t = string[j];
149                   string[j] = string[j - 1];
150                   string[j - 1] = t;
151                   swap = 1;
152                 }
153               /* We're re-entering the loop looking at the old
154                  character again.  */
155               next = last;
156             }
157           last = next;
158         }
159     }
160 }
161
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. */
166 static void
167 decompose_hangul (gunichar s,
168                   gunichar *r,
169                   gsize *result_len)
170 {
171   gint SIndex = s - SBase;
172   gint TIndex = SIndex % TCount;
173
174   if (r)
175     {
176       r[0] = LBase + SIndex / NCount;
177       r[1] = VBase + (SIndex % NCount) / TCount;
178     }
179
180   if (TIndex)
181     {
182       if (r)
183         r[2] = TBase + TIndex;
184       *result_len = 3;
185     }
186   else
187     *result_len = 2;
188 }
189
190 /* returns a pointer to a null-terminated UTF-8 string */
191 static const gchar *
192 find_decomposition (gunichar ch,
193                     gboolean compat)
194 {
195   int start = 0;
196   int end = G_N_ELEMENTS (decomp_table);
197   
198   if (ch >= decomp_table[start].ch &&
199       ch <= decomp_table[end - 1].ch)
200     {
201       while (TRUE)
202         {
203           int half = (start + end) / 2;
204           if (ch == decomp_table[half].ch)
205             {
206               int offset;
207
208               if (compat)
209                 {
210                   offset = decomp_table[half].compat_offset;
211                   if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
212                     offset = decomp_table[half].canon_offset;
213                 }
214               else
215                 {
216                   offset = decomp_table[half].canon_offset;
217                   if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
218                     return NULL;
219                 }
220               
221               return &(decomp_expansion_string[offset]);
222             }
223           else if (half == start)
224             break;
225           else if (ch > decomp_table[half].ch)
226             start = half;
227           else
228             end = half;
229         }
230     }
231
232   return NULL;
233 }
234
235 /**
236  * g_unicode_canonical_decomposition:
237  * @ch: a Unicode character.
238  * @result_len: location to store the length of the return value.
239  *
240  * Computes the canonical decomposition of a Unicode character.  
241  * 
242  * Returns: a newly allocated string of Unicode characters.
243  *   @result_len is set to the resulting length of the string.
244  *
245  * Deprecated: 2.30: Use the more flexible g_unichar_fully_decompose()
246  *   instead.
247  **/
248 gunichar *
249 g_unicode_canonical_decomposition (gunichar ch,
250                                    gsize   *result_len)
251 {
252   const gchar *decomp;
253   const gchar *p;
254   gunichar *r;
255
256   /* Hangul syllable */
257   if (ch >= SBase && ch < SBase + SCount)
258     {
259       decompose_hangul (ch, NULL, result_len);
260       r = g_malloc (*result_len * sizeof (gunichar));
261       decompose_hangul (ch, r, result_len);
262     }
263   else if ((decomp = find_decomposition (ch, FALSE)) != NULL)
264     {
265       /* Found it.  */
266       int i;
267       
268       *result_len = g_utf8_strlen (decomp, -1);
269       r = g_malloc (*result_len * sizeof (gunichar));
270       
271       for (p = decomp, i = 0; *p != '\0'; p = g_utf8_next_char (p), i++)
272         r[i] = g_utf8_get_char (p);
273     }
274   else
275     {
276       /* Not in our table.  */
277       r = g_malloc (sizeof (gunichar));
278       *r = ch;
279       *result_len = 1;
280     }
281
282   return r;
283 }
284
285 /* L,V => LV and LV,T => LVT  */
286 static gboolean
287 combine_hangul (gunichar a,
288                 gunichar b,
289                 gunichar *result)
290 {
291   gint LIndex = a - LBase;
292   gint SIndex = a - SBase;
293
294   gint VIndex = b - VBase;
295   gint TIndex = b - TBase;
296
297   if (0 <= LIndex && LIndex < LCount
298       && 0 <= VIndex && VIndex < VCount)
299     {
300       *result = SBase + (LIndex * VCount + VIndex) * TCount;
301       return TRUE;
302     }
303   else if (0 <= SIndex && SIndex < SCount && (SIndex % TCount) == 0
304            && 0 < TIndex && TIndex < TCount)
305     {
306       *result = a + TIndex;
307       return TRUE;
308     }
309
310   return FALSE;
311 }
312
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]))
317
318 #define COMPOSE_INDEX(Char) \
319      (((Char >> 8) > (COMPOSE_TABLE_LAST)) ? 0 : CI((Char) >> 8, (Char) & 0xff))
320
321 static gboolean
322 combine (gunichar  a,
323          gunichar  b,
324          gunichar *result)
325 {
326   gushort index_a, index_b;
327
328   if (combine_hangul (a, b, result))
329     return TRUE;
330
331   index_a = COMPOSE_INDEX(a);
332
333   if (index_a >= COMPOSE_FIRST_SINGLE_START && index_a < COMPOSE_SECOND_START)
334     {
335       if (b == compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][0])
336         {
337           *result = compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][1];
338           return TRUE;
339         }
340       else
341         return FALSE;
342     }
343   
344   index_b = COMPOSE_INDEX(b);
345
346   if (index_b >= COMPOSE_SECOND_SINGLE_START)
347     {
348       if (a == compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][0])
349         {
350           *result = compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][1];
351           return TRUE;
352         }
353       else
354         return FALSE;
355     }
356
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)
359     {
360       gunichar res = compose_array[index_a - COMPOSE_FIRST_START][index_b - COMPOSE_SECOND_START];
361
362       if (res)
363         {
364           *result = res;
365           return TRUE;
366         }
367     }
368
369   return FALSE;
370 }
371
372 gunichar *
373 _g_utf8_normalize_wc (const gchar    *str,
374                       gssize          max_len,
375                       GNormalizeMode  mode)
376 {
377   gsize n_wc;
378   gunichar *wc_buffer;
379   const char *p;
380   gsize last_start;
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);
385
386   n_wc = 0;
387   p = str;
388   while ((max_len < 0 || p < str + max_len) && *p)
389     {
390       const gchar *decomp;
391       gunichar wc = g_utf8_get_char (p);
392
393       if (wc >= SBase && wc < SBase + SCount)
394         {
395           gsize result_len;
396           decompose_hangul (wc, NULL, &result_len);
397           n_wc += result_len;
398         }
399       else 
400         {
401           decomp = find_decomposition (wc, do_compat);
402
403           if (decomp)
404             n_wc += g_utf8_strlen (decomp, -1);
405           else
406             n_wc++;
407         }
408
409       p = g_utf8_next_char (p);
410     }
411
412   wc_buffer = g_new (gunichar, n_wc + 1);
413
414   last_start = 0;
415   n_wc = 0;
416   p = str;
417   while ((max_len < 0 || p < str + max_len) && *p)
418     {
419       gunichar wc = g_utf8_get_char (p);
420       const gchar *decomp;
421       int cc;
422       gsize old_n_wc = n_wc;
423           
424       if (wc >= SBase && wc < SBase + SCount)
425         {
426           gsize result_len;
427           decompose_hangul (wc, wc_buffer + n_wc, &result_len);
428           n_wc += result_len;
429         }
430       else
431         {
432           decomp = find_decomposition (wc, do_compat);
433           
434           if (decomp)
435             {
436               const char *pd;
437               for (pd = decomp; *pd != '\0'; pd = g_utf8_next_char (pd))
438                 wc_buffer[n_wc++] = g_utf8_get_char (pd);
439             }
440           else
441             wc_buffer[n_wc++] = wc;
442         }
443
444       if (n_wc > 0)
445         {
446           cc = COMBINING_CLASS (wc_buffer[old_n_wc]);
447
448           if (cc == 0)
449             {
450               g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
451               last_start = old_n_wc;
452             }
453         }
454       
455       p = g_utf8_next_char (p);
456     }
457
458   if (n_wc > 0)
459     {
460       g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
461       last_start = n_wc;
462       (void) last_start;
463     }
464           
465   wc_buffer[n_wc] = 0;
466
467   /* All decomposed and reordered */ 
468
469   if (do_compose && n_wc > 0)
470     {
471       gsize i, j;
472       int last_cc = 0;
473       last_start = 0;
474       
475       for (i = 0; i < n_wc; i++)
476         {
477           int cc = COMBINING_CLASS (wc_buffer[i]);
478
479           if (i > 0 &&
480               (last_cc == 0 || last_cc < cc) &&
481               combine (wc_buffer[last_start], wc_buffer[i],
482                        &wc_buffer[last_start]))
483             {
484               for (j = i + 1; j < n_wc; j++)
485                 wc_buffer[j-1] = wc_buffer[j];
486               n_wc--;
487               i--;
488               
489               if (i == last_start)
490                 last_cc = 0;
491               else
492                 last_cc = COMBINING_CLASS (wc_buffer[i-1]);
493               
494               continue;
495             }
496
497           if (cc == 0)
498             last_start = i;
499
500           last_cc = cc;
501         }
502     }
503
504   wc_buffer[n_wc] = 0;
505
506   return wc_buffer;
507 }
508
509 /**
510  * g_utf8_normalize:
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.
514  *
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.
522  *
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.
532  *
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.
540  *
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.
544  **/
545 gchar *
546 g_utf8_normalize (const gchar    *str,
547                   gssize          len,
548                   GNormalizeMode  mode)
549 {
550   gunichar *result_wc = _g_utf8_normalize_wc (str, len, mode);
551   gchar *result;
552
553   result = g_ucs4_to_utf8 (result_wc, -1, NULL, NULL, NULL);
554   g_free (result_wc);
555
556   return result;
557 }
558
559 static gboolean
560 decompose_hangul_step (gunichar  ch,
561                        gunichar *a,
562                        gunichar *b)
563 {
564   gint SIndex, TIndex;
565
566   if (ch < SBase || ch >= SBase + SCount)
567     return FALSE;  /* not a hangul syllable */
568
569   SIndex = ch - SBase;
570   TIndex = SIndex % TCount;
571
572   if (TIndex)
573     {
574       /* split LVT -> LV,T */
575       *a = ch - TIndex;
576       *b = TBase + TIndex;
577     }
578   else
579     {
580       /* split LV -> L,V */
581       *a = LBase + SIndex / NCount;
582       *b = VBase + (SIndex % NCount) / TCount;
583     }
584
585   return TRUE;
586 }
587
588 /**
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
593  *
594  * Performs a single decomposition step of the
595  * Unicode canonical decomposition algorithm.
596  *
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
602  * be set to zero.
603  *
604  * If @ch is not decomposable, *@a is set to @ch and *@b
605  * is set to zero.
606  *
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().
613  *
614  * See
615  * [UAX#15](http://unicode.org/reports/tr15/)
616  * for details.
617  *
618  * Returns: %TRUE if the character could be decomposed
619  *
620  * Since: 2.30
621  */
622 gboolean
623 g_unichar_decompose (gunichar  ch,
624                      gunichar *a,
625                      gunichar *b)
626 {
627   gint start = 0;
628   gint end = G_N_ELEMENTS (decomp_step_table);
629
630   if (decompose_hangul_step (ch, a, b))
631     return TRUE;
632
633   /* TODO use bsearch() */
634   if (ch >= decomp_step_table[start].ch &&
635       ch <= decomp_step_table[end - 1].ch)
636     {
637       while (TRUE)
638         {
639           gint half = (start + end) / 2;
640           const decomposition_step *p = &(decomp_step_table[half]);
641           if (ch == p->ch)
642             {
643               *a = p->a;
644               *b = p->b;
645               return TRUE;
646             }
647           else if (half == start)
648             break;
649           else if (ch > p->ch)
650             start = half;
651           else
652             end = half;
653         }
654     }
655
656   *a = ch;
657   *b = 0;
658
659   return FALSE;
660 }
661
662 /**
663  * g_unichar_compose:
664  * @a: a Unicode character
665  * @b: a Unicode character
666  * @ch: (out) (not optional): return location for the composed character
667  *
668  * Performs a single composition step of the
669  * Unicode canonical composition algorithm.
670  *
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.
678  *
679  * If @a and @b do not compose a new character, @ch is set to zero.
680  *
681  * See
682  * [UAX#15](http://unicode.org/reports/tr15/)
683  * for details.
684  *
685  * Returns: %TRUE if the characters could be composed
686  *
687  * Since: 2.30
688  */
689 gboolean
690 g_unichar_compose (gunichar  a,
691                    gunichar  b,
692                    gunichar *ch)
693 {
694   if (combine (a, b, ch))
695     return TRUE;
696
697   *ch = 0;
698   return FALSE;
699 }
700
701 /**
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
707  *
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.
712  *
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.
723  *
724  * See
725  * [UAX#15](http://unicode.org/reports/tr15/)
726  * for details.
727  *
728  * Returns: the length of the full decomposition.
729  *
730  * Since: 2.30
731  **/
732 gsize
733 g_unichar_fully_decompose (gunichar  ch,
734                            gboolean  compat,
735                            gunichar *result,
736                            gsize     result_len)
737 {
738   const gchar *decomp;
739   const gchar *p;
740
741   /* Hangul syllable */
742   if (ch >= SBase && ch < SBase + SCount)
743     {
744       gsize len, i;
745       gunichar buffer[3];
746       decompose_hangul (ch, result ? buffer : NULL, &len);
747       if (result)
748         for (i = 0; i < len && i < result_len; i++)
749           result[i] = buffer[i];
750       return len;
751     }
752   else if ((decomp = find_decomposition (ch, compat)) != NULL)
753     {
754       /* Found it.  */
755       gsize len, i;
756
757       len = g_utf8_strlen (decomp, -1);
758
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);
761
762       return len;
763     }
764
765   /* Does not decompose */
766   if (result && result_len >= 1)
767     *result = ch;
768   return 1;
769 }