Debian Codegraph @

Unicode/uninames.c

raw symbols SCIP
1 /* Copyright (C) 2021 by Jeremy Tan */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "uninames_data.h"
29 #include <assert.h>
30
31 static char* read_lexicon(char* dst, unsigned int idx, int* bufsiz) {
32 idx = lexicon_offset[idx] + lexicon_shift[idx >> LEXICON_SHIFT];
33 do {
34 *dst++ = lexicon_data[idx] & 0x7f;
35 } while (--*bufsiz > 1 && !(lexicon_data[idx++] & 0x80));
36 assert(lexicon_data[idx-1] & 0x80);
37 return dst;
38 }
39
40 static const unsigned char* get_phrasebook_entry(unichar_t ch) {
41 int index = 0, shift_index;
42 if (ch < UNICODE_MAX) {
43 index = phrasebook_index1[ch >> PHRASEBOOK_SHIFT1];
44 index = phrasebook_index2[(index << PHRASEBOOK_SHIFT1) + (ch & ((1 << PHRASEBOOK_SHIFT1) - 1))];
45 }
46 if (index == 0) {
47 return NULL;
48 }
49 shift_index = ch >> PHRASEBOOK_SHIFT2;
50 if (shift_index > PHRASEBOOK_SHIFT2_CAP) {
51 shift_index = PHRASEBOOK_SHIFT2_CAP;
52 }
53 index += phrasebook_shift[shift_index];
54 assert(index >= 0 && (size_t)index < sizeof(phrasebook_data)/sizeof(phrasebook_data[0]));
55 return &phrasebook_data[index];
56 }
57
58 static char* prettify_annotation_start(char* dst, int src, int* bufsiz) {
59 switch(src) {
60 case '#': src = 0x2248; *bufsiz -= 3; break;
61 case '%': src = 0x203b; *bufsiz -= 3; break;
62 case '*': src = 0x2022; *bufsiz -= 3; break;
63 case ':': src = 0x2261; *bufsiz -= 3; break;
64 case 'x': src = 0x2192; *bufsiz -= 3; break;
65 case '~': src = 0x2053; *bufsiz -= 3; break;
66 default:
67 assert(false && "unknown annotation start char");
68 // fallthrough
69 case '=': *bufsiz -= 1;
70 }
71 return utf8_idpb(dst, src, 0);
72 }
73
74 char* uniname_name(unichar_t ch) {
75 const unsigned char *src;
76 static char ret[MAX_NAME_LENGTH];
77 int bufsiz = sizeof(ret)/sizeof(ret[0]);
78 char* dst = ret;
79
80 src = get_phrasebook_entry(ch);
81 if (src == NULL || *src == '\n' || *src == '\0') {
82 return get_derived_name(ch);
83 }
84
85 while (*src != '\n' && *src != '\0' && bufsiz > 4) {
86 switch (*src >> 4) {
87 case 8: case 9: case 10: case 11: // 0b10xx -> invalid utf-8; our escape
88 dst = read_lexicon(dst, ((src[0] & 0x3f) << 7) | (src[1] & 0x7f), &bufsiz);
89 src += 2;
90 break;
91 case 15: // 0b1111 -> 4 bytes
92 *dst++ = *src++;
93 --bufsiz;
94 // fallthrough
95 case 14: // 0b1110 -> 3 bytes
96 *dst++ = *src++;
97 --bufsiz;
98 // fallthrough
99 case 12: case 13: // 0b110x -> 2 bytes
100 *dst++ = *src++;
101 --bufsiz;
102 // fallthrough
103 default: // 0b0xxx -> 1 byte
104 *dst++ = *src++;
105 --bufsiz;
106 }
107 }
108
109 assert(bufsiz > 4);
110 if (bufsiz < 0) {
111 bufsiz = 0;
112 }
113 return copyn(ret, (sizeof(ret)/sizeof(ret[0])) - bufsiz);
114 }
115
116 char* uniname_annotation(unichar_t ch, int prettify) {
117 const unsigned char *src;
118 char ret[MAX_ANNOTATION_LENGTH];
119 int bufsiz = sizeof(ret)/sizeof(ret[0]);
120 char* dst = ret;
121
122 src = get_phrasebook_entry(ch);
123 if (src == NULL) {
124 return NULL;
125 }
126
127 src = (const unsigned char*)strchr((const char*)src, '\n');
128 if (src == NULL) {
129 return NULL;
130 }
131 ++src;
132
133 if (prettify) {
134 *dst++ = '\t';
135 dst = prettify_annotation_start(dst, *src++, &bufsiz);
136 *dst++ = ' ';
137 } else {
138 *dst++ = *src++;
139 *dst++ = ' ';
140 }
141 bufsiz -= 2;
142
143 while (*src != '\0' && bufsiz > 4) {
144 switch (*src >> 4) {
145 case 8: case 9: case 10: case 11: // 0b10xx -> invalid utf-8; our escape
146 dst = read_lexicon(dst, ((src[0] & 0x3f) << 7) | (src[1] & 0x7f), &bufsiz);
147 src += 2;
148 break;
149 case 15: // 0b1111 -> 4 bytes
150 *dst++ = *src++;
151 --bufsiz;
152 // fallthrough
153 case 14: // 0b1110 -> 3 bytes
154 *dst++ = *src++;
155 --bufsiz;
156 // fallthrough
157 case 12: case 13: // 0b110x -> 2 bytes
158 *dst++ = *src++;
159 --bufsiz;
160 // fallthrough
161 default: // 0b0xxx -> 1 byte
162 *dst = *src++;
163 --bufsiz;
164 if (*dst++ == '\n') {
165 if (prettify) {
166 *dst++ = '\t';
167 dst = prettify_annotation_start(dst, *src++, &bufsiz);
168 *dst++ = ' ';
169 } else {
170 *dst++ = *src++;
171 *dst++ = ' ';
172 }
173 bufsiz -= 2;
174 }
175 }
176 }
177
178 assert(bufsiz > 4);
179 if (bufsiz < 0) {
180 bufsiz = 0;
181 }
182 return copyn(ret, sizeof(ret)/sizeof(ret[0]) - bufsiz);
183 }
184
185 char* uniname_formal_alias(unichar_t ch) {
186 char *annot = uniname_annotation(ch, false), *ret = NULL;
187 if (annot) {
188 if (annot[0] == '%') {
189 char* pt = strchr(annot, '\n');
190 if (pt) {
191 *pt = '\0';
192 }
193 ret = copy(annot + 2);
194 }
195 free(annot);
196 }
197 return ret;
198 }
199
200 static const struct unicode_range* find_range(unichar_t ch, const struct unicode_range* data, size_t hi) {
201 size_t lo = 0, mid;
202
203 while (lo <= hi) {
204 mid = (lo+hi)/2;
205 if (ch >= data[mid].start && ch <= data[mid].end) {
206 return &data[mid];
207 } else if (data[mid].start < ch) {
208 lo = mid + 1;
209 } else {
210 hi = mid - 1;
211 }
212 }
213
214 return NULL;
215 }
216
217 const struct unicode_range* uniname_block(unichar_t ch) {
218 return find_range(ch, unicode_blocks, sizeof(unicode_blocks)/sizeof(unicode_blocks[0]));
219 }
220
221 const struct unicode_range* uniname_plane(unichar_t ch) {
222 return find_range(ch, unicode_planes, sizeof(unicode_planes)/sizeof(unicode_planes[0]));
223 }
224
225 const struct unicode_range* uniname_blocks(int *sz) {
226 if (sz) {
227 *sz = sizeof(unicode_blocks)/sizeof(unicode_blocks[0]);
228 }
229 return unicode_blocks;
230 }
231
232 const struct unicode_range* uniname_planes(int *sz) {
233 if (sz) {
234 *sz = sizeof(unicode_planes)/sizeof(unicode_planes[0]);
235 }
236 return unicode_planes;
237 }