Coverage Report

Created: 2026-05-23 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib/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
213k
  ((combining_class_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
36
213k
   ? (combining_class_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
37
213k
   : (cclass_data[combining_class_table_part1[Page]][Char]))
38
39
#define CC_PART2(Page, Char) \
40
794
  ((combining_class_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
41
794
   ? (combining_class_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
42
794
   : (cclass_data[combining_class_table_part2[Page]][Char]))
43
44
#define COMBINING_CLASS(Char) \
45
215k
  (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
46
215k
   ? CC_PART1 ((Char) >> 8, (Char) & 0xff) \
47
215k
   : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
48
1.77k
      ? CC_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
49
1.77k
      : 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
224k
#define SBase 0xAC00 
69
42.1k
#define LBase 0x1100 
70
42.1k
#define VBase 0x1161 
71
42.1k
#define TBase 0x11A7
72
48.3k
#define LCount 19 
73
4.51k
#define VCount 21
74
5.19k
#define TCount 28
75
4.23k
#define NCount (VCount * TCount)
76
45.8k
#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
38.3k
{
92
38.3k
  gsize i;
93
38.3k
  int swap = 1;
94
95
77.0k
  while (swap)
96
38.6k
    {
97
38.6k
      int last;
98
38.6k
      swap = 0;
99
38.6k
      last = COMBINING_CLASS (string[0]);
100
103k
      for (i = 0; i < len - 1; ++i)
101
64.8k
  {
102
64.8k
    int next = COMBINING_CLASS (string[i + 1]);
103
64.8k
    if (next != 0 && last > next)
104
5.51k
      {
105
5.51k
        gsize j;
106
        /* Percolate item leftward through string.  */
107
11.3k
        for (j = i + 1; j > 0; --j)
108
11.3k
    {
109
11.3k
      gunichar t;
110
11.3k
      if (COMBINING_CLASS (string[j - 1]) <= next)
111
5.51k
        break;
112
5.87k
      t = string[j];
113
5.87k
      string[j] = string[j - 1];
114
5.87k
      string[j - 1] = t;
115
5.87k
      swap = 1;
116
5.87k
    }
117
        /* We're re-entering the loop looking at the old
118
     character again.  */
119
5.51k
        next = last;
120
5.51k
      }
121
64.8k
    last = next;
122
64.8k
  }
123
38.6k
    }
124
38.3k
}
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
272
{
135
272
  gint SIndex = s - SBase;
136
272
  gint TIndex = SIndex % TCount;
137
138
272
  if (r)
139
136
    {
140
136
      r[0] = LBase + SIndex / NCount;
141
136
      r[1] = VBase + (SIndex % NCount) / TCount;
142
136
    }
143
144
272
  if (TIndex)
145
272
    {
146
272
      if (r)
147
136
  r[2] = TBase + TIndex;
148
272
      *result_len = 3;
149
272
    }
150
0
  else
151
0
    *result_len = 2;
152
272
}
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
89.4k
{
159
89.4k
  int start = 0;
160
89.4k
  int end = G_N_ELEMENTS (decomp_table);
161
  
162
89.4k
  if (ch >= decomp_table[start].ch &&
163
21.2k
      ch <= decomp_table[end - 1].ch)
164
20.6k
    {
165
243k
      while (TRUE)
166
243k
  {
167
243k
    int half = (start + end) / 2;
168
243k
    if (ch == decomp_table[half].ch)
169
18.7k
      {
170
18.7k
        int offset;
171
172
18.7k
        if (compat)
173
18.7k
    {
174
18.7k
      offset = decomp_table[half].compat_offset;
175
18.7k
      if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
176
18.3k
        offset = decomp_table[half].canon_offset;
177
18.7k
    }
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
18.7k
        return &(decomp_expansion_string[offset]);
186
18.7k
      }
187
224k
    else if (half == start)
188
1.92k
      break;
189
222k
    else if (ch > decomp_table[half].ch)
190
95.2k
      start = half;
191
127k
    else
192
127k
      end = half;
193
243k
  }
194
20.6k
    }
195
196
70.7k
  return NULL;
197
89.4k
}
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
42.0k
{
255
42.0k
  gint LIndex = a - LBase;
256
42.0k
  gint SIndex = a - SBase;
257
258
42.0k
  gint VIndex = b - VBase;
259
42.0k
  gint TIndex = b - TBase;
260
261
42.0k
  if (0 <= LIndex && LIndex < LCount
262
136
      && 0 <= VIndex && VIndex < VCount)
263
136
    {
264
136
      *result = SBase + (LIndex * VCount + VIndex) * TCount;
265
136
      return TRUE;
266
136
    }
267
41.8k
  else if (0 <= SIndex && SIndex < SCount && (SIndex % TCount) == 0
268
136
           && 0 < TIndex && TIndex < TCount)
269
136
    {
270
136
      *result = a + TIndex;
271
136
      return TRUE;
272
136
    }
273
274
41.7k
  return FALSE;
275
42.0k
}
276
277
#define CI(Page, Char) \
278
79.9k
  ((compose_table[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
279
79.9k
   ? (compose_table[Page] - G_UNICODE_MAX_TABLE_INDEX) \
280
79.9k
   : (compose_data[compose_table[Page]][Char]))
281
282
#define COMPOSE_INDEX(Char) \
283
80.5k
     (((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
42.0k
{
290
42.0k
  gushort index_a, index_b;
291
292
42.0k
  if (combine_hangul (a, b, result))
293
272
    return TRUE;
294
295
41.7k
  index_a = COMPOSE_INDEX(a);
296
297
41.7k
  if (index_a >= COMPOSE_FIRST_SINGLE_START && index_a < COMPOSE_SECOND_START)
298
2.98k
    {
299
2.98k
      if (b == compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][0])
300
2.59k
  {
301
2.59k
    *result = compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][1];
302
2.59k
    return TRUE;
303
2.59k
  }
304
393
      else
305
393
        return FALSE;
306
2.98k
    }
307
  
308
38.7k
  index_b = COMPOSE_INDEX(b);
309
310
38.7k
  if (index_b >= COMPOSE_SECOND_SINGLE_START && index_b < COMPOSE_EITHER_START)
311
163
    {
312
163
      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
163
      else
318
163
        return FALSE;
319
163
    }
320
321
38.6k
  if (index_a >= COMPOSE_FIRST_START && index_a < COMPOSE_FIRST_SINGLE_START &&
322
34.1k
      index_b >= COMPOSE_SECOND_START && index_b < COMPOSE_SECOND_SINGLE_START)
323
555
    {
324
555
      gunichar res = compose_array[index_a - COMPOSE_FIRST_START][index_b - COMPOSE_SECOND_START];
325
326
555
      if (res)
327
555
  {
328
555
    *result = res;
329
555
    return TRUE;
330
555
  }
331
555
    }
332
333
38.0k
  if (index_a >= COMPOSE_EITHER_START &&
334
375
      index_b >= COMPOSE_EITHER_START)
335
375
    {
336
375
      gunichar res = compose_either_array[index_a - COMPOSE_EITHER_START][index_b - COMPOSE_EITHER_START];
337
338
375
      if (res)
339
375
        {
340
375
          *result = res;
341
375
          return TRUE;
342
375
        }
343
375
    }
344
345
37.6k
  return FALSE;
346
38.0k
}
347
348
gunichar *
349
_g_utf8_normalize_wc (const gchar    *str,
350
          gssize          max_len,
351
          GNormalizeMode  mode)
352
1.29k
{
353
1.29k
  gsize n_wc;
354
1.29k
  gunichar *wc_buffer;
355
1.29k
  const char *p;
356
1.29k
  gsize last_start;
357
1.29k
  gboolean do_compat = (mode == G_NORMALIZE_NFKC ||
358
0
      mode == G_NORMALIZE_NFKD);
359
1.29k
  gboolean do_compose = (mode == G_NORMALIZE_NFC ||
360
1.29k
       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
1.29k
  n_wc = 0;
365
1.29k
  p = str;
366
46.1k
  while ((max_len < 0 || p < str + max_len) && *p)
367
44.8k
    {
368
44.8k
      const gchar *decomp;
369
44.8k
      const char *next, *between;
370
44.8k
      gunichar wc;
371
372
44.8k
      next = g_utf8_next_char (p);
373
      /* Avoid reading truncated multibyte characters
374
         which run past the end of the buffer */
375
44.8k
      if (max_len < 0)
376
44.8k
        {
377
          /* Does the character contain a NUL terminator? */
378
58.2k
          for (between = &p[1]; between < next; between++)
379
13.4k
            {
380
13.4k
              if (G_UNLIKELY (!*between))
381
0
                return NULL;
382
13.4k
            }
383
44.8k
        }
384
0
      else
385
0
        {
386
0
          if (G_UNLIKELY (next > str + max_len))
387
0
            return NULL;
388
0
        }
389
44.8k
      wc = g_utf8_get_char (p);
390
391
44.8k
      if (G_UNLIKELY (wc == (gunichar) -1))
392
0
        {
393
0
          return NULL;
394
0
        }
395
44.8k
      else if (wc >= SBase && wc < SBase + SCount)
396
136
        {
397
136
          gsize result_len;
398
136
          decompose_hangul (wc, NULL, &result_len);
399
136
          n_wc += result_len;
400
136
        }
401
44.7k
      else 
402
44.7k
        {
403
44.7k
          decomp = find_decomposition (wc, do_compat);
404
405
44.7k
          if (decomp)
406
9.35k
            n_wc += g_utf8_strlen (decomp, -1);
407
35.3k
          else
408
35.3k
            n_wc++;
409
44.7k
        }
410
411
44.8k
      p = next;
412
44.8k
    }
413
414
  /* Allocate the buffer for the result. */
415
1.29k
  wc_buffer = g_new (gunichar, n_wc + 1);
416
417
  /* Do another pass to fill the buffer with the normalised string. */
418
1.29k
  last_start = 0;
419
1.29k
  n_wc = 0;
420
1.29k
  p = str;
421
46.1k
  while ((max_len < 0 || p < str + max_len) && *p)
422
44.8k
    {
423
44.8k
      gunichar wc = g_utf8_get_char (p);
424
44.8k
      const gchar *decomp;
425
44.8k
      int cc;
426
44.8k
      gsize old_n_wc = n_wc;
427
    
428
44.8k
      if (wc >= SBase && wc < SBase + SCount)
429
136
        {
430
136
          gsize result_len;
431
136
          decompose_hangul (wc, wc_buffer + n_wc, &result_len);
432
136
          n_wc += result_len;
433
136
        }
434
44.7k
      else
435
44.7k
        {
436
44.7k
          decomp = find_decomposition (wc, do_compat);
437
          
438
44.7k
          if (decomp)
439
9.35k
            {
440
9.35k
              const char *pd;
441
28.7k
              for (pd = decomp; *pd != '\0'; pd = g_utf8_next_char (pd))
442
19.4k
                wc_buffer[n_wc++] = g_utf8_get_char (pd);
443
9.35k
            }
444
35.3k
          else
445
35.3k
            wc_buffer[n_wc++] = wc;
446
44.7k
        }
447
448
      /* Each code path above *must* have appended at least gunichar to wc_buffer. */
449
44.8k
      g_assert (n_wc > old_n_wc);
450
451
44.8k
      cc = COMBINING_CLASS (wc_buffer[old_n_wc]);
452
453
44.8k
      if (cc == 0)
454
37.6k
        {
455
37.6k
          g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
456
37.6k
          last_start = old_n_wc;
457
37.6k
        }
458
      
459
44.8k
      p = g_utf8_next_char (p);
460
44.8k
    }
461
462
1.29k
  if (n_wc > 0)
463
648
    {
464
648
      g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
465
648
      last_start = n_wc;
466
648
      (void) last_start;
467
648
    }
468
    
469
1.29k
  wc_buffer[n_wc] = 0;
470
471
  /* All decomposed and reordered */ 
472
473
1.29k
  if (do_compose && n_wc > 0)
474
648
    {
475
648
      gsize i, j;
476
648
      int last_cc = 0;
477
648
      last_start = 0;
478
      
479
55.8k
      for (i = 0; i < n_wc; i++)
480
55.2k
  {
481
55.2k
    int cc = COMBINING_CLASS (wc_buffer[i]);
482
483
55.2k
    if (i > 0 &&
484
54.5k
        (last_cc == 0 || last_cc < cc) &&
485
42.0k
        combine (wc_buffer[last_start], wc_buffer[i],
486
42.0k
           &wc_buffer[last_start]))
487
3.79k
      {
488
303k
        for (j = i + 1; j < n_wc; j++)
489
299k
    wc_buffer[j-1] = wc_buffer[j];
490
3.79k
        n_wc--;
491
3.79k
        i--;
492
        
493
3.79k
        if (i == last_start)
494
3.75k
    last_cc = 0;
495
39
        else
496
39
    last_cc = COMBINING_CLASS (wc_buffer[i-1]);
497
        
498
3.79k
        continue;
499
3.79k
      }
500
501
51.4k
    if (cc == 0)
502
37.9k
      last_start = i;
503
504
51.4k
    last_cc = cc;
505
51.4k
  }
506
648
    }
507
508
1.29k
  wc_buffer[n_wc] = 0;
509
510
1.29k
  return wc_buffer;
511
1.29k
}
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
}