1 /*-
2  * Copyright © 2013, 2014
3  *	Thorsten Glaser <tg@mirbsd.org>
4  *
5  * Provided that these terms and disclaimer and all copyright notices
6  * are retained or reproduced in an accompanying document, permission
7  * is granted to deal in this work without restriction, including un‐
8  * limited rights to use, publicly perform, distribute, sell, modify,
9  * merge, give away, or sublicence.
10  *
11  * This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
12  * the utmost extent permitted by applicable law, neither express nor
13  * implied; without malicious intent or gross negligence. In no event
14  * may a licensor, author or contributor be held liable for indirect,
15  * direct, other damage, loss, or other issues arising in any way out
16  * of dealing in the work, even if advised of the possibility of such
17  * damage or existence of a defect, except proven that it results out
18  * of said person’s immediate fault when using the work as intended.
19  */
20 
21 #ifndef __STDC_ISO_10646__
22 #error This code assumes that wchar_t is UCS
23 #endif
24 
25 #include <wchar.h>
26 
27 __RCSID("$MirOS: src/lib/libc/i18n/wcwidth.c,v 1.14 2014/06/24 19:53:26 tg Exp $");
28 
29 /* From: X11/xc/programs/xterm/wcwidth.c,v 1.8 2014/06/24 19:53:53 tg Exp $ */
30 
31 struct mb_ucsrange {
32 	wchar_t beg;
33 	wchar_t end;
34 };
35 
36 static int mb_ucsbsearch(const struct mb_ucsrange arr[], size_t elems,
37     wchar_t val);
38 
39 /*
40  * Generated by MirOS: contrib/code/Snippets/eawparse,v 1.2 2013/11/30 13:45:17 tg Exp $
41  * from the Unicode Character Database, Version 7.0.0
42  */
43 
44 static const struct mb_ucsrange mb_ucs_combining[] = {
45 	{ 0x0300, 0x036F },
46 	{ 0x0483, 0x0489 },
47 	{ 0x0591, 0x05BD },
48 	{ 0x05BF, 0x05BF },
49 	{ 0x05C1, 0x05C2 },
50 	{ 0x05C4, 0x05C5 },
51 	{ 0x05C7, 0x05C7 },
52 	{ 0x0600, 0x0605 },
53 	{ 0x0610, 0x061A },
54 	{ 0x061C, 0x061C },
55 	{ 0x064B, 0x065F },
56 	{ 0x0670, 0x0670 },
57 	{ 0x06D6, 0x06DD },
58 	{ 0x06DF, 0x06E4 },
59 	{ 0x06E7, 0x06E8 },
60 	{ 0x06EA, 0x06ED },
61 	{ 0x070F, 0x070F },
62 	{ 0x0711, 0x0711 },
63 	{ 0x0730, 0x074A },
64 	{ 0x07A6, 0x07B0 },
65 	{ 0x07EB, 0x07F3 },
66 	{ 0x0816, 0x0819 },
67 	{ 0x081B, 0x0823 },
68 	{ 0x0825, 0x0827 },
69 	{ 0x0829, 0x082D },
70 	{ 0x0859, 0x085B },
71 	{ 0x08E4, 0x0902 },
72 	{ 0x093A, 0x093A },
73 	{ 0x093C, 0x093C },
74 	{ 0x0941, 0x0948 },
75 	{ 0x094D, 0x094D },
76 	{ 0x0951, 0x0957 },
77 	{ 0x0962, 0x0963 },
78 	{ 0x0981, 0x0981 },
79 	{ 0x09BC, 0x09BC },
80 	{ 0x09C1, 0x09C4 },
81 	{ 0x09CD, 0x09CD },
82 	{ 0x09E2, 0x09E3 },
83 	{ 0x0A01, 0x0A02 },
84 	{ 0x0A3C, 0x0A3C },
85 	{ 0x0A41, 0x0A42 },
86 	{ 0x0A47, 0x0A48 },
87 	{ 0x0A4B, 0x0A4D },
88 	{ 0x0A51, 0x0A51 },
89 	{ 0x0A70, 0x0A71 },
90 	{ 0x0A75, 0x0A75 },
91 	{ 0x0A81, 0x0A82 },
92 	{ 0x0ABC, 0x0ABC },
93 	{ 0x0AC1, 0x0AC5 },
94 	{ 0x0AC7, 0x0AC8 },
95 	{ 0x0ACD, 0x0ACD },
96 	{ 0x0AE2, 0x0AE3 },
97 	{ 0x0B01, 0x0B01 },
98 	{ 0x0B3C, 0x0B3C },
99 	{ 0x0B3F, 0x0B3F },
100 	{ 0x0B41, 0x0B44 },
101 	{ 0x0B4D, 0x0B4D },
102 	{ 0x0B56, 0x0B56 },
103 	{ 0x0B62, 0x0B63 },
104 	{ 0x0B82, 0x0B82 },
105 	{ 0x0BC0, 0x0BC0 },
106 	{ 0x0BCD, 0x0BCD },
107 	{ 0x0C00, 0x0C00 },
108 	{ 0x0C3E, 0x0C40 },
109 	{ 0x0C46, 0x0C48 },
110 	{ 0x0C4A, 0x0C4D },
111 	{ 0x0C55, 0x0C56 },
112 	{ 0x0C62, 0x0C63 },
113 	{ 0x0C81, 0x0C81 },
114 	{ 0x0CBC, 0x0CBC },
115 	{ 0x0CBF, 0x0CBF },
116 	{ 0x0CC6, 0x0CC6 },
117 	{ 0x0CCC, 0x0CCD },
118 	{ 0x0CE2, 0x0CE3 },
119 	{ 0x0D01, 0x0D01 },
120 	{ 0x0D41, 0x0D44 },
121 	{ 0x0D4D, 0x0D4D },
122 	{ 0x0D62, 0x0D63 },
123 	{ 0x0DCA, 0x0DCA },
124 	{ 0x0DD2, 0x0DD4 },
125 	{ 0x0DD6, 0x0DD6 },
126 	{ 0x0E31, 0x0E31 },
127 	{ 0x0E34, 0x0E3A },
128 	{ 0x0E47, 0x0E4E },
129 	{ 0x0EB1, 0x0EB1 },
130 	{ 0x0EB4, 0x0EB9 },
131 	{ 0x0EBB, 0x0EBC },
132 	{ 0x0EC8, 0x0ECD },
133 	{ 0x0F18, 0x0F19 },
134 	{ 0x0F35, 0x0F35 },
135 	{ 0x0F37, 0x0F37 },
136 	{ 0x0F39, 0x0F39 },
137 	{ 0x0F71, 0x0F7E },
138 	{ 0x0F80, 0x0F84 },
139 	{ 0x0F86, 0x0F87 },
140 	{ 0x0F8D, 0x0F97 },
141 	{ 0x0F99, 0x0FBC },
142 	{ 0x0FC6, 0x0FC6 },
143 	{ 0x102D, 0x1030 },
144 	{ 0x1032, 0x1037 },
145 	{ 0x1039, 0x103A },
146 	{ 0x103D, 0x103E },
147 	{ 0x1058, 0x1059 },
148 	{ 0x105E, 0x1060 },
149 	{ 0x1071, 0x1074 },
150 	{ 0x1082, 0x1082 },
151 	{ 0x1085, 0x1086 },
152 	{ 0x108D, 0x108D },
153 	{ 0x109D, 0x109D },
154 	{ 0x1160, 0x11FF },
155 	{ 0x135D, 0x135F },
156 	{ 0x1712, 0x1714 },
157 	{ 0x1732, 0x1734 },
158 	{ 0x1752, 0x1753 },
159 	{ 0x1772, 0x1773 },
160 	{ 0x17B4, 0x17B5 },
161 	{ 0x17B7, 0x17BD },
162 	{ 0x17C6, 0x17C6 },
163 	{ 0x17C9, 0x17D3 },
164 	{ 0x17DD, 0x17DD },
165 	{ 0x180B, 0x180E },
166 	{ 0x18A9, 0x18A9 },
167 	{ 0x1920, 0x1922 },
168 	{ 0x1927, 0x1928 },
169 	{ 0x1932, 0x1932 },
170 	{ 0x1939, 0x193B },
171 	{ 0x1A17, 0x1A18 },
172 	{ 0x1A1B, 0x1A1B },
173 	{ 0x1A56, 0x1A56 },
174 	{ 0x1A58, 0x1A5E },
175 	{ 0x1A60, 0x1A60 },
176 	{ 0x1A62, 0x1A62 },
177 	{ 0x1A65, 0x1A6C },
178 	{ 0x1A73, 0x1A7C },
179 	{ 0x1A7F, 0x1A7F },
180 	{ 0x1AB0, 0x1ABE },
181 	{ 0x1B00, 0x1B03 },
182 	{ 0x1B34, 0x1B34 },
183 	{ 0x1B36, 0x1B3A },
184 	{ 0x1B3C, 0x1B3C },
185 	{ 0x1B42, 0x1B42 },
186 	{ 0x1B6B, 0x1B73 },
187 	{ 0x1B80, 0x1B81 },
188 	{ 0x1BA2, 0x1BA5 },
189 	{ 0x1BA8, 0x1BA9 },
190 	{ 0x1BAB, 0x1BAD },
191 	{ 0x1BE6, 0x1BE6 },
192 	{ 0x1BE8, 0x1BE9 },
193 	{ 0x1BED, 0x1BED },
194 	{ 0x1BEF, 0x1BF1 },
195 	{ 0x1C2C, 0x1C33 },
196 	{ 0x1C36, 0x1C37 },
197 	{ 0x1CD0, 0x1CD2 },
198 	{ 0x1CD4, 0x1CE0 },
199 	{ 0x1CE2, 0x1CE8 },
200 	{ 0x1CED, 0x1CED },
201 	{ 0x1CF4, 0x1CF4 },
202 	{ 0x1CF8, 0x1CF9 },
203 	{ 0x1DC0, 0x1DF5 },
204 	{ 0x1DFC, 0x1DFF },
205 	{ 0x200B, 0x200F },
206 	{ 0x202A, 0x202E },
207 	{ 0x2060, 0x2064 },
208 	{ 0x2066, 0x206F },
209 	{ 0x20D0, 0x20F0 },
210 	{ 0x2CEF, 0x2CF1 },
211 	{ 0x2D7F, 0x2D7F },
212 	{ 0x2DE0, 0x2DFF },
213 	{ 0x302A, 0x302D },
214 	{ 0x3099, 0x309A },
215 	{ 0xA66F, 0xA672 },
216 	{ 0xA674, 0xA67D },
217 	{ 0xA69F, 0xA69F },
218 	{ 0xA6F0, 0xA6F1 },
219 	{ 0xA802, 0xA802 },
220 	{ 0xA806, 0xA806 },
221 	{ 0xA80B, 0xA80B },
222 	{ 0xA825, 0xA826 },
223 	{ 0xA8C4, 0xA8C4 },
224 	{ 0xA8E0, 0xA8F1 },
225 	{ 0xA926, 0xA92D },
226 	{ 0xA947, 0xA951 },
227 	{ 0xA980, 0xA982 },
228 	{ 0xA9B3, 0xA9B3 },
229 	{ 0xA9B6, 0xA9B9 },
230 	{ 0xA9BC, 0xA9BC },
231 	{ 0xA9E5, 0xA9E5 },
232 	{ 0xAA29, 0xAA2E },
233 	{ 0xAA31, 0xAA32 },
234 	{ 0xAA35, 0xAA36 },
235 	{ 0xAA43, 0xAA43 },
236 	{ 0xAA4C, 0xAA4C },
237 	{ 0xAA7C, 0xAA7C },
238 	{ 0xAAB0, 0xAAB0 },
239 	{ 0xAAB2, 0xAAB4 },
240 	{ 0xAAB7, 0xAAB8 },
241 	{ 0xAABE, 0xAABF },
242 	{ 0xAAC1, 0xAAC1 },
243 	{ 0xAAEC, 0xAAED },
244 	{ 0xAAF6, 0xAAF6 },
245 	{ 0xABE5, 0xABE5 },
246 	{ 0xABE8, 0xABE8 },
247 	{ 0xABED, 0xABED },
248 	{ 0xFB1E, 0xFB1E },
249 	{ 0xFE00, 0xFE0F },
250 	{ 0xFE20, 0xFE2D },
251 	{ 0xFEFF, 0xFEFF },
252 	{ 0xFFF9, 0xFFFB }
253 };
254 
255 static const struct mb_ucsrange mb_ucs_fullwidth[] = {
256 	{ 0x1100, 0x115F },
257 	{ 0x2329, 0x232A },
258 	{ 0x2E80, 0x303E },
259 	{ 0x3040, 0xA4CF },
260 	{ 0xA960, 0xA97F },
261 	{ 0xAC00, 0xD7A3 },
262 	{ 0xF900, 0xFAFF },
263 	{ 0xFE10, 0xFE19 },
264 	{ 0xFE30, 0xFE6F },
265 	{ 0xFF00, 0xFF60 },
266 	{ 0xFFE0, 0xFFE6 }
267 };
268 
269 /* from mksh */
270 #define NELEM(a)	(sizeof(a) / sizeof((a)[0]))
271 
272 /* simple binary search in ranges, with bounds optimisation */
273 static int
mb_ucsbsearch(const struct mb_ucsrange arr[],size_t elems,wchar_t val)274 mb_ucsbsearch(const struct mb_ucsrange arr[], size_t elems, wchar_t val)
275 {
276 	size_t min = 0, mid, max = elems;
277 
278 	if (val < arr[min].beg || val > arr[max - 1].end)
279 		return (0);
280 
281 	while (min < max) {
282 		mid = (min + max) / 2;
283 
284 		if (val < arr[mid].beg)
285 			max = mid;
286 		else if (val > arr[mid].end)
287 			min = mid + 1;
288 		else
289 			return (1);
290 	}
291 	return (0);
292 }
293 
294 /* Unix column width of a wide character (Unicode code point, really) */
295 int
wcwidth(wchar_t wc)296 wcwidth(wchar_t wc)
297 {
298 	/* except NUL, C0/C1 control characters and DEL yield -1 */
299 	if (wc < 0x20 || (wc >= 0x7F && wc < 0xA0))
300 		return (wc ? -1 : 0);
301 
302 	/* combining characters use 0 screen columns */
303 	if (mb_ucsbsearch(mb_ucs_combining, NELEM(mb_ucs_combining), wc))
304 		return (0);
305 
306 	/* all others use 1 or 2 screen columns */
307 	if (mb_ucsbsearch(mb_ucs_fullwidth, NELEM(mb_ucs_fullwidth), wc))
308 		return (2);
309 	return (1);
310 }
311