Avoid build errors due to wrong references between modules.
[gnulib.git] / lib / uninorm / decomposition.c
bloba0e6cfc738b6e4b8851c5041729b7f8acba03c22
1 /* Decomposition of Unicode characters.
2 Copyright (C) 2009-2019 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2009.
5 This program is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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 GNU
13 Lesser General Public License for more details.
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/>. */
18 #include <config.h>
20 /* Specification. */
21 #include "uninorm.h"
23 #include "uninorm/decomposition-table.h"
25 int
26 uc_decomposition (ucs4_t uc, int *decomp_tag, ucs4_t *decomposition)
28 if (uc >= 0xAC00 && uc < 0xD7A4)
30 /* Hangul syllable. See Unicode standard, chapter 3, section
31 "Hangul Syllable Decomposition", See also the clarification at
32 <http://www.unicode.org/versions/Unicode5.1.0/>, section
33 "Clarification of Hangul Jamo Handling". */
34 unsigned int t;
36 uc -= 0xAC00;
37 t = uc % 28;
39 *decomp_tag = UC_DECOMP_CANONICAL;
40 if (t == 0)
42 unsigned int v, l;
44 uc = uc / 28;
45 v = uc % 21;
46 l = uc / 21;
48 decomposition[0] = 0x1100 + l;
49 decomposition[1] = 0x1161 + v;
50 return 2;
52 else
54 #if 1 /* Return the pairwise decomposition, not the full decomposition. */
55 decomposition[0] = 0xAC00 + uc - t; /* = 0xAC00 + (l * 21 + v) * 28; */
56 decomposition[1] = 0x11A7 + t;
57 return 2;
58 #else
59 unsigned int v, l;
61 uc = uc / 28;
62 v = uc % 21;
63 l = uc / 21;
65 decomposition[0] = 0x1100 + l;
66 decomposition[1] = 0x1161 + v;
67 decomposition[2] = 0x11A7 + t;
68 return 3;
69 #endif
72 else if (uc < 0x110000)
74 unsigned short entry = decomp_index (uc);
75 if (entry != (unsigned short)(-1))
77 const unsigned char *p;
78 unsigned int element;
79 unsigned int length;
81 p = &gl_uninorm_decomp_chars_table[3 * (entry & 0x7FFF)];
82 element = (p[0] << 16) | (p[1] << 8) | p[2];
83 /* The first element has 5 bits for the decomposition type. */
84 *decomp_tag = (element >> 18) & 0x1f;
85 length = 1;
86 for (;;)
88 /* Every element has an 18 bits wide Unicode code point. */
89 *decomposition = element & 0x3ffff;
90 /* Bit 23 tells whether there are more elements, */
91 if ((element & (1 << 23)) == 0)
92 break;
93 p += 3;
94 element = (p[0] << 16) | (p[1] << 8) | p[2];
95 decomposition++;
96 length++;
98 return length;
101 return -1;