forked from haipome/utf8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutf8.c
411 lines (329 loc) · 6.83 KB
/
utf8.c
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Description: UTF-8 字符串的解码和编码函数
* unicode 字符处理函数
* History: [email protected], 2013/05/29, create
*
* This code is in the public domain.
* You may use this code any way you wish, private, educational,
* or commercial. It's free.
*/
# include <stdint.h>
# include <stddef.h>
# include "utf8.h"
ucs4_t getu8c(char **src, int *illegal)
{
static char umap[256] = { 0 };
static int umap_init_flag = 0;
if (umap_init_flag == 0)
{
int i;
for (i = 0; i < 0x100; ++i)
{
if (i < 0x80)
{
umap[i] = 1;
}
else if (i >= 0xc0 && i < 0xe0)
{
umap[i] = 2;
}
else if (i >= 0xe0 && i < 0xf0)
{
umap[i] = 3;
}
else if (i >= 0xf0 && i < 0xf8)
{
umap[i] = 4;
}
else if (i >= 0xf8 && i < 0xfc)
{
umap[i] = 5;
}
else if (i >= 0xfc && i < 0xfe)
{
umap[i] = 6;
}
else
{
umap[i] = 0;
}
}
umap_init_flag = 1;
}
uint8_t *s = (uint8_t *)(*src);
int r_illegal = 0;
while (umap[*s] == 0)
{
++s;
++r_illegal;
}
uint8_t *t;
int byte_num;
uint32_t uc;
int i;
repeat_label:
t = s;
byte_num = umap[*s];
uc = *s++ & (0xff >> byte_num);
for (i = 1; i < byte_num; ++i)
{
if (umap[*s])
{
r_illegal += s - t;
goto repeat_label;
}
else
{
uc = (uc << 6) + (*s & 0x3f);
s += 1;
}
}
*src = (char *)s;
if (illegal)
{
*illegal = r_illegal;
}
return uc;
}
size_t u8decode(char const *str, ucs4_t *des, size_t n, int *illegal)
{
if (n == 0)
return 0;
char *s = (char *)str;
size_t i = 0;
ucs4_t uc = 0;
int r_illegal_all = 0, r_illegal;
while ((uc = getu8c(&s, &r_illegal)))
{
if (i < (n - 1))
{
des[i++] = uc;
r_illegal_all += r_illegal;
}
else
{
break;
}
}
des[i] = 0;
if (illegal)
{
*illegal = r_illegal_all + r_illegal;
}
return i;
}
# define IF_CAN_HOLD(left, n) do { \
size_t m = (size_t)(n); \
if ((size_t)(left) < (m + 1)) return -2; \
(left) -= m; \
} while (0)
int putu8c(ucs4_t uc, char **des, size_t *left)
{
if (uc < 0)
return -1;
if (uc < (0x1 << 7))
{
IF_CAN_HOLD(*left, 1);
**des = (char)uc;
*des += 1;
**des = 0;
return 1;
}
int byte_num;
if (uc < (0x1 << 11))
{
byte_num = 2;
}
else if (uc < (0x1 << 16))
{
byte_num = 3;
}
else if (uc < (0x1 << 21))
{
byte_num = 4;
}
else if (uc < (0x1 << 26))
{
byte_num = 5;
}
else
{
byte_num = 6;
}
IF_CAN_HOLD(*left, byte_num);
int i;
for (i = byte_num - 1; i > 0; --i)
{
*(uint8_t *)(*des + i) = (uc & 0x3f) | 0x80;
uc >>= 6;
}
*(uint8_t *)(*des) = uc | (0xff << (8 - byte_num));
*des += byte_num;
**des = 0;
return byte_num;
}
size_t u8encode(ucs4_t *us, char *des, size_t n, int *illegal)
{
if (n == 0)
return 0;
char *s = des;
size_t left = n;
size_t len = 0;
int r_illegal = 0;
*s = 0;
while (*us)
{
int ret = putu8c(*us, &s, &left);
if (ret > 0)
{
len += ret;
}
else if (ret == -1)
{
r_illegal += 1;
}
else
{
break;
}
++us;
}
if (illegal)
{
*illegal = r_illegal;
}
return len;
}
/* 全角字符 */
int isufullwidth(ucs4_t uc)
{
if (uc == 0x3000)
return 1;
if (uc >= 0xff01 && uc <= 0xff5e)
return 1;
return 0;
}
/* 全角字母 */
int isufullwidthalpha(ucs4_t uc)
{
if (uc >= 0xff21 && uc <= 0xff3a)
return 1;
if (uc >= 0xff41 && uc <= 0xff5a)
return 2;
return 0;
}
/* 全角数字 */
int isufullwidthdigit(ucs4_t uc)
{
if (uc >= 0xff10 && uc <= 0xff19)
return 1;
return 0;
}
/* 全角转半角 */
ucs4_t ufull2half(ucs4_t uc)
{
if (uc == 0x3000)
return ' ';
if (uc >= 0xff01 && uc <= 0xff5e)
return uc - 0xfee0;
return uc;
}
/* 半角转全角 */
ucs4_t uhalf2full(ucs4_t uc)
{
if (uc == ' ')
return 0x3000;
if (uc >= 0x21 && uc <= 0x7e)
return uc + 0xfee0;
return uc;
}
/* 中日韩越统一表意文字 */
int isuchiness(ucs4_t uc)
{
/* 最初期统一汉字 */
if (uc >= 0x4e00 && uc <= 0x9fcc)
return 1;
/* 扩展 A 区 */
if (uc >= 0x3400 && uc <= 0x4db5)
return 2;
/* 扩展 B 区 */
if (uc >= 0x20000 && uc <= 0x2a6d6)
return 3;
/* 扩展 C 区 */
if (uc >= 0x2a700 && uc <= 0x2b734)
return 4;
/* 扩展 D 区 */
if (uc >= 0x2b740 && uc <= 0x2b81f)
return 5;
/* 扩展 E 区 */
if (uc >= 0x2b820 && uc <= 0x2f7ff)
return 6;
/* 台湾兼容汉字 */
if (uc >= 0x2f800 && uc <= 0x2fa1d)
return 7;
/* 北朝鲜兼容汉字 */
if (uc >= 0xfa70 && uc <= 0xfad9)
return 8;
/* 兼容汉字 */
if (uc >= 0xf900 && uc <= 0xfa2d)
return 9;
/* 兼容汉字 */
if (uc >= 0xfa30 && uc <= 0xfa6d)
return 10;
return 0;
}
/* 中文标点 */
int isuzhpunct(ucs4_t uc)
{
if (uc >= 0x3001 && uc <= 0x3002)
return 1;
if (uc >= 0x3008 && uc <= 0x300f)
return 1;
if (uc >= 0xff01 && uc <= 0xff0f)
return 1;
if (uc >= 0xff1a && uc <= 0xff20)
return 1;
if (uc >= 0xff3b && uc <= 0xff40)
return 1;
if (uc >= 0xff5b && uc <= 0xff5e)
return 1;
if (uc >= 0x2012 && uc <= 0x201f)
return 1;
if (uc >= 0xfe41 && uc <= 0xfe44)
return 1;
if (uc >= 0xfe49 && uc <= 0xfe4f)
return 1;
if (uc >= 0x3010 && uc <= 0x3017)
return 1;
return 0;
}
/* 日文平假名 */
int isuhiragana(ucs4_t uc)
{
if (uc >= 0x3040 && uc <= 0x309f)
return 1;
return 0;
}
/* 日文片假名 */
int isukatakana(ucs4_t uc)
{
if (uc >= 0x30a0 && uc <= 0x30ff)
return 1;
if (uc >= 0x31f0 && uc <= 0x31ff)
return 2;
return 0;
}
/* 韩文 */
int isukorean(ucs4_t uc)
{
/* 韩文拼音 */
if (uc >= 0xac00 && uc <= 0xd7af)
return 1;
/* 韩文字母 */
if (uc >= 0x1100 && uc <= 0x11ff)
return 2;
/* 韩文兼容字母 */
if (uc >= 0x3130 && uc <= 0x318f)
return 3;
return 0;
}