1 : /* Copyright (c) 2007 James Antill -- See LICENSE file for terms. */
2 :
3 : #ifndef USTR_UTF8_H
4 : #error " You should have already included ustr-utf8.h, or just include ustr.h."
5 : #endif
6 :
7 : /* import and hacked from:
8 : http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c 2007-06-04 */
9 : /*
10 : * This is an implementation of wcwidth() and wcswidth() (defined in
11 : * IEEE Std 1002.1-2001) for Unicode.
12 : *
13 : * http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html
14 : * http://www.opengroup.org/onlinepubs/007904975/functions/wcswidth.html
15 : *
16 : * In fixed-width output devices, Latin characters all occupy a single
17 : * "cell" position of equal width, whereas ideographic CJK characters
18 : * occupy two such cells. Interoperability between terminal-line
19 : * applications and (teletype-style) character terminals using the
20 : * UTF-8 encoding requires agreement on which character should advance
21 : * the cursor by how many cell positions. No established formal
22 : * standards exist at present on which Unicode character shall occupy
23 : * how many cell positions on character terminals. These routines are
24 : * a first attempt of defining such behavior based on simple rules
25 : * applied to data provided by the Unicode Consortium.
26 : *
27 : * For some graphical characters, the Unicode standard explicitly
28 : * defines a character-cell width via the definition of the East Asian
29 : * FullWidth (F), Wide (W), Half-width (H), and Narrow (Na) classes.
30 : * In all these cases, there is no ambiguity about which width a
31 : * terminal shall use. For characters in the East Asian Ambiguous (A)
32 : * class, the width choice depends purely on a preference of backward
33 : * compatibility with either historic CJK or Western practice.
34 : * Choosing single-width for these characters is easy to justify as
35 : * the appropriate long-term solution, as the CJK practice of
36 : * displaying these characters as double-width comes from historic
37 : * implementation simplicity (8-bit encoded characters were displayed
38 : * single-width and 16-bit ones double-width, even for Greek,
39 : * Cyrillic, etc.) and not any typographic considerations.
40 : *
41 : * Much less clear is the choice of width for the Not East Asian
42 : * (Neutral) class. Existing practice does not dictate a width for any
43 : * of these characters. It would nevertheless make sense
44 : * typographically to allocate two character cells to characters such
45 : * as for instance EM SPACE or VOLUME INTEGRAL, which cannot be
46 : * represented adequately with a single-width glyph. The following
47 : * routines at present merely assign a single-cell width to all
48 : * neutral characters, in the interest of simplicity. This is not
49 : * entirely satisfactory and should be reconsidered before
50 : * establishing a formal standard in this area. At the moment, the
51 : * decision which Not East Asian (Neutral) characters should be
52 : * represented by double-width glyphs cannot yet be answered by
53 : * applying a simple rule from the Unicode database content. Setting
54 : * up a proper standard for the behavior of UTF-8 character terminals
55 : * will require a careful analysis not only of each Unicode character,
56 : * but also of each presentation form, something the author of these
57 : * routines has avoided to do so far.
58 : *
59 : * http://www.unicode.org/unicode/reports/tr11/
60 : *
61 : * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
62 : *
63 : * Permission to use, copy, modify, and distribute this software
64 : * for any purpose and without fee is hereby granted. The author
65 : * disclaims all warranties with regard to this software.
66 : *
67 : * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
68 : */
69 :
70 : /* auxiliary function for binary search in interval table */
71 : USTR_CONF_i_PROTO
72 : int ustr__utf8_bisearch(USTR__UTF8_WCHAR ucs,
73 : const struct ustr__utf8_interval *table, int max)
74 56 : {
75 56 : int min = 0;
76 : int mid;
77 :
78 56 : if (ucs < table[0].first || ucs > table[max].last)
79 32 : return (USTR_FALSE);
80 212 : while (max >= min) {
81 180 : mid = (min + max) / 2;
82 180 : if (ucs > table[mid].last)
83 120 : min = mid + 1;
84 60 : else if (ucs < table[mid].first)
85 56 : max = mid - 1;
86 : else
87 4 : return (USTR_TRUE);
88 : }
89 :
90 20 : return (USTR_FALSE);
91 : }
92 :
93 :
94 : /* The following two functions define the column width of an ISO 10646
95 : * character as follows:
96 : *
97 : * - The null character (U+0000) has a column width of 0.
98 : *
99 : * - Other C0/C1 control characters and DEL will lead to a return
100 : * value of -1.
101 : *
102 : * - Non-spacing and enclosing combining characters (general
103 : * category code Mn or Me in the Unicode database) have a
104 : * column width of 0.
105 : *
106 : * - SOFT HYPHEN (U+00AD) has a column width of 1.
107 : *
108 : * - Other format characters (general category code Cf in the Unicode
109 : * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
110 : *
111 : * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
112 : * have a column width of 0.
113 : *
114 : * - Spacing characters in the East Asian Wide (W) or East Asian
115 : * Full-width (F) category as defined in Unicode Technical
116 : * Report #11 have a column width of 2.
117 : *
118 : * - All remaining characters (including all printable
119 : * ISO 8859-1 and WGL4 characters, Unicode control characters,
120 : * etc.) have a column width of 1.
121 : *
122 : * This implementation assumes that wchar_t characters are encoded
123 : * in ISO 10646.
124 : */
125 :
126 : USTR_CONF_i_PROTO
127 : USTR__SSIZE ustr__utf8_mk_wcwidth(USTR__UTF8_WCHAR ucs)
128 66 : {
129 : /* sorted list of non-overlapping intervals of non-spacing characters */
130 : /* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */
131 : static const struct ustr__utf8_interval combining[] = {
132 : { 0x0300, 0x036F }, { 0x0483, 0x0486 }, { 0x0488, 0x0489 },
133 : { 0x0591, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 },
134 : { 0x05C4, 0x05C5 }, { 0x05C7, 0x05C7 }, { 0x0600, 0x0603 },
135 : { 0x0610, 0x0615 }, { 0x064B, 0x065E }, { 0x0670, 0x0670 },
136 : { 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED },
137 : { 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A },
138 : { 0x07A6, 0x07B0 }, { 0x07EB, 0x07F3 }, { 0x0901, 0x0902 },
139 : { 0x093C, 0x093C }, { 0x0941, 0x0948 }, { 0x094D, 0x094D },
140 : { 0x0951, 0x0954 }, { 0x0962, 0x0963 }, { 0x0981, 0x0981 },
141 : { 0x09BC, 0x09BC }, { 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD },
142 : { 0x09E2, 0x09E3 }, { 0x0A01, 0x0A02 }, { 0x0A3C, 0x0A3C },
143 : { 0x0A41, 0x0A42 }, { 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D },
144 : { 0x0A70, 0x0A71 }, { 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC },
145 : { 0x0AC1, 0x0AC5 }, { 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD },
146 : { 0x0AE2, 0x0AE3 }, { 0x0B01, 0x0B01 }, { 0x0B3C, 0x0B3C },
147 : { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 }, { 0x0B4D, 0x0B4D },
148 : { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 }, { 0x0BC0, 0x0BC0 },
149 : { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 }, { 0x0C46, 0x0C48 },
150 : { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 }, { 0x0CBC, 0x0CBC },
151 : { 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD },
152 : { 0x0CE2, 0x0CE3 }, { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D },
153 : { 0x0DCA, 0x0DCA }, { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 },
154 : { 0x0E31, 0x0E31 }, { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E },
155 : { 0x0EB1, 0x0EB1 }, { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC },
156 : { 0x0EC8, 0x0ECD }, { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 },
157 : { 0x0F37, 0x0F37 }, { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E },
158 : { 0x0F80, 0x0F84 }, { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 },
159 : { 0x0F99, 0x0FBC }, { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 },
160 : { 0x1032, 0x1032 }, { 0x1036, 0x1037 }, { 0x1039, 0x1039 },
161 : { 0x1058, 0x1059 }, { 0x1160, 0x11FF }, { 0x135F, 0x135F },
162 : { 0x1712, 0x1714 }, { 0x1732, 0x1734 }, { 0x1752, 0x1753 },
163 : { 0x1772, 0x1773 }, { 0x17B4, 0x17B5 }, { 0x17B7, 0x17BD },
164 : { 0x17C6, 0x17C6 }, { 0x17C9, 0x17D3 }, { 0x17DD, 0x17DD },
165 : { 0x180B, 0x180D }, { 0x18A9, 0x18A9 }, { 0x1920, 0x1922 },
166 : { 0x1927, 0x1928 }, { 0x1932, 0x1932 }, { 0x1939, 0x193B },
167 : { 0x1A17, 0x1A18 }, { 0x1B00, 0x1B03 }, { 0x1B34, 0x1B34 },
168 : { 0x1B36, 0x1B3A }, { 0x1B3C, 0x1B3C }, { 0x1B42, 0x1B42 },
169 : { 0x1B6B, 0x1B73 }, { 0x1DC0, 0x1DCA }, { 0x1DFE, 0x1DFF },
170 : { 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x2060, 0x2063 },
171 : { 0x206A, 0x206F }, { 0x20D0, 0x20EF }, { 0x302A, 0x302F },
172 : { 0x3099, 0x309A }, { 0xA806, 0xA806 }, { 0xA80B, 0xA80B },
173 : { 0xA825, 0xA826 }, { 0xFB1E, 0xFB1E }, { 0xFE00, 0xFE0F },
174 : { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF }, { 0xFFF9, 0xFFFB },
175 : { 0x10A01, 0x10A03 }, { 0x10A05, 0x10A06 }, { 0x10A0C, 0x10A0F },
176 : { 0x10A38, 0x10A3A }, { 0x10A3F, 0x10A3F }, { 0x1D167, 0x1D169 },
177 : { 0x1D173, 0x1D182 }, { 0x1D185, 0x1D18B }, { 0x1D1AA, 0x1D1AD },
178 : { 0x1D242, 0x1D244 }, { 0xE0001, 0xE0001 }, { 0xE0020, 0xE007F },
179 : { 0xE0100, 0xE01EF }
180 : };
181 :
182 : /* test for 8-bit control characters */
183 66 : if (ucs == 0)
184 2 : return 0;
185 :
186 64 : if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
187 8 : return (-1);
188 :
189 : /* binary search in table of non-spacing characters */
190 56 : if (ustr__utf8_bisearch(ucs, combining, sizeof(combining) /
191 : sizeof(struct ustr__utf8_interval) - 1))
192 4 : return 0;
193 :
194 : /* if we arrive here, ucs is not a combining or C0/C1 control character */
195 :
196 52 : return 1 +
197 : (ucs >= 0x1100 &&
198 : (ucs <= 0x115f || /* Hangul Jamo init. consonants */
199 : ucs == 0x2329 || ucs == 0x232a ||
200 : (ucs >= 0x2e80 && ucs <= 0xa4cf &&
201 : ucs != 0x303f) || /* CJK ... Yi */
202 : (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
203 : (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */
204 : (ucs >= 0xfe10 && ucs <= 0xfe19) || /* Vertical forms */
205 : (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
206 : (ucs >= 0xff00 && ucs <= 0xff60) || /* Fullwidth Forms */
207 : (ucs >= 0xffe0 && ucs <= 0xffe6) ||
208 : (ucs >= 0x20000 && ucs <= 0x2fffd) ||
209 : (ucs >= 0x30000 && ucs <= 0x3fffd)));
210 : }
211 :
212 : /* import and hacked from:
213 : http://www.cl.cam.ac.uk/~mgk25/ucs/utf8_check.c 2007-06-04 */
214 : /*
215 : * The utf8_check() function scans the '\0'-terminated string starting
216 : * at s. It returns a pointer to the first byte of the first malformed
217 : * or overlong UTF-8 sequence found, or NULL if the string contains
218 : * only correct UTF-8. It also spots UTF-8 sequences that could cause
219 : * trouble if converted to UTF-16, namely surrogate characters
220 : * (U+D800..U+DFFF) and non-Unicode positions (U+FFFE..U+FFFF). This
221 : * routine is very likely to find a malformed sequence if the input
222 : * uses any other encoding than UTF-8. It therefore can be used as a
223 : * very effective heuristic for distinguishing between UTF-8 and other
224 : * encodings.
225 : *
226 : * Markus Kuhn <http://www.cl.cam.ac.uk/~mgk25/> -- 2005-03-30
227 : */
228 : USTR_CONF_i_PROTO
229 : USTR__UTF8_WCHAR ustr__utf8_check(const unsigned char **ps)
230 264 : {
231 264 : const unsigned char *s = *ps;
232 264 : USTR__UTF8_WCHAR ret = 0;
233 :
234 264 : if (*s < 0x80) /* 0xxxxxxx */
235 : {
236 56 : ret = *s;
237 56 : s++;
238 : }
239 208 : else if ((s[0] & 0xe0) == 0xc0) /* 110XXXXx 10xxxxxx */
240 : {
241 152 : if (((s[1] & 0xc0) != 0x80) ||
242 : ((s[0] & 0xfe) == 0xc0)) /* overlong? */
243 : goto utf8_fail;
244 :
245 152 : ret = ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
246 152 : s += 2;
247 : }
248 56 : else if ((s[0] & 0xf0) == 0xe0) /* 1110XXXX 10Xxxxxx 10xxxxxx */
249 : {
250 24 : if (((s[1] & 0xc0) != 0x80) || ((s[2] & 0xc0) != 0x80) ||
251 : ((s[0] == 0xe0) && ((s[1] & 0xe0) == 0x80)) || /* overlong? */
252 : ((s[0] == 0xed) && ((s[1] & 0xe0) == 0xa0)) || /* surrogate? */
253 : ((s[0] == 0xef) && (s[1] == 0xbf) &&
254 : ((s[2] & 0xfe) == 0xbe))) /* U+FFFE or U+FFFF? */
255 : goto utf8_fail;
256 :
257 24 : ret = (((s[0] & 0x0f) << 12) | ((s[1] & 0x3f) << 6) | (s[2] & 0x3f));
258 24 : s += 3;
259 : }
260 32 : else if ((s[0] & 0xf8) == 0xf0) /* 11110XXX 10XXxxxx 10xxxxxx 10xxxxxx */
261 : {
262 16 : if (((s[1] & 0xc0) != 0x80) ||
263 : ((s[2] & 0xc0) != 0x80) ||
264 : ((s[3] & 0xc0) != 0x80) ||
265 : ((s[0] == 0xf0) && ((s[1] & 0xf0) == 0x80)) || /* overlong? */
266 : ((s[0] == 0xf4) && (s[1] > 0x8f)) || (s[0] > 0xf4)) /* > U+10FFFF? */
267 : goto utf8_fail;
268 :
269 16 : ret = (((s[0] & 0x07) << 18) | ((s[1] & 0x3f) << 12) |
270 : ((s[2] & 0x3f) << 6) | (s[3] & 0x3f));
271 16 : s += 4;
272 : }
273 : else
274 16 : goto utf8_fail;
275 :
276 248 : *ps = s;
277 248 : return (ret);
278 :
279 16 : utf8_fail:
280 16 : *ps = NULL;
281 16 : return (0);
282 : }
283 :
284 : /* See: http://en.wikipedia.org/wiki/UTF-8#Description */
285 : USTR_CONF_e_PROTO
286 : const unsigned char *ustr__utf8_prev(const unsigned char *ptr, size_t len)
287 549 : { /* find the begining of the previous UTF-8 character, no checking */
288 1965 : while (len--)
289 : {
290 1400 : if ((*--ptr & 0xc0) != 0x80)
291 716 : return (ptr);
292 : }
293 :
294 16 : return (0);
295 : }
296 :
297 : USTR_CONF_e_PROTO
298 : const unsigned char *ustr__utf8_next(const unsigned char *ptr)
299 3096 : { /* find the begining of the next UTF-8 character, no checking.
300 : * -- assumes NIL termination, so no length. */
301 : while (1)
302 : {
303 4128 : if ((*++ptr & 0xc0) != 0x80)
304 1584 : break;
305 1008 : }
306 :
307 2112 : return (ptr);
308 : }
309 :
310 : USTR_CONF_I_PROTO
311 : int ustr_utf8_valid(const struct Ustr *s1)
312 60 : {
313 60 : const unsigned char *beg = (const unsigned char *)ustr_cstr(s1);
314 60 : const unsigned char *scan = beg;
315 60 : size_t ret = 0;
316 :
317 30 : USTR_ASSERT(ustr_assert_valid(s1));
318 :
319 274 : while (*scan)
320 : {
321 96 : USTR_ASSERT(ustr_len(s1) > (size_t)(scan - beg));
322 :
323 192 : ustr__utf8_check(&scan);
324 192 : if (!scan)
325 8 : return (USTR_FALSE);
326 184 : ++ret;
327 : }
328 26 : USTR_ASSERT(ustr_len(s1) >= (size_t)(scan - beg));
329 :
330 52 : if (ustr_len(s1) != (size_t)(scan - beg))
331 4 : return (USTR_FALSE); /* string contains a NIL byte */
332 :
333 48 : return (USTR_TRUE);
334 : }
335 :
336 : USTR_CONF_I_PROTO
337 : size_t ustr_utf8_len(const struct Ustr *s1)
338 104 : { /* this is a "fast" version */
339 104 : const unsigned char *scan = (const unsigned char *)ustr_cstr(s1);
340 104 : size_t ret = 0;
341 :
342 52 : USTR_ASSERT(ustr_assert_valid(s1));
343 :
344 1852 : while (*scan)
345 1696 : ret += ((*scan++ & 0xc0) != 0x80);
346 :
347 104 : return (ret);
348 : }
349 :
350 : USTR__SSIZE ustr_utf8_width(const struct Ustr *s1)
351 52 : {
352 52 : const unsigned char *beg = (const unsigned char *)ustr_cstr(s1);
353 52 : const unsigned char *scan = beg;
354 52 : USTR__SSIZE ret = 0;
355 :
356 26 : USTR_ASSERT(ustr_assert_valid(s1));
357 :
358 142 : while (*scan)
359 : {
360 72 : USTR__UTF8_WCHAR tmp = 0;
361 :
362 36 : USTR_ASSERT(ustr_len(s1) > (size_t)(scan - beg));
363 :
364 72 : tmp = ustr__utf8_check(&scan);
365 72 : if (!scan)
366 8 : return (0);
367 64 : ret += ustr__utf8_mk_wcwidth(tmp);
368 : }
369 22 : USTR_ASSERT(ustr_len(s1) >= (size_t)(scan - beg));
370 :
371 44 : if (ustr_len(s1) != (size_t)(scan - beg))
372 4 : return (0); /* string contains a NIL byte */
373 :
374 40 : return (ret);
375 : }
376 :
377 : USTR_CONF_I_PROTO
378 : size_t ustr_utf8_chars2bytes(const struct Ustr *s1, size_t pos, size_t len,
379 : size_t *pret_pos)
380 204 : {
381 204 : const unsigned char *beg = (const unsigned char *)ustr_cstr(s1);
382 204 : const unsigned char *scan = beg;
383 204 : const unsigned char *ret_beg = beg;
384 204 : size_t ret_pos = 0;
385 :
386 102 : USTR_ASSERT(ustr_assert_valid_subustr(s1, pos, len) || !len);
387 102 : USTR_ASSERT(pret_pos || (pos == 1));
388 :
389 830 : while (*scan)
390 : {
391 724 : const unsigned char *prev = scan;
392 :
393 362 : USTR_ASSERT(ustr_len(s1) > (size_t)(scan - beg));
394 :
395 724 : scan = ustr__utf8_next(scan);
396 724 : if (!--pos)
397 : {
398 200 : ret_beg = prev;
399 200 : ret_pos = (ret_beg - beg) + 1;
400 200 : break;
401 : }
402 : }
403 :
404 204 : if (len)
405 710 : while (*scan && --len)
406 : {
407 214 : USTR_ASSERT(ustr_len(s1) > (size_t)(scan - beg));
408 :
409 428 : scan = ustr__utf8_next(scan);
410 : }
411 :
412 102 : USTR_ASSERT(ustr_len(s1) >= (size_t)(scan - beg));
413 :
414 204 : if (len > 1)
415 8 : return (0); /* string contains a NIL byte, or ends with a bad UTF-8 char */
416 :
417 196 : if (pret_pos)
418 144 : *pret_pos = ret_pos;
419 :
420 196 : return (scan - ret_beg);
421 : }
422 :
423 : USTR_CONF_I_PROTO
424 : size_t ustr_utf8_bytes2chars(const struct Ustr *s1, size_t pos, size_t len,
425 : size_t *pret_pos)
426 110 : {
427 110 : const unsigned char *beg = (const unsigned char *)ustr_cstr(s1);
428 110 : const unsigned char *scan = beg;
429 110 : const unsigned char *ret_beg = beg;
430 110 : size_t clen = ustr_assert_valid_subustr(s1, pos, len);
431 110 : size_t unum = 0;
432 110 : size_t ret_pos = 0;
433 :
434 54 : USTR_ASSERT(pret_pos || (pos == 1));
435 :
436 110 : if (!clen) /* bad position */
437 6 : return (0);
438 :
439 104 : scan += pos;
440 104 : if (!(ret_beg = ustr__utf8_prev(scan, pos)))
441 12 : return (0);
442 46 : USTR_ASSERT(ustr_len(s1) >= (size_t)(scan - beg));
443 :
444 92 : scan = beg;
445 272 : while (scan < ret_beg)
446 88 : unum += ((*scan++ & 0xc0) != 0x80);
447 :
448 92 : unum += ((*scan & 0xc0) != 0x80);
449 92 : ret_pos = unum;
450 :
451 92 : if (len)
452 : {
453 80 : ret_beg += len - 1;
454 40 : USTR_ASSERT(ustr_len(s1) >= (size_t)(ret_beg - beg));
455 :
456 552 : while (scan <= ret_beg)
457 432 : unum += ((*scan++ & 0xc0) != 0x80);
458 : }
459 :
460 92 : if (pret_pos)
461 84 : *pret_pos = ret_pos;
462 :
463 92 : return (unum - ret_pos);
464 : }
|