1 /* Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
3 * Permission is hereby granted, free of charge, to any person
4 * obtaining a copy of this software and associated documentation
5 * files (the "Software"), to deal in the Software without
6 * restriction, including without limitation the rights to use, copy,
7 * modify, merge, publish, distribute, sublicense, and/or sell copies
8 * of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be
12 * included in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 static const uint8_t utf8d[] = {
37 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 00..1f
38 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 20..3f
39 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 40..5f
40 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 60..7f
41 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, // 80..9f
42 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, // a0..bf
43 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // c0..df
44 0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef
45 0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff
46 0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0
47 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1, // s1..s2
48 1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1, // s3..s4
49 1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1, // s5..s6
50 1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // s7..s8
53 static inline uint32_t
54 decode(uint32_t* restrict state, uint32_t* restrict codep, uint8_t byte)
56 uint32_t type = utf8d[byte];
58 *codep = (*state != UTF8_ACCEPT) ?
59 (byte & 0x3fu) | (*codep << 6) :
60 (0xff >> type) & (byte);
62 *state = utf8d[256 + *state*16 + type];
67 /* end of the converter, utility functions ahead */
69 #define ZERO_WIDTH_SPACE 0x200B
71 /* public version of decode */
73 utf8_decode(uint32_t* restrict state, uint32_t* restrict codep, uint8_t byte)
75 return decode(state, codep, byte);
79 * returns 0, 1, 2 or less than 8 for tabs. assumes that
80 * sizeof(wchar_t) == 4
83 utf8_chwidth(uint32_t cp, int col)
85 /* XXX: if we're running on a platform where sizeof(wchar_t)
86 * == 2 what to do? The manpage for wcwidth and wcs isn't
87 * clear about the encoding, but if it's 16 bit wide I assume
88 * it must use UTF-16... right? */
89 assert(sizeof(wchar_t) == 4);
92 * Tabs are wide until the next multiple of eight.
95 return (((col + 8) / 8) * 8) - col;
97 return wcwidth((wchar_t)cp);
101 utf8_snwidth(const char *s, size_t off, int col)
104 uint32_t cp = 0, state = 0;
108 for (i = 0; i < off; ++i)
109 if (!decode(&state, &cp, s[i])) {
110 width = utf8_chwidth(cp, col);
119 utf8_swidth(const char *s, int col )
122 uint32_t cp = 0, state = 0;
127 if (!decode(&state, &cp, *s)) {
128 width = utf8_chwidth(cp, col);
137 utf8_swidth_between(const char *str, const char *end, int col)
140 uint32_t cp = 0, state = 0;
144 for (; *str && str < end; ++str)
145 if (!decode(&state, &cp, *str)) {
146 width = utf8_chwidth(cp, col);
154 * XXX: This is not correct. There are codepoints classified as
155 * "emoji", but these can be joined together to form more complex
156 * emoji. There is an official list of what these valid combinations
157 * are, but it would require a costly lookup (a trie can be used to
158 * reduce the times, but...). The following approach is conceptually
159 * simpler: if there is a sequence of "emoji codepoints" (or ZWS) and
160 * then a space, consider everything before the space a single emoji.
161 * It needs a special check for numbers (yes, 0..9 and # are
162 * technically speaking emojis) but otherwise seems to work well in
166 emojied_line(const char *s, const char **space_ret)
168 uint32_t cp = 0, state = 0;
169 int only_numbers = 1;
172 if (!decode(&state, &cp, *s)) {
173 if (cp == ZERO_WIDTH_SPACE)
177 return !only_numbers;
181 if (cp < '0' || cp > '9')