Coverage Report

Created: 2026-06-08 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libunistring/lib/uninorm/canonical-decomposition.c
Line
Count
Source
1
/* Canonical decomposition of Unicode characters.
2
   Copyright (C) 2009-2026 Free Software Foundation, Inc.
3
   Written by Bruno Haible <bruno@clisp.org>, 2009.
4
5
   This file is free software: you can redistribute it and/or modify
6
   it under the terms of the GNU Lesser General Public License as
7
   published by the Free Software Foundation; either version 2.1 of the
8
   License, or (at your option) any later version.
9
10
   This file is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   GNU Lesser General Public License for more details.
14
15
   You should have received a copy of the GNU Lesser General Public License
16
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
17
18
#include <config.h>
19
20
/* Specification.  */
21
#include "uninorm.h"
22
23
#include <stdlib.h>
24
25
#include "uninorm/decomposition-table.h"
26
27
int
28
uc_canonical_decomposition (ucs4_t uc, ucs4_t *decomposition)
29
362k
{
30
362k
  if (uc >= 0xAC00 && uc < 0xD7A4)
31
3.11k
    {
32
      /* Hangul syllable.  See Unicode standard, chapter 3, section
33
         "Hangul Syllable Decomposition",  See also the clarification at
34
         <https://www.unicode.org/versions/Unicode5.1.0/>, section
35
         "Clarification of Hangul Jamo Handling".  */
36
3.11k
      uc -= 0xAC00;
37
3.11k
      unsigned int t = uc % 28;
38
39
3.11k
      if (t == 0)
40
1.63k
        {
41
1.63k
          uc = uc / 28;
42
1.63k
          unsigned int v = uc % 21;
43
1.63k
          unsigned int l = uc / 21;
44
45
1.63k
          decomposition[0] = 0x1100 + l;
46
1.63k
          decomposition[1] = 0x1161 + v;
47
1.63k
          return 2;
48
1.63k
        }
49
1.47k
      else
50
1.47k
        {
51
1.47k
#if 1 /* Return the pairwise decomposition, not the full decomposition.  */
52
1.47k
          decomposition[0] = 0xAC00 + uc - t; /* = 0xAC00 + (l * 21 + v) * 28; */
53
1.47k
          decomposition[1] = 0x11A7 + t;
54
1.47k
          return 2;
55
#else
56
          uc = uc / 28;
57
          unsigned int v = uc % 21;
58
          unsigned int l = uc / 21;
59
60
          decomposition[0] = 0x1100 + l;
61
          decomposition[1] = 0x1161 + v;
62
          decomposition[2] = 0x11A7 + t;
63
          return 3;
64
#endif
65
1.47k
        }
66
3.11k
    }
67
359k
  else if (uc < 0x110000)
68
359k
    {
69
359k
      unsigned short entry = decomp_index (uc);
70
      /* An entry of (unsigned short)(-1) denotes an absent entry.
71
         Otherwise, bit 15 of the entry tells whether the decomposition
72
         is a canonical one.  */
73
359k
      if (entry < 0x8000)
74
11.5k
        {
75
11.5k
          const unsigned char *p = &gl_uninorm_decomp_chars_table[3 * entry];
76
11.5k
          unsigned int element = (p[0] << 16) | (p[1] << 8) | p[2];
77
          /* The first element has 5 bits for the decomposition type.  */
78
11.5k
          if (((element >> 18) & 0x1f) != UC_DECOMP_CANONICAL)
79
0
            abort ();
80
11.5k
          unsigned int length = 1;
81
11.5k
          for (;;)
82
22.9k
            {
83
              /* Every element has an 18 bits wide Unicode code point.  */
84
22.9k
              *decomposition = element & 0x3ffff;
85
              /* Bit 23 tells whether there are more elements,  */
86
22.9k
              if ((element & (1 << 23)) == 0)
87
11.5k
                break;
88
11.3k
              p += 3;
89
11.3k
              element = (p[0] << 16) | (p[1] << 8) | p[2];
90
11.3k
              decomposition++;
91
11.3k
              length++;
92
11.3k
            }
93
11.5k
          return length;
94
11.5k
        }
95
359k
    }
96
347k
  return -1;
97
362k
}