Debian Codegraph @

lib/unistring/uninorm/canonical-decomposition.c

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