Coverage Report

Created: 2026-02-14 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gstreamer/subprojects/glib-2.86.3/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 && index_b < COMPOSE_EITHER_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
  if (index_a >= COMPOSE_EITHER_START &&
334
0
      index_b >= COMPOSE_EITHER_START)
335
0
    {
336
0
      gunichar res = compose_either_array[index_a - COMPOSE_EITHER_START][index_b - COMPOSE_EITHER_START];
337
338
0
      if (res)
339
0
        {
340
0
          *result = res;
341
0
          return TRUE;
342
0
        }
343
0
    }
344
345
0
  return FALSE;
346
0
}
347
348
gunichar *
349
_g_utf8_normalize_wc (const gchar    *str,
350
          gssize          max_len,
351
          GNormalizeMode  mode)
352
0
{
353
0
  gsize n_wc;
354
0
  gunichar *wc_buffer;
355
0
  const char *p;
356
0
  gsize last_start;
357
0
  gboolean do_compat = (mode == G_NORMALIZE_NFKC ||
358
0
      mode == G_NORMALIZE_NFKD);
359
0
  gboolean do_compose = (mode == G_NORMALIZE_NFC ||
360
0
       mode == G_NORMALIZE_NFKC);
361
362
  /* Do a first pass to work out the length of the normalised string so we can
363
   * allocate a buffer. */
364
0
  n_wc = 0;
365
0
  p = str;
366
0
  while ((max_len < 0 || p < str + max_len) && *p)
367
0
    {
368
0
      const gchar *decomp;
369
0
      const char *next, *between;
370
0
      gunichar wc;
371
372
0
      next = g_utf8_next_char (p);
373
      /* Avoid reading truncated multibyte characters
374
         which run past the end of the buffer */
375
0
      if (max_len < 0)
376
0
        {
377
          /* Does the character contain a NUL terminator? */
378
0
          for (between = &p[1]; between < next; between++)
379
0
            {
380
0
              if (G_UNLIKELY (!*between))
381
0
                return NULL;
382
0
            }
383
0
        }
384
0
      else
385
0
        {
386
0
          if (G_UNLIKELY (next > str + max_len))
387
0
            return NULL;
388
0
        }
389
0
      wc = g_utf8_get_char (p);
390
391
0
      if (G_UNLIKELY (wc == (gunichar) -1))
392
0
        {
393
0
          return NULL;
394
0
        }
395
0
      else if (wc >= SBase && wc < SBase + SCount)
396
0
        {
397
0
          gsize result_len;
398
0
          decompose_hangul (wc, NULL, &result_len);
399
0
          n_wc += result_len;
400
0
        }
401
0
      else 
402
0
        {
403
0
          decomp = find_decomposition (wc, do_compat);
404
405
0
          if (decomp)
406
0
            n_wc += g_utf8_strlen (decomp, -1);
407
0
          else
408
0
            n_wc++;
409
0
        }
410
411
0
      p = next;
412
0
    }
413
414
  /* Allocate the buffer for the result. */
415
0
  wc_buffer = g_new (gunichar, n_wc + 1);
416
417
  /* Do another pass to fill the buffer with the normalised string. */
418
0
  last_start = 0;
419
0
  n_wc = 0;
420
0
  p = str;
421
0
  while ((max_len < 0 || p < str + max_len) && *p)
422
0
    {
423
0
      gunichar wc = g_utf8_get_char (p);
424
0
      const gchar *decomp;
425
0
      int cc;
426
0
      gsize old_n_wc = n_wc;
427
    
428
0
      if (wc >= SBase && wc < SBase + SCount)
429
0
        {
430
0
          gsize result_len;
431
0
          decompose_hangul (wc, wc_buffer + n_wc, &result_len);
432
0
          n_wc += result_len;
433
0
        }
434
0
      else
435
0
        {
436
0
          decomp = find_decomposition (wc, do_compat);
437
          
438
0
          if (decomp)
439
0
            {
440
0
              const char *pd;
441
0
              for (pd = decomp; *pd != '\0'; pd = g_utf8_next_char (pd))
442
0
                wc_buffer[n_wc++] = g_utf8_get_char (pd);
443
0
            }
444
0
          else
445
0
            wc_buffer[n_wc++] = wc;
446
0
        }
447
448
      /* Each code path above *must* have appended at least gunichar to wc_buffer. */
449
0
      g_assert (n_wc > old_n_wc);
450
451
0
      cc = COMBINING_CLASS (wc_buffer[old_n_wc]);
452
453
0
      if (cc == 0)
454
0
        {
455
0
          g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
456
0
          last_start = old_n_wc;
457
0
        }
458
      
459
0
      p = g_utf8_next_char (p);
460
0
    }
461
462
0
  if (n_wc > 0)
463
0
    {
464
0
      g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
465
0
      last_start = n_wc;
466
0
      (void) last_start;
467
0
    }
468
    
469
0
  wc_buffer[n_wc] = 0;
470
471
  /* All decomposed and reordered */ 
472
473
0
  if (do_compose && n_wc > 0)
474
0
    {
475
0
      gsize i, j;
476
0
      int last_cc = 0;
477
0
      last_start = 0;
478
      
479
0
      for (i = 0; i < n_wc; i++)
480
0
  {
481
0
    int cc = COMBINING_CLASS (wc_buffer[i]);
482
483
0
    if (i > 0 &&
484
0
        (last_cc == 0 || last_cc < cc) &&
485
0
        combine (wc_buffer[last_start], wc_buffer[i],
486
0
           &wc_buffer[last_start]))
487
0
      {
488
0
        for (j = i + 1; j < n_wc; j++)
489
0
    wc_buffer[j-1] = wc_buffer[j];
490
0
        n_wc--;
491
0
        i--;
492
        
493
0
        if (i == last_start)
494
0
    last_cc = 0;
495
0
        else
496
0
    last_cc = COMBINING_CLASS (wc_buffer[i-1]);
497
        
498
0
        continue;
499
0
      }
500
501
0
    if (cc == 0)
502
0
      last_start = i;
503
504
0
    last_cc = cc;
505
0
  }
506
0
    }
507
508
0
  wc_buffer[n_wc] = 0;
509
510
0
  return wc_buffer;
511
0
}
512
513
/**
514
 * g_utf8_normalize:
515
 * @str: a UTF-8 encoded string.
516
 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
517
 * @mode: the type of normalization to perform.
518
 *
519
 * Converts a string into canonical form, standardizing
520
 * such issues as whether a character with an accent
521
 * is represented as a base character and combining
522
 * accent or as a single precomposed character. The
523
 * string has to be valid UTF-8, otherwise %NULL is
524
 * returned. You should generally call g_utf8_normalize()
525
 * before comparing two Unicode strings.
526
 *
527
 * The normalization mode %G_NORMALIZE_DEFAULT only
528
 * standardizes differences that do not affect the
529
 * text content, such as the above-mentioned accent
530
 * representation. %G_NORMALIZE_ALL also standardizes
531
 * the "compatibility" characters in Unicode, such
532
 * as SUPERSCRIPT THREE to the standard forms
533
 * (in this case DIGIT THREE). Formatting information
534
 * may be lost but for most text operations such
535
 * characters should be considered the same.
536
 *
537
 * %G_NORMALIZE_DEFAULT_COMPOSE and %G_NORMALIZE_ALL_COMPOSE
538
 * are like %G_NORMALIZE_DEFAULT and %G_NORMALIZE_ALL,
539
 * but returned a result with composed forms rather
540
 * than a maximally decomposed form. This is often
541
 * useful if you intend to convert the string to
542
 * a legacy encoding or pass it to a system with
543
 * less capable Unicode handling.
544
 *
545
 * Returns: (nullable): a newly allocated string, that
546
 *   is the normalized form of @str, or %NULL if @str
547
 *   is not valid UTF-8.
548
 **/
549
gchar *
550
g_utf8_normalize (const gchar    *str,
551
      gssize          len,
552
      GNormalizeMode  mode)
553
0
{
554
0
  gunichar *result_wc = _g_utf8_normalize_wc (str, len, mode);
555
0
  gchar *result = NULL;
556
557
0
  if (G_LIKELY (result_wc != NULL))
558
0
    {
559
0
      result = g_ucs4_to_utf8 (result_wc, -1, NULL, NULL, NULL);
560
0
      g_free (result_wc);
561
0
    }
562
563
0
  return result;
564
0
}
565
566
static gboolean
567
decompose_hangul_step (gunichar  ch,
568
                       gunichar *a,
569
                       gunichar *b)
570
0
{
571
0
  gint SIndex, TIndex;
572
573
0
  if (ch < SBase || ch >= SBase + SCount)
574
0
    return FALSE;  /* not a hangul syllable */
575
576
0
  SIndex = ch - SBase;
577
0
  TIndex = SIndex % TCount;
578
579
0
  if (TIndex)
580
0
    {
581
      /* split LVT -> LV,T */
582
0
      *a = ch - TIndex;
583
0
      *b = TBase + TIndex;
584
0
    }
585
0
  else
586
0
    {
587
      /* split LV -> L,V */
588
0
      *a = LBase + SIndex / NCount;
589
0
      *b = VBase + (SIndex % NCount) / TCount;
590
0
    }
591
592
0
  return TRUE;
593
0
}
594
595
/**
596
 * g_unichar_decompose:
597
 * @ch: a Unicode character
598
 * @a: (out) (not optional): return location for the first component of @ch
599
 * @b: (out) (not optional): return location for the second component of @ch
600
 *
601
 * Performs a single decomposition step of the
602
 * Unicode canonical decomposition algorithm.
603
 *
604
 * This function does not include compatibility
605
 * decompositions. It does, however, include algorithmic
606
 * Hangul Jamo decomposition, as well as 'singleton'
607
 * decompositions which replace a character by a single
608
 * other character. In the case of singletons `*b` will
609
 * be set to zero.
610
 *
611
 * If @ch is not decomposable, `*a` is set to @ch and `*b`
612
 * is set to zero.
613
 *
614
 * Note that the way Unicode decomposition pairs are
615
 * defined, it is guaranteed that @b would not decompose
616
 * further, but @a may itself decompose.  To get the full
617
 * canonical decomposition for @ch, one would need to
618
 * recursively call this function on @a.  Or use
619
 * g_unichar_fully_decompose().
620
 *
621
 * See
622
 * [UAX#15](http://unicode.org/reports/tr15/)
623
 * for details.
624
 *
625
 * Returns: %TRUE if the character could be decomposed
626
 *
627
 * Since: 2.30
628
 */
629
gboolean
630
g_unichar_decompose (gunichar  ch,
631
                     gunichar *a,
632
                     gunichar *b)
633
0
{
634
0
  gint start = 0;
635
0
  gint end = G_N_ELEMENTS (decomp_step_table);
636
637
0
  if (decompose_hangul_step (ch, a, b))
638
0
    return TRUE;
639
640
  /* TODO use bsearch() */
641
0
  if (ch >= decomp_step_table[start].ch &&
642
0
      ch <= decomp_step_table[end - 1].ch)
643
0
    {
644
0
      while (TRUE)
645
0
        {
646
0
          gint half = (start + end) / 2;
647
0
          const decomposition_step *p = &(decomp_step_table[half]);
648
0
          if (ch == p->ch)
649
0
            {
650
0
              *a = p->a;
651
0
              *b = p->b;
652
0
              return TRUE;
653
0
            }
654
0
          else if (half == start)
655
0
            break;
656
0
          else if (ch > p->ch)
657
0
            start = half;
658
0
          else
659
0
            end = half;
660
0
        }
661
0
    }
662
663
0
  *a = ch;
664
0
  *b = 0;
665
666
0
  return FALSE;
667
0
}
668
669
/**
670
 * g_unichar_compose:
671
 * @a: a Unicode character
672
 * @b: a Unicode character
673
 * @ch: (out) (not optional): return location for the composed character
674
 *
675
 * Performs a single composition step of the
676
 * Unicode canonical composition algorithm.
677
 *
678
 * This function includes algorithmic Hangul Jamo composition,
679
 * but it is not exactly the inverse of g_unichar_decompose().
680
 * No composition can have either of @a or @b equal to zero.
681
 * To be precise, this function composes if and only if
682
 * there exists a Primary Composite P which is canonically
683
 * equivalent to the sequence <@a,@b>.  See the Unicode
684
 * Standard for the definition of Primary Composite.
685
 *
686
 * If @a and @b do not compose a new character, @ch is set to zero.
687
 *
688
 * See
689
 * [UAX#15](http://unicode.org/reports/tr15/)
690
 * for details.
691
 *
692
 * Returns: %TRUE if the characters could be composed
693
 *
694
 * Since: 2.30
695
 */
696
gboolean
697
g_unichar_compose (gunichar  a,
698
                   gunichar  b,
699
                   gunichar *ch)
700
0
{
701
0
  if (combine (a, b, ch))
702
0
    return TRUE;
703
704
0
  *ch = 0;
705
0
  return FALSE;
706
0
}
707
708
/**
709
 * g_unichar_fully_decompose:
710
 * @ch: a Unicode character.
711
 * @compat: whether perform canonical or compatibility decomposition
712
 * @result: (optional) (out caller-allocates): location to store decomposed result, or %NULL
713
 * @result_len: length of @result
714
 *
715
 * Computes the canonical or compatibility decomposition of a
716
 * Unicode character.  For compatibility decomposition,
717
 * pass %TRUE for @compat; for canonical decomposition
718
 * pass %FALSE for @compat.
719
 *
720
 * The decomposed sequence is placed in @result.  Only up to
721
 * @result_len characters are written into @result.  The length
722
 * of the full decomposition (irrespective of @result_len) is
723
 * returned by the function.  For canonical decomposition,
724
 * currently all decompositions are of length at most 4, but
725
 * this may change in the future (very unlikely though).
726
 * At any rate, Unicode does guarantee that a buffer of length
727
 * 18 is always enough for both compatibility and canonical
728
 * decompositions, so that is the size recommended. This is provided
729
 * as %G_UNICHAR_MAX_DECOMPOSITION_LENGTH.
730
 *
731
 * See
732
 * [UAX#15](http://unicode.org/reports/tr15/)
733
 * for details.
734
 *
735
 * Returns: the length of the full decomposition.
736
 *
737
 * Since: 2.30
738
 **/
739
gsize
740
g_unichar_fully_decompose (gunichar  ch,
741
         gboolean  compat,
742
         gunichar *result,
743
         gsize     result_len)
744
0
{
745
0
  const gchar *decomp;
746
0
  const gchar *p;
747
748
  /* Hangul syllable */
749
0
  if (ch >= SBase && ch < SBase + SCount)
750
0
    {
751
0
      gsize len, i;
752
0
      gunichar buffer[3];
753
0
      decompose_hangul (ch, result ? buffer : NULL, &len);
754
0
      if (result)
755
0
        for (i = 0; i < len && i < result_len; i++)
756
0
    result[i] = buffer[i];
757
0
      return len;
758
0
    }
759
0
  else if ((decomp = find_decomposition (ch, compat)) != NULL)
760
0
    {
761
      /* Found it.  */
762
0
      gsize len, i;
763
764
0
      len = g_utf8_strlen (decomp, -1);
765
766
0
      for (p = decomp, i = 0; i < len && i < result_len; p = g_utf8_next_char (p), i++)
767
0
        result[i] = g_utf8_get_char (p);
768
769
0
      return len;
770
0
    }
771
772
  /* Does not decompose */
773
0
  if (result && result_len >= 1)
774
0
    *result = ch;
775
0
  return 1;
776
0
}