Coverage Report

Created: 2025-11-16 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gstreamer/subprojects/glib-2.82.5/glib/gunidecomp.c
Line
Count
Source
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
#include "config.h"
23
24
#include <stdlib.h>
25
26
#include "gunicode.h"
27
#include "gunidecomp.h"
28
#include "gmem.h"
29
#include "gtestutils.h"
30
#include "gunicomp.h"
31
#include "gunicodeprivate.h"
32
33
34
#define CC_PART1(Page, Char) \
35
0
  ((combining_class_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
36
0
   ? (combining_class_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
37
0
   : (cclass_data[combining_class_table_part1[Page]][Char]))
38
39
#define CC_PART2(Page, Char) \
40
0
  ((combining_class_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
41
0
   ? (combining_class_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
42
0
   : (cclass_data[combining_class_table_part2[Page]][Char]))
43
44
#define COMBINING_CLASS(Char) \
45
0
  (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
46
0
   ? CC_PART1 ((Char) >> 8, (Char) & 0xff) \
47
0
   : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
48
0
      ? CC_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
49
0
      : 0))
50
51
/**
52
 * g_unichar_combining_class:
53
 * @uc: a Unicode character
54
 * 
55
 * Determines the canonical combining class of a Unicode character.
56
 * 
57
 * Returns: the combining class of the character
58
 *
59
 * Since: 2.14
60
 **/
61
gint
62
g_unichar_combining_class (gunichar uc)
63
0
{
64
0
  return COMBINING_CLASS (uc);
65
0
}
66
67
/* constants for hangul syllable [de]composition */
68
0
#define SBase 0xAC00 
69
0
#define LBase 0x1100 
70
0
#define VBase 0x1161 
71
0
#define TBase 0x11A7
72
0
#define LCount 19 
73
0
#define VCount 21
74
0
#define TCount 28
75
0
#define NCount (VCount * TCount)
76
0
#define SCount (LCount * NCount)
77
78
/**
79
 * g_unicode_canonical_ordering:
80
 * @string: (array length=len) (element-type gunichar): a UCS-4 encoded string.
81
 * @len: the maximum length of @string to use.
82
 *
83
 * Computes the canonical ordering of a string in-place.  
84
 * This rearranges decomposed characters in the string 
85
 * according to their combining classes.  See the Unicode 
86
 * manual for more information. 
87
 **/
88
void
89
g_unicode_canonical_ordering (gunichar *string,
90
            gsize     len)
91
0
{
92
0
  gsize i;
93
0
  int swap = 1;
94
95
0
  while (swap)
96
0
    {
97
0
      int last;
98
0
      swap = 0;
99
0
      last = COMBINING_CLASS (string[0]);
100
0
      for (i = 0; i < len - 1; ++i)
101
0
  {
102
0
    int next = COMBINING_CLASS (string[i + 1]);
103
0
    if (next != 0 && last > next)
104
0
      {
105
0
        gsize j;
106
        /* Percolate item leftward through string.  */
107
0
        for (j = i + 1; j > 0; --j)
108
0
    {
109
0
      gunichar t;
110
0
      if (COMBINING_CLASS (string[j - 1]) <= next)
111
0
        break;
112
0
      t = string[j];
113
0
      string[j] = string[j - 1];
114
0
      string[j - 1] = t;
115
0
      swap = 1;
116
0
    }
117
        /* We're re-entering the loop looking at the old
118
     character again.  */
119
0
        next = last;
120
0
      }
121
0
    last = next;
122
0
  }
123
0
    }
124
0
}
125
126
/* http://www.unicode.org/unicode/reports/tr15/#Hangul
127
 * r should be null or have sufficient space. Calling with r == NULL will
128
 * only calculate the result_len; however, a buffer with space for three
129
 * characters will always be big enough. */
130
static void
131
decompose_hangul (gunichar s,
132
                  gunichar *r,
133
                  gsize *result_len)
134
0
{
135
0
  gint SIndex = s - SBase;
136
0
  gint TIndex = SIndex % TCount;
137
138
0
  if (r)
139
0
    {
140
0
      r[0] = LBase + SIndex / NCount;
141
0
      r[1] = VBase + (SIndex % NCount) / TCount;
142
0
    }
143
144
0
  if (TIndex)
145
0
    {
146
0
      if (r)
147
0
  r[2] = TBase + TIndex;
148
0
      *result_len = 3;
149
0
    }
150
0
  else
151
0
    *result_len = 2;
152
0
}
153
154
/* returns a pointer to a null-terminated UTF-8 string */
155
static const gchar *
156
find_decomposition (gunichar ch,
157
        gboolean compat)
158
0
{
159
0
  int start = 0;
160
0
  int end = G_N_ELEMENTS (decomp_table);
161
  
162
0
  if (ch >= decomp_table[start].ch &&
163
0
      ch <= decomp_table[end - 1].ch)
164
0
    {
165
0
      while (TRUE)
166
0
  {
167
0
    int half = (start + end) / 2;
168
0
    if (ch == decomp_table[half].ch)
169
0
      {
170
0
        int offset;
171
172
0
        if (compat)
173
0
    {
174
0
      offset = decomp_table[half].compat_offset;
175
0
      if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
176
0
        offset = decomp_table[half].canon_offset;
177
0
    }
178
0
        else
179
0
    {
180
0
      offset = decomp_table[half].canon_offset;
181
0
      if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
182
0
        return NULL;
183
0
    }
184
        
185
0
        return &(decomp_expansion_string[offset]);
186
0
      }
187
0
    else if (half == start)
188
0
      break;
189
0
    else if (ch > decomp_table[half].ch)
190
0
      start = half;
191
0
    else
192
0
      end = half;
193
0
  }
194
0
    }
195
196
0
  return NULL;
197
0
}
198
199
/**
200
 * g_unicode_canonical_decomposition:
201
 * @ch: a Unicode character.
202
 * @result_len: location to store the length of the return value.
203
 *
204
 * Computes the canonical decomposition of a Unicode character.  
205
 * 
206
 * Returns: a newly allocated string of Unicode characters.
207
 *   @result_len is set to the resulting length of the string.
208
 *
209
 * Deprecated: 2.30: Use the more flexible g_unichar_fully_decompose()
210
 *   instead.
211
 **/
212
gunichar *
213
g_unicode_canonical_decomposition (gunichar ch,
214
           gsize   *result_len)
215
0
{
216
0
  const gchar *decomp;
217
0
  const gchar *p;
218
0
  gunichar *r;
219
220
  /* Hangul syllable */
221
0
  if (ch >= SBase && ch < SBase + SCount)
222
0
    {
223
0
      decompose_hangul (ch, NULL, result_len);
224
0
      r = g_malloc (*result_len * sizeof (gunichar));
225
0
      decompose_hangul (ch, r, result_len);
226
0
    }
227
0
  else if ((decomp = find_decomposition (ch, FALSE)) != NULL)
228
0
    {
229
      /* Found it.  */
230
0
      int i;
231
      
232
0
      *result_len = g_utf8_strlen (decomp, -1);
233
0
      r = g_malloc (*result_len * sizeof (gunichar));
234
      
235
0
      for (p = decomp, i = 0; *p != '\0'; p = g_utf8_next_char (p), i++)
236
0
        r[i] = g_utf8_get_char (p);
237
0
    }
238
0
  else
239
0
    {
240
      /* Not in our table.  */
241
0
      r = g_malloc (sizeof (gunichar));
242
0
      *r = ch;
243
0
      *result_len = 1;
244
0
    }
245
246
0
  return r;
247
0
}
248
249
/* L,V => LV and LV,T => LVT  */
250
static gboolean
251
combine_hangul (gunichar a,
252
                gunichar b,
253
                gunichar *result)
254
0
{
255
0
  gint LIndex = a - LBase;
256
0
  gint SIndex = a - SBase;
257
258
0
  gint VIndex = b - VBase;
259
0
  gint TIndex = b - TBase;
260
261
0
  if (0 <= LIndex && LIndex < LCount
262
0
      && 0 <= VIndex && VIndex < VCount)
263
0
    {
264
0
      *result = SBase + (LIndex * VCount + VIndex) * TCount;
265
0
      return TRUE;
266
0
    }
267
0
  else if (0 <= SIndex && SIndex < SCount && (SIndex % TCount) == 0
268
0
           && 0 < TIndex && TIndex < TCount)
269
0
    {
270
0
      *result = a + TIndex;
271
0
      return TRUE;
272
0
    }
273
274
0
  return FALSE;
275
0
}
276
277
#define CI(Page, Char) \
278
0
  ((compose_table[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
279
0
   ? (compose_table[Page] - G_UNICODE_MAX_TABLE_INDEX) \
280
0
   : (compose_data[compose_table[Page]][Char]))
281
282
#define COMPOSE_INDEX(Char) \
283
0
     (((Char >> 8) > (COMPOSE_TABLE_LAST)) ? 0 : CI((Char) >> 8, (Char) & 0xff))
284
285
static gboolean
286
combine (gunichar  a,
287
   gunichar  b,
288
   gunichar *result)
289
0
{
290
0
  gushort index_a, index_b;
291
292
0
  if (combine_hangul (a, b, result))
293
0
    return TRUE;
294
295
0
  index_a = COMPOSE_INDEX(a);
296
297
0
  if (index_a >= COMPOSE_FIRST_SINGLE_START && index_a < COMPOSE_SECOND_START)
298
0
    {
299
0
      if (b == compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][0])
300
0
  {
301
0
    *result = compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][1];
302
0
    return TRUE;
303
0
  }
304
0
      else
305
0
        return FALSE;
306
0
    }
307
  
308
0
  index_b = COMPOSE_INDEX(b);
309
310
0
  if (index_b >= COMPOSE_SECOND_SINGLE_START)
311
0
    {
312
0
      if (a == compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][0])
313
0
  {
314
0
    *result = compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][1];
315
0
    return TRUE;
316
0
  }
317
0
      else
318
0
        return FALSE;
319
0
    }
320
321
0
  if (index_a >= COMPOSE_FIRST_START && index_a < COMPOSE_FIRST_SINGLE_START &&
322
0
      index_b >= COMPOSE_SECOND_START && index_b < COMPOSE_SECOND_SINGLE_START)
323
0
    {
324
0
      gunichar res = compose_array[index_a - COMPOSE_FIRST_START][index_b - COMPOSE_SECOND_START];
325
326
0
      if (res)
327
0
  {
328
0
    *result = res;
329
0
    return TRUE;
330
0
  }
331
0
    }
332
333
0
  return FALSE;
334
0
}
335
336
gunichar *
337
_g_utf8_normalize_wc (const gchar    *str,
338
          gssize          max_len,
339
          GNormalizeMode  mode)
340
0
{
341
0
  gsize n_wc;
342
0
  gunichar *wc_buffer;
343
0
  const char *p;
344
0
  gsize last_start;
345
0
  gboolean do_compat = (mode == G_NORMALIZE_NFKC ||
346
0
      mode == G_NORMALIZE_NFKD);
347
0
  gboolean do_compose = (mode == G_NORMALIZE_NFC ||
348
0
       mode == G_NORMALIZE_NFKC);
349
350
0
  n_wc = 0;
351
0
  p = str;
352
0
  while ((max_len < 0 || p < str + max_len) && *p)
353
0
    {
354
0
      const gchar *decomp;
355
0
      const char *next, *between;
356
0
      gunichar wc;
357
358
0
      next = g_utf8_next_char (p);
359
      /* Avoid reading truncated multibyte characters
360
         which run past the end of the buffer */
361
0
      if (max_len < 0)
362
0
        {
363
          /* Does the character contain a NUL terminator? */
364
0
          for (between = &p[1]; between < next; between++)
365
0
            {
366
0
              if (G_UNLIKELY (!*between))
367
0
                return NULL;
368
0
            }
369
0
        }
370
0
      else
371
0
        {
372
0
          if (G_UNLIKELY (next > str + max_len))
373
0
            return NULL;
374
0
        }
375
0
      wc = g_utf8_get_char (p);
376
377
0
      if (G_UNLIKELY (wc == (gunichar) -1))
378
0
        {
379
0
          return NULL;
380
0
        }
381
0
      else if (wc >= SBase && wc < SBase + SCount)
382
0
        {
383
0
          gsize result_len;
384
0
          decompose_hangul (wc, NULL, &result_len);
385
0
          n_wc += result_len;
386
0
        }
387
0
      else 
388
0
        {
389
0
          decomp = find_decomposition (wc, do_compat);
390
391
0
          if (decomp)
392
0
            n_wc += g_utf8_strlen (decomp, -1);
393
0
          else
394
0
            n_wc++;
395
0
        }
396
397
0
      p = next;
398
0
    }
399
400
0
  wc_buffer = g_new (gunichar, n_wc + 1);
401
402
0
  last_start = 0;
403
0
  n_wc = 0;
404
0
  p = str;
405
0
  while ((max_len < 0 || p < str + max_len) && *p)
406
0
    {
407
0
      gunichar wc = g_utf8_get_char (p);
408
0
      const gchar *decomp;
409
0
      int cc;
410
0
      gsize old_n_wc = n_wc;
411
    
412
0
      if (wc >= SBase && wc < SBase + SCount)
413
0
        {
414
0
          gsize result_len;
415
0
          decompose_hangul (wc, wc_buffer + n_wc, &result_len);
416
0
          n_wc += result_len;
417
0
        }
418
0
      else
419
0
        {
420
0
          decomp = find_decomposition (wc, do_compat);
421
          
422
0
          if (decomp)
423
0
            {
424
0
              const char *pd;
425
0
              for (pd = decomp; *pd != '\0'; pd = g_utf8_next_char (pd))
426
0
                wc_buffer[n_wc++] = g_utf8_get_char (pd);
427
0
            }
428
0
          else
429
0
            wc_buffer[n_wc++] = wc;
430
0
        }
431
432
      /* Each code path above *must* have appended at least gunichar to wc_buffer. */
433
0
      g_assert (n_wc > old_n_wc);
434
435
0
      cc = COMBINING_CLASS (wc_buffer[old_n_wc]);
436
437
0
      if (cc == 0)
438
0
        {
439
0
          g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
440
0
          last_start = old_n_wc;
441
0
        }
442
      
443
0
      p = g_utf8_next_char (p);
444
0
    }
445
446
0
  if (n_wc > 0)
447
0
    {
448
0
      g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
449
0
      last_start = n_wc;
450
0
      (void) last_start;
451
0
    }
452
    
453
0
  wc_buffer[n_wc] = 0;
454
455
  /* All decomposed and reordered */ 
456
457
0
  if (do_compose && n_wc > 0)
458
0
    {
459
0
      gsize i, j;
460
0
      int last_cc = 0;
461
0
      last_start = 0;
462
      
463
0
      for (i = 0; i < n_wc; i++)
464
0
  {
465
0
    int cc = COMBINING_CLASS (wc_buffer[i]);
466
467
0
    if (i > 0 &&
468
0
        (last_cc == 0 || last_cc < cc) &&
469
0
        combine (wc_buffer[last_start], wc_buffer[i],
470
0
           &wc_buffer[last_start]))
471
0
      {
472
0
        for (j = i + 1; j < n_wc; j++)
473
0
    wc_buffer[j-1] = wc_buffer[j];
474
0
        n_wc--;
475
0
        i--;
476
        
477
0
        if (i == last_start)
478
0
    last_cc = 0;
479
0
        else
480
0
    last_cc = COMBINING_CLASS (wc_buffer[i-1]);
481
        
482
0
        continue;
483
0
      }
484
485
0
    if (cc == 0)
486
0
      last_start = i;
487
488
0
    last_cc = cc;
489
0
  }
490
0
    }
491
492
0
  wc_buffer[n_wc] = 0;
493
494
0
  return wc_buffer;
495
0
}
496
497
/**
498
 * g_utf8_normalize:
499
 * @str: a UTF-8 encoded string.
500
 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
501
 * @mode: the type of normalization to perform.
502
 *
503
 * Converts a string into canonical form, standardizing
504
 * such issues as whether a character with an accent
505
 * is represented as a base character and combining
506
 * accent or as a single precomposed character. The
507
 * string has to be valid UTF-8, otherwise %NULL is
508
 * returned. You should generally call g_utf8_normalize()
509
 * before comparing two Unicode strings.
510
 *
511
 * The normalization mode %G_NORMALIZE_DEFAULT only
512
 * standardizes differences that do not affect the
513
 * text content, such as the above-mentioned accent
514
 * representation. %G_NORMALIZE_ALL also standardizes
515
 * the "compatibility" characters in Unicode, such
516
 * as SUPERSCRIPT THREE to the standard forms
517
 * (in this case DIGIT THREE). Formatting information
518
 * may be lost but for most text operations such
519
 * characters should be considered the same.
520
 *
521
 * %G_NORMALIZE_DEFAULT_COMPOSE and %G_NORMALIZE_ALL_COMPOSE
522
 * are like %G_NORMALIZE_DEFAULT and %G_NORMALIZE_ALL,
523
 * but returned a result with composed forms rather
524
 * than a maximally decomposed form. This is often
525
 * useful if you intend to convert the string to
526
 * a legacy encoding or pass it to a system with
527
 * less capable Unicode handling.
528
 *
529
 * Returns: (nullable): a newly allocated string, that
530
 *   is the normalized form of @str, or %NULL if @str
531
 *   is not valid UTF-8.
532
 **/
533
gchar *
534
g_utf8_normalize (const gchar    *str,
535
      gssize          len,
536
      GNormalizeMode  mode)
537
0
{
538
0
  gunichar *result_wc = _g_utf8_normalize_wc (str, len, mode);
539
0
  gchar *result = NULL;
540
541
0
  if (G_LIKELY (result_wc != NULL))
542
0
    {
543
0
      result = g_ucs4_to_utf8 (result_wc, -1, NULL, NULL, NULL);
544
0
      g_free (result_wc);
545
0
    }
546
547
0
  return result;
548
0
}
549
550
static gboolean
551
decompose_hangul_step (gunichar  ch,
552
                       gunichar *a,
553
                       gunichar *b)
554
0
{
555
0
  gint SIndex, TIndex;
556
557
0
  if (ch < SBase || ch >= SBase + SCount)
558
0
    return FALSE;  /* not a hangul syllable */
559
560
0
  SIndex = ch - SBase;
561
0
  TIndex = SIndex % TCount;
562
563
0
  if (TIndex)
564
0
    {
565
      /* split LVT -> LV,T */
566
0
      *a = ch - TIndex;
567
0
      *b = TBase + TIndex;
568
0
    }
569
0
  else
570
0
    {
571
      /* split LV -> L,V */
572
0
      *a = LBase + SIndex / NCount;
573
0
      *b = VBase + (SIndex % NCount) / TCount;
574
0
    }
575
576
0
  return TRUE;
577
0
}
578
579
/**
580
 * g_unichar_decompose:
581
 * @ch: a Unicode character
582
 * @a: (out) (not optional): return location for the first component of @ch
583
 * @b: (out) (not optional): return location for the second component of @ch
584
 *
585
 * Performs a single decomposition step of the
586
 * Unicode canonical decomposition algorithm.
587
 *
588
 * This function does not include compatibility
589
 * decompositions. It does, however, include algorithmic
590
 * Hangul Jamo decomposition, as well as 'singleton'
591
 * decompositions which replace a character by a single
592
 * other character. In the case of singletons *@b will
593
 * be set to zero.
594
 *
595
 * If @ch is not decomposable, *@a is set to @ch and *@b
596
 * is set to zero.
597
 *
598
 * Note that the way Unicode decomposition pairs are
599
 * defined, it is guaranteed that @b would not decompose
600
 * further, but @a may itself decompose.  To get the full
601
 * canonical decomposition for @ch, one would need to
602
 * recursively call this function on @a.  Or use
603
 * g_unichar_fully_decompose().
604
 *
605
 * See
606
 * [UAX#15](http://unicode.org/reports/tr15/)
607
 * for details.
608
 *
609
 * Returns: %TRUE if the character could be decomposed
610
 *
611
 * Since: 2.30
612
 */
613
gboolean
614
g_unichar_decompose (gunichar  ch,
615
                     gunichar *a,
616
                     gunichar *b)
617
0
{
618
0
  gint start = 0;
619
0
  gint end = G_N_ELEMENTS (decomp_step_table);
620
621
0
  if (decompose_hangul_step (ch, a, b))
622
0
    return TRUE;
623
624
  /* TODO use bsearch() */
625
0
  if (ch >= decomp_step_table[start].ch &&
626
0
      ch <= decomp_step_table[end - 1].ch)
627
0
    {
628
0
      while (TRUE)
629
0
        {
630
0
          gint half = (start + end) / 2;
631
0
          const decomposition_step *p = &(decomp_step_table[half]);
632
0
          if (ch == p->ch)
633
0
            {
634
0
              *a = p->a;
635
0
              *b = p->b;
636
0
              return TRUE;
637
0
            }
638
0
          else if (half == start)
639
0
            break;
640
0
          else if (ch > p->ch)
641
0
            start = half;
642
0
          else
643
0
            end = half;
644
0
        }
645
0
    }
646
647
0
  *a = ch;
648
0
  *b = 0;
649
650
0
  return FALSE;
651
0
}
652
653
/**
654
 * g_unichar_compose:
655
 * @a: a Unicode character
656
 * @b: a Unicode character
657
 * @ch: (out) (not optional): return location for the composed character
658
 *
659
 * Performs a single composition step of the
660
 * Unicode canonical composition algorithm.
661
 *
662
 * This function includes algorithmic Hangul Jamo composition,
663
 * but it is not exactly the inverse of g_unichar_decompose().
664
 * No composition can have either of @a or @b equal to zero.
665
 * To be precise, this function composes if and only if
666
 * there exists a Primary Composite P which is canonically
667
 * equivalent to the sequence <@a,@b>.  See the Unicode
668
 * Standard for the definition of Primary Composite.
669
 *
670
 * If @a and @b do not compose a new character, @ch is set to zero.
671
 *
672
 * See
673
 * [UAX#15](http://unicode.org/reports/tr15/)
674
 * for details.
675
 *
676
 * Returns: %TRUE if the characters could be composed
677
 *
678
 * Since: 2.30
679
 */
680
gboolean
681
g_unichar_compose (gunichar  a,
682
                   gunichar  b,
683
                   gunichar *ch)
684
0
{
685
0
  if (combine (a, b, ch))
686
0
    return TRUE;
687
688
0
  *ch = 0;
689
0
  return FALSE;
690
0
}
691
692
/**
693
 * g_unichar_fully_decompose:
694
 * @ch: a Unicode character.
695
 * @compat: whether perform canonical or compatibility decomposition
696
 * @result: (optional) (out caller-allocates): location to store decomposed result, or %NULL
697
 * @result_len: length of @result
698
 *
699
 * Computes the canonical or compatibility decomposition of a
700
 * Unicode character.  For compatibility decomposition,
701
 * pass %TRUE for @compat; for canonical decomposition
702
 * pass %FALSE for @compat.
703
 *
704
 * The decomposed sequence is placed in @result.  Only up to
705
 * @result_len characters are written into @result.  The length
706
 * of the full decomposition (irrespective of @result_len) is
707
 * returned by the function.  For canonical decomposition,
708
 * currently all decompositions are of length at most 4, but
709
 * this may change in the future (very unlikely though).
710
 * At any rate, Unicode does guarantee that a buffer of length
711
 * 18 is always enough for both compatibility and canonical
712
 * decompositions, so that is the size recommended. This is provided
713
 * as %G_UNICHAR_MAX_DECOMPOSITION_LENGTH.
714
 *
715
 * See
716
 * [UAX#15](http://unicode.org/reports/tr15/)
717
 * for details.
718
 *
719
 * Returns: the length of the full decomposition.
720
 *
721
 * Since: 2.30
722
 **/
723
gsize
724
g_unichar_fully_decompose (gunichar  ch,
725
         gboolean  compat,
726
         gunichar *result,
727
         gsize     result_len)
728
0
{
729
0
  const gchar *decomp;
730
0
  const gchar *p;
731
732
  /* Hangul syllable */
733
0
  if (ch >= SBase && ch < SBase + SCount)
734
0
    {
735
0
      gsize len, i;
736
0
      gunichar buffer[3];
737
0
      decompose_hangul (ch, result ? buffer : NULL, &len);
738
0
      if (result)
739
0
        for (i = 0; i < len && i < result_len; i++)
740
0
    result[i] = buffer[i];
741
0
      return len;
742
0
    }
743
0
  else if ((decomp = find_decomposition (ch, compat)) != NULL)
744
0
    {
745
      /* Found it.  */
746
0
      gsize len, i;
747
748
0
      len = g_utf8_strlen (decomp, -1);
749
750
0
      for (p = decomp, i = 0; i < len && i < result_len; p = g_utf8_next_char (p), i++)
751
0
        result[i] = g_utf8_get_char (p);
752
753
0
      return len;
754
0
    }
755
756
  /* Does not decompose */
757
0
  if (result && result_len >= 1)
758
0
    *result = ch;
759
0
  return 1;
760
0
}