Coverage Report

Created: 2026-01-06 07:18

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