Blob


1 /* Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
2 *
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:
10 *
11 * The above copyright notice and this permission notice shall be
12 * included in all copies or substantial portions of the Software.
13 *
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
21 * SOFTWARE.
22 */
24 #include "telescope.h"
26 #include <assert.h>
27 #include <stddef.h>
28 #include <stdint.h>
29 #include <wchar.h>
31 #define UTF8_ACCEPT 0
32 #define UTF8_REJECT 1
34 static const uint8_t utf8d[] = {
35 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
36 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
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, // 40..5f
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, // 60..7f
39 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
40 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
41 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
42 0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef
43 0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff
44 0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0
45 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
46 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
47 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
48 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
49 };
51 uint32_t
52 utf8_decode(uint32_t* restrict state, uint32_t* restrict codep, uint8_t byte) {
53 uint32_t type = utf8d[byte];
55 *codep = (*state != UTF8_ACCEPT) ?
56 (byte & 0x3fu) | (*codep << 6) :
57 (0xff >> type) & (byte);
59 *state = utf8d[256 + *state*16 + type];
60 return *state;
61 }
64 /* end of the converter, utility functions ahead */
66 /* encode cp in s. s must be at least 4 bytes wide */
67 size_t
68 utf8_encode(uint32_t cp, char *s)
69 {
70 if (cp <= 0x7F) {
71 *s = (uint8_t)cp;
72 return 1;
73 } else if (cp <= 0x7FF) {
74 s[1] = (uint8_t)(( cp & 0x3F ) + 0x80);
75 s[0] = (uint8_t)(((cp >> 6) & 0x1F) + 0xC0);
76 return 2;
77 } else if (cp <= 0xFFFF) {
78 s[2] = (uint8_t)(( cp & 0x3F) + 0x80);
79 s[1] = (uint8_t)(((cp >> 6) & 0x3F) + 0x80);
80 s[0] = (uint8_t)(((cp >> 12) & 0x0F) + 0xE0);
81 return 3;
82 } else if (cp <= 0x10FFFF) {
83 s[3] = (uint8_t)(( cp & 0x3F) + 0x80);
84 s[2] = (uint8_t)(((cp >> 6) & 0x3F) + 0x80);
85 s[1] = (uint8_t)(((cp >> 12) & 0x3F) + 0x80);
86 s[0] = (uint8_t)(((cp >> 18) & 0x07) + 0xF0);
87 return 4;
88 } else {
89 s[0] = '\0';
90 return 0;
91 }
92 }
94 char *
95 utf8_nth(char *s, size_t n)
96 {
97 size_t i;
98 uint32_t cp = 0, state = 0;
100 for (i = 0; *s && i < n; ++s)
101 if (!utf8_decode(&state, &cp, *s))
102 ++i;
104 if (state != UTF8_ACCEPT)
105 return NULL;
106 if (i == n)
107 return s;
108 return NULL;
111 size_t
112 utf8_cplen(char *s)
114 uint32_t cp = 0, state = 0;
115 size_t len;
117 len = 0;
118 for (; *s; ++s)
119 if (!utf8_decode(&state, &cp, *s))
120 len++;
121 return len;
124 /* returns only 0, 1, 2 or 8. assumes sizeof(wchar_t) is 4 */
125 size_t
126 utf8_chwidth(uint32_t cp)
128 /* XXX: if we're running on a platform where sizeof(wchar_t)
129 * == 2 what to do? The manpage for wcwidth and wcs isn't
130 * clear about the encoding, but if it's 16 bit wide I assume
131 * it must use UTF-16... right? */
132 assert(sizeof(wchar_t) == 4);
134 /*
135 * quick and dirty fix for the tabs. In the future we may
136 * want to expand tabs into N spaces, but for the time being
137 * this seems to be good enough (tm).
138 */
139 if (cp == '\t')
140 return 8;
142 return wcwidth((wchar_t)cp);
145 /* NOTE: n is the number of codepoints, NOT the byte length. In
146 * other words, s MUST be NUL-terminated. */
147 size_t
148 utf8_snwidth(const char *s, size_t n)
150 size_t i, tot;
151 uint32_t cp = 0, state = 0;
153 tot = 0;
154 for (i = 0; *s && i < n; ++s)
155 if (!utf8_decode(&state, &cp, *s)) {
156 i++;
157 tot += utf8_chwidth(cp);
160 return tot;
163 size_t
164 utf8_swidth(const char *s)
166 size_t tot;
167 uint32_t cp = 0, state = 0;
169 tot = 0;
170 for (; *s; ++s)
171 if (!utf8_decode(&state, &cp, *s))
172 tot += utf8_chwidth(cp);
174 return tot;
177 size_t
178 utf8_swidth_between(const char *str, const char *end)
180 size_t tot;
181 uint32_t cp = 0, state = 0;
183 tot = 0;
184 for (; *str && str < end; ++str)
185 if (!utf8_decode(&state, &cp, *str))
186 tot += utf8_chwidth(cp);
187 return tot;
190 char *
191 utf8_next_cp(const char *s)
193 uint32_t cp = 0, state = 0;
195 for (; *s; ++s)
196 if (!utf8_decode(&state, &cp, *s))
197 break;
198 return (char*)s+1;
201 char *
202 utf8_prev_cp(const char *start, const char *base)
204 uint8_t c;
206 for (; start > base; start--) {
207 c = *start;
208 if ((c & 0xC0) != 0x80)
209 return (char*)start;
212 return (char*)base;