Coverage Report

Created: 2026-02-05 06:23

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
7.26M
{
30
7.26M
  if (uc >= 0xAC00 && uc < 0xD7A4)
31
11.6k
    {
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
11.6k
      uc -= 0xAC00;
37
11.6k
      unsigned int t = uc % 28;
38
39
11.6k
      if (t == 0)
40
7.27k
        {
41
7.27k
          uc = uc / 28;
42
7.27k
          unsigned int v = uc % 21;
43
7.27k
          unsigned int l = uc / 21;
44
45
7.27k
          decomposition[0] = 0x1100 + l;
46
7.27k
          decomposition[1] = 0x1161 + v;
47
7.27k
          return 2;
48
7.27k
        }
49
4.39k
      else
50
4.39k
        {
51
4.39k
#if 1 /* Return the pairwise decomposition, not the full decomposition.  */
52
4.39k
          decomposition[0] = 0xAC00 + uc - t; /* = 0xAC00 + (l * 21 + v) * 28; */
53
4.39k
          decomposition[1] = 0x11A7 + t;
54
4.39k
          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
4.39k
        }
66
11.6k
    }
67
7.25M
  else if (uc < 0x110000)
68
7.25M
    {
69
7.25M
      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
7.25M
      if (entry < 0x8000)
74
21.8k
        {
75
21.8k
          const unsigned char *p = &gl_uninorm_decomp_chars_table[3 * entry];
76
21.8k
          unsigned int element = (p[0] << 16) | (p[1] << 8) | p[2];
77
          /* The first element has 5 bits for the decomposition type.  */
78
21.8k
          if (((element >> 18) & 0x1f) != UC_DECOMP_CANONICAL)
79
0
            abort ();
80
21.8k
          unsigned int length = 1;
81
21.8k
          for (;;)
82
43.4k
            {
83
              /* Every element has an 18 bits wide Unicode code point.  */
84
43.4k
              *decomposition = element & 0x3ffff;
85
              /* Bit 23 tells whether there are more elements,  */
86
43.4k
              if ((element & (1 << 23)) == 0)
87
21.8k
                break;
88
21.6k
              p += 3;
89
21.6k
              element = (p[0] << 16) | (p[1] << 8) | p[2];
90
21.6k
              decomposition++;
91
21.6k
              length++;
92
21.6k
            }
93
21.8k
          return length;
94
21.8k
        }
95
7.25M
    }
96
7.23M
  return -1;
97
7.26M
}