-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutf8.c
More file actions
375 lines (345 loc) · 8.08 KB
/
Copy pathutf8.c
File metadata and controls
375 lines (345 loc) · 8.08 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
Copyright (C) 2018 Mark Alexander
This file is part of MicroEMACS, a small text editor.
MicroEMACS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* To compile this file as a test program:
*
* gcc -DTEST -Isys/unix -Itty/ncurses -g -o utf8 utf8.c
*
* Replace -g with -O to make an optimized version in which
* uclen (defined in def.h) is inlined.
*/
#define _XOPEN_SOURCE
#include <wchar.h>
#include "def.h"
#ifdef TEST
#include <time.h>
#endif
#include <string.h>
#include <stdio.h>
/*
* Return true if Unicode character c is a combining character,
* i.e. is a non-spacing character that combines
* with a subsequent one.
*/
int
ucombining (wchar_t c)
{
return ((c >= 0x300 && c <= 0x36f) ||
(c >= 0x1ab0 && c <= 0x1aff) ||
(c >= 0x1dc0 && c <= 0x1dff) ||
(c >= 0x20d0 && c <= 0x20ff) ||
(c >= 0xfe20 && c <= 0xfe2f));
}
/*
* Return the address of the nth UTF-8 character in the string s.
*/
const uchar *
ugetcptr (const uchar *s, int n)
{
while (n > 0)
{
s += uclen (s);
--n;
}
return s;
}
/*
* Return the byte offset of the nth UTF-8 character in the string s.
*/
int
uoffset (const uchar *s, int n)
{
const uchar *start = s;
while (n > 0)
{
s += uclen (s);
--n;
}
return s - start;
}
/*
* Return number of UTF-8 characters in the null-terminated string s.
*/
int
uslen (const uchar *s)
{
int len = 0;
while (*s != 0)
{
s += uclen (s);
len++;
}
return len;
}
/*
* Return number of UTF-8 characters in the string s of length n.
*/
int
unslen (const uchar *s, int n)
{
int len = 0;
const uchar *end = s + n;
while (s < end)
{
s += uclen (s);
len++;
}
return len;
}
/*
* Return the number of bytes used by the next n UFT-8 characters
* in the string s.
*/
int
unblen (const uchar *s, int n)
{
const uchar *start = s;
while (n > 0)
{
s += uclen (s);
--n;
}
return s - start;
}
/*
* Get the nth UTF-8 character in s, return it
* as a 32-bit unicode character. If len is not NULL,
* return the length of the UTF-8 character to *len.
*/
wchar_t
ugetc (const uchar *s, int n, int *len)
{
uchar c;
s = ugetcptr (s, n);
c = *s;
if (c < 0x80)
{
if (len)
*len = 1;
return c;
}
if (c >= 0xc0 && c <= 0xdf)
{
if (len)
*len = 2;
return ((int)(c & 0x1f) << 6) + (int)(s[1] & 0x3f);
}
if (c >= 0xe0 && c <= 0xef)
{
if (len)
*len = 3;
return ((int)(c & 0xf) << 12) +
((int)(s[1] & 0x3f) << 6) +
(int)(s[2] & 0x3f);
}
if (c >= 0xf0 && c <= 0xf7)
{
if (len)
*len = 4;
return ((int)(c & 0x7) << 18) +
((int)(s[1] & 0x3f) << 12) +
((int)(s[2] & 0x3f) << 6) +
(int)(s[3] & 0x3f);
}
if (c >= 0xf8 && c <= 0xfb)
{
if (len)
*len = 5;
return ((int)(c & 0x3) << 24) +
((int)(s[1] & 0x3f) << 18) +
((int)(s[2] & 0x3f) << 12) +
((int)(s[3] & 0x3f) << 6) +
(int)(s[4] & 0x3f);
}
if (c >= 0xfc && c <= 0xfd)
{
if (len)
*len = 6;
return ((int)(c & 0x1) << 30) +
((int)(s[1] & 0x3f) << 24) +
((int)(s[2] & 0x3f) << 18) +
((int)(s[3] & 0x3f) << 12) +
((int)(s[4] & 0x3f) << 6) +
(int)(s[5] & 0x3f);
}
/* Error */
if (len)
*len = 1;
return c;
}
/*
* Get the previous UTF-8 character, i.e. the character just
* before the one pointed to by s. Return it
* as a 32-bit unicode character. If len is not NULL,
* return the length of the UTF-8 character to *len.
*/
wchar_t
ugetprevc (const uchar *s, int *len)
{
do {
--s;
} while ((*s & 0xc0) == 0x80);
return ugetc (s, 0, len);
}
/*
* Convert a Unicode character c to UTF-8, writing the
* characters to s; s must be at least 6 bytes long.
* Return the number of bytes in the UTF-8 string.
*/
int
uputc (wchar_t c, uchar *s)
{
if (c < 0x80)
{
s[0] = c;
return 1;
}
if (c >= 0x80 && c <= 0x7ff)
{
s[0] = 0xc0 | ((c >> 6) & 0x1f);
s[1] = 0x80 | (c & 0x3f);
return 2;
}
if (c >= 0x800 && c <= 0xffff)
{
s[0] = 0xe0 | ((c >> 12) & 0x0f);
s[1] = 0x80 | ((c >> 6) & 0x3f);
s[2] = 0x80 | (c & 0x3f);
return 3;
}
if (c >= 0x10000 && c <= 0x1fffff)
{
s[0] = 0xf0 | ((c >> 18) & 0x07);
s[1] = 0x80 | ((c >> 12) & 0x3f);
s[2] = 0x80 | ((c >> 6) & 0x3f);
s[3] = 0x80 | (c & 0x3f);
return 4;
}
if (c >= 0x200000 && c <= 0x3ffffff)
{
s[0] = 0xf8 | ((c >> 24) & 0x03);
s[1] = 0x80 | ((c >> 18) & 0x3f);
s[2] = 0x80 | ((c >> 12) & 0x3f);
s[3] = 0x80 | ((c >> 6) & 0x3f);
s[4] = 0x80 | (c & 0x3f);
return 5;
}
if (c >= 0x4000000 && c <= 0x7fffffff)
{
s[0] = 0xfc | ((c >> 30) & 0x01);
s[1] = 0x80 | ((c >> 24) & 0x3f);
s[2] = 0x80 | ((c >> 18) & 0x3f);
s[3] = 0x80 | ((c >> 12) & 0x3f);
s[4] = 0x80 | ((c >> 6) & 0x3f);
s[5] = 0x80 | (c & 0x3f);
return 6;
}
/* Error */
s[0] = c;
return 1;
}
/*
* Return the display width of a Unicode character.
* This is just a wrapper for wcwidth.
*/
int
uwidth (wchar_t c)
{
return wcwidth(c);
}
/*
* Prompt for a string of hex numbers separated by spaces.
* Treat each hex number as a Unicode character, convert
* it to UTF-8, and insert into the current buffer.
*/
#ifndef TEST
int
unicode (int f, int n, int k)
{
int s, len, i;
char buf[80];
char *p;
unsigned int c[40];
int count;
s = ereply ("Enter Unicode characters in hex: ", buf, sizeof (buf));
if (s != TRUE)
return s;
for (p = buf, count = 0; p < &buf[sizeof (buf)] && *p != '\0'; p += len, ++count)
{
s = sscanf (p, " %x%n", &c[count], &len);
if (s != 1)
{
eprintf("Illegal hex number: %s", p);
return FALSE;
}
}
for (i = 0; i < count; i++)
{
if (linsert (1, c[i], NULL) == FALSE)
return FALSE;
}
return TRUE;
}
#endif
#ifdef TEST
int
main(int argc, char *argv[])
{
static uchar s[] = { 'a', '=', 0xc3, 0xa4, ',', 'i', '=', 0xe2, 0x88, 0xab, ',',
'+', '=', 0xf0, 0x90, 0x80, 0x8f, ',',
'j', '=', 0xf0, 0x9f, 0x82, 0xab, '.',
'a', '=', 0xc3, 0xa4, ',', 'i', '=', 0xe2, 0x88, 0xab, ',',
'+', '=', 0xf0, 0x90, 0x80, 0x8f, ',',
'j', '=', 0xf0, 0x9f, 0x82, 0xab, '.', 0 };
wchar_t c;
int i, len;
double time_spent;
clock_t begin, end;
const uchar *p;
printf("size of wchar_t is %ld\n", sizeof(c));
printf("s is '%s'\n", s);
printf("length of s is %ld\n", sizeof(s));
len = uslen(s);
printf("number of UTF-8 chars in s is %d\n", len);
printf("Scanning forward...\n");
for (i = 0; i < len; i++)
{
int u, size;
printf("offset of UTF-8 char #%d in s is %ld\n", i, ugetcptr(s, i) - s);
u = ugetc(s, i, &size);
printf("char #%d in s in unicode is %x, size %d\n", i, u, size);
}
printf("Scanning backwards...\n");
p = s + strlen (s);
i = len;
while (p > s)
{
int u, size;
u = ugetprevc (p, &size);
p -= size;
--i;
printf("offset of UTF-8 char #%d in s is %ld\n", i, p - s);
printf("char #%d in s in unicode is %x, size %d\n", i, u, size);
}
begin = clock ();
for (i = 0; i < 10000000; i++)
if ((p = ugetcptr (s, len)) == s)
printf ("Should never get here!\n");
end = clock ();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf ("Time spent in %d loops of ugetcptr is %f\n", i, time_spent);
return 0;
}
#endif