aboutsummaryrefslogtreecommitdiff
path: root/utf8.c
blob: 0ea606482b47905c55b84be2416315afe82a5f1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "telescope.h"

#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <wchar.h>

#define UTF8_ACCEPT 0
#define UTF8_REJECT 1

static const uint8_t utf8d[] = {
	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
	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
	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
	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
	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
	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
	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
	0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef
	0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff
	0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0
	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
	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
	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
	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
};

uint32_t
utf8_decode(uint32_t* restrict state, uint32_t* restrict codep, uint8_t byte) {
	uint32_t type = utf8d[byte];

	*codep = (*state != UTF8_ACCEPT) ?
		(byte & 0x3fu) | (*codep << 6) :
		(0xff >> type) & (byte);

	*state = utf8d[256 + *state*16 + type];
	return *state;
}


/* end of the converter, utility functions ahead */

/* encode cp in s.  s must be at least 4 bytes wide */
size_t
utf8_encode(uint32_t cp, char *s)
{
	if (cp <= 0x7F) {
                *s = (uint8_t)cp;
		return 1;
	} else if (cp <= 0x7FF) {
                s[1] = (uint8_t)(( cp        & 0x3F ) + 0x80);
		s[0] = (uint8_t)(((cp >>  6) & 0x1F) + 0xC0);
		return 2;
	} else if (cp <= 0xFFFF) {
                s[2] = (uint8_t)(( cp        & 0x3F) + 0x80);
		s[1] = (uint8_t)(((cp >>  6) & 0x3F) + 0x80);
		s[0] = (uint8_t)(((cp >> 12) & 0x0F) + 0xE0);
		return 3;
	} else if (cp <= 0x10FFFF) {
                s[3] = (uint8_t)(( cp        & 0x3F) + 0x80);
		s[2] = (uint8_t)(((cp >>  6) & 0x3F) + 0x80);
		s[1] = (uint8_t)(((cp >> 12) & 0x3F) + 0x80);
		s[0] = (uint8_t)(((cp >> 18) & 0x07) + 0xF0);
		return 4;
	} else {
		s[0] = '\0';
		return 0;
	}
}

char *
utf8_nth(char *s, size_t n)
{
	size_t i;
	uint32_t cp = 0, state = 0;

	for (i = 0; *s && i < n; ++s)
		if (!utf8_decode(&state, &cp, *s))
			++i;

	if (state != UTF8_ACCEPT)
		return NULL;
	if (i == n)
		return s;
	return NULL;
}

size_t
utf8_cplen(char *s)
{
	uint32_t cp = 0, state = 0;
	size_t len;

	len = 0;
	for (; *s; ++s)
		if (!utf8_decode(&state, &cp, *s))
			len++;
	return len;
}

/* returns only 0, 1, 2 or 8.  assumes sizeof(wchar_t) is 4 */
size_t
utf8_chwidth(uint32_t cp)
{
	/* XXX: if we're running on a platform where sizeof(wchar_t)
	 * == 2 what to do?  The manpage for wcwidth and wcs isn't
	 * clear about the encoding, but if it's 16 bit wide I assume
	 * it must use UTF-16... right? */
	assert(sizeof(wchar_t) == 4);

	/*
	 * quick and dirty fix for the tabs.  In the future we may
	 * want to expand tabs into N spaces, but for the time being
	 * this seems to be good enough (tm).
	 */
	if (cp == '\t')
		return 8;

	return wcwidth((wchar_t)cp);
}

/* NOTE: n is the number of codepoints, NOT the byte length.  In
 * other words, s MUST be NUL-terminated. */
size_t
utf8_snwidth(const char *s, size_t n)
{
	size_t i, tot;
	uint32_t cp = 0, state = 0;

	tot = 0;
	for (i = 0; *s && i < n; ++s)
		if (!utf8_decode(&state, &cp, *s)) {
			i++;
			tot += utf8_chwidth(cp);
		}

	return tot;
}

size_t
utf8_swidth(const char *s)
{
	size_t tot;
	uint32_t cp = 0, state = 0;

	tot = 0;
	for (; *s; ++s)
		if (!utf8_decode(&state, &cp, *s))
			tot += utf8_chwidth(cp);

	return tot;
}

size_t
utf8_swidth_between(const char *str, const char *end)
{
	size_t tot;
	uint32_t cp = 0, state = 0;

	tot = 0;
	for (; *str && str < end; ++str)
		if (!utf8_decode(&state, &cp, *str))
			tot += utf8_chwidth(cp);
	return tot;
}

char *
utf8_next_cp(const char *s)
{
	uint32_t cp = 0, state = 0;

	for (; *s; ++s)
		if (!utf8_decode(&state, &cp, *s))
			break;
	return (char*)s+1;
}

char *
utf8_prev_cp(const char *start, const char *base)
{
	uint8_t c;

	for (; start > base; start--) {
		c = *start;
                if ((c & 0xC0) != 0x80)
			return (char*)start;
	}

	return (char*)base;
}