-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutf.hpp
454 lines (394 loc) · 10.4 KB
/
utf.hpp
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* UTF --- UTF-8, UTF-16, UTF-32 conversion library
* Copyright (C) 2019 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
*/
#ifndef UTF_HPP_
#define UTF_HPP_ 7 // Version 7
#include "utf.h"
#ifndef __cplusplus
#error Use C++ compiler.
#endif
#include <string>
/* UTF_US8, UTF_US16, UTF_US32 --- string classes */
typedef std::string UTF_S8;
typedef std::basic_string<UTF_UC8> UTF_US8;
#if __cplusplus >= 201103L
#ifdef UTF_WIDE_IS_UTF16
typedef std::wstring UTF_US16;
#else
typedef std::u16string UTF_US16;
#endif
typedef std::u32string UTF_US32;
#else
#ifdef UTF_WIDE_IS_UTF16
typedef std::wstring UTF_US16;
#else
typedef std::basic_string<UTF_UC16> UTF_US16;
#endif
typedef std::basic_string<UTF_UC32> UTF_US32;
#endif
#ifndef UTF_OPT_
#if __cplusplus >= 201103L
#define UTF_OPT_(arg) = arg
#else
#define UTF_OPT_(arg)
#endif
#endif
template <char t_default_char UTF_OPT_(UTF_DEFAULT_CHAR)>
inline bool
UTF_u8_to_u(const UTF_US8& us8, UTF_US16& us16)
{
UTF_UC8 uc8[4];
UTF_UC16 uc16[2];
UTF_US8::const_iterator it, end = us8.end();
for (it = us8.begin(); it != end; ++it)
{
int count = UTF_uc8_count(*it);
if (!count)
{
if (!t_default_char)
return false;
us16.push_back(UTF_STATIC_CAST(UTF_UC8, t_default_char));
continue;
}
uc8[0] = *it;
for (int i = 1; i < count; ++i)
{
++it;
if (it == end)
{
if (!t_default_char)
return false;
us16.push_back(UTF_STATIC_CAST(UTF_UC8, t_default_char));
return true;
}
uc8[i] = *it;
}
if (!UTF_uc8_to_uc16(uc8, uc16))
{
if (!t_default_char)
return false;
us16.push_back(UTF_STATIC_CAST(UTF_UC8, t_default_char));
continue;
}
us16.push_back(uc16[0]);
if (uc16[1])
us16.push_back(uc16[1]);
}
return true;
}
template <char t_default_char UTF_OPT_(UTF_DEFAULT_CHAR)>
inline bool
UTF_u8_to_u(const UTF_S8& s8, UTF_US16& us16)
{
return UTF_u8_to_u<t_default_char>(reinterpret_cast<const UTF_US8&>(s8), us16);
}
template <char t_default_char UTF_OPT_(UTF_DEFAULT_CHAR)>
inline bool
UTF_u8_to_U(const UTF_US8& us8, UTF_US32& us32)
{
UTF_UC8 uc8[4];
UTF_UC32 uc32;
UTF_US8::const_iterator it, end = us8.end();
for (it = us8.begin(); it != end; ++it)
{
int count = UTF_uc8_count(UTF_STATIC_CAST(UTF_UC8, *it));
if (!count)
{
if (!t_default_char)
return false;
us32.push_back(UTF_STATIC_CAST(UTF_UC8, t_default_char));
continue;
}
uc8[0] = *it;
for (int i = 1; i < count; ++i)
{
++it;
if (it == end)
{
if (!t_default_char)
return false;
us32.push_back(UTF_STATIC_CAST(UTF_UC8, t_default_char));
return true;
}
uc8[i] = *it;
}
if (!UTF_uc8_to_uc32(uc8, &uc32))
{
if (!t_default_char)
return false;
us32.push_back(UTF_STATIC_CAST(UTF_UC8, t_default_char));
continue;
}
us32.push_back(uc32);
}
return true;
}
template <char t_default_char UTF_OPT_(UTF_DEFAULT_CHAR)>
inline bool
UTF_u8_to_U(const UTF_S8& s8, UTF_US32& us32)
{
return UTF_u8_to_U<t_default_char>(reinterpret_cast<const UTF_US8&>(s8), us32);
}
template <char t_default_char UTF_OPT_(UTF_DEFAULT_CHAR)>
inline bool
UTF_u_to_u8(const UTF_US16& us16, UTF_US8& us8)
{
UTF_UC16 uc16[2];
UTF_UC8 uc8[4];
UTF_US16::const_iterator it, end = us16.end();
for (it = us16.begin(); it != end; ++it)
{
if (UTF_uc16_is_surrogate_high(*it))
{
uc16[0] = *it;
++it;
if (it == end)
{
if (!t_default_char)
return false;
us8.push_back(UTF_STATIC_CAST(UTF_UC8, t_default_char));
return true;
}
uc16[1] = *it;
}
else
{
uc16[0] = *it;
uc16[1] = 0;
}
if (!UTF_uc16_to_uc8(uc16, uc8))
{
if (!t_default_char)
return false;
us8.push_back(UTF_STATIC_CAST(UTF_UC8, t_default_char));
continue;
}
us8.push_back(uc8[0]);
if (uc8[1])
{
us8.push_back(uc8[1]);
if (uc8[2])
{
us8.push_back(uc8[2]);
if (uc8[3])
{
us8.push_back(uc8[3]);
}
}
}
}
return true;
}
template <char t_default_char UTF_OPT_(UTF_DEFAULT_CHAR)>
inline bool
UTF_u_to_u8(const UTF_US16& us16, UTF_S8& s8)
{
return UTF_u_to_u8<t_default_char>(us16, reinterpret_cast<UTF_US8&>(s8));
}
template <char t_default_char UTF_OPT_(UTF_DEFAULT_CHAR)>
inline bool
UTF_u_to_U(const UTF_US16& us16, UTF_US32& us32)
{
UTF_UC16 uc16[2];
UTF_UC32 uc32;
UTF_US16::const_iterator it, end = us16.end();
for (it = us16.begin(); it != end; ++it)
{
if (UTF_uc16_is_surrogate_high(*it))
{
uc16[0] = *it;
++it;
if (it == end)
{
if (!t_default_char)
return false;
us32.push_back(UTF_STATIC_CAST(UTF_UC8, t_default_char));
return true;
}
uc16[1] = *it;
}
else
{
uc16[0] = *it;
uc16[1] = 0;
}
if (!UTF_uc16_to_uc32(uc16, &uc32))
{
if (!t_default_char)
return false;
uc32 = UTF_STATIC_CAST(UTF_UC8, t_default_char);
}
us32.push_back(uc32);
}
return true;
}
template <char t_default_char UTF_OPT_(UTF_DEFAULT_CHAR)>
inline bool
UTF_U_to_u8(const UTF_US32& us32, UTF_US8& us8)
{
UTF_UC8 uc8[4];
UTF_US32::const_iterator it, end = us32.end();
for (it = us32.begin(); it != end; ++it)
{
if (!UTF_uc32_to_uc8(*it, uc8))
{
if (!t_default_char)
return false;
us8.push_back(UTF_STATIC_CAST(UTF_UC8, t_default_char));
continue;
}
us8.push_back(uc8[0]);
if (uc8[1])
{
us8.push_back(uc8[1]);
if (uc8[2])
{
us8.push_back(uc8[2]);
if (uc8[3])
{
us8.push_back(uc8[3]);
}
}
}
}
return true;
}
template <char t_default_char UTF_OPT_(UTF_DEFAULT_CHAR)>
inline bool
UTF_U_to_u8(const UTF_US32& us32, UTF_S8& s8)
{
return UTF_U_to_u8<t_default_char>(us32, reinterpret_cast<UTF_US8&>(s8));
}
template <char t_default_char UTF_OPT_(UTF_DEFAULT_CHAR)>
inline bool
UTF_U_to_u(const UTF_US32& us32, UTF_US16& us16)
{
UTF_UC16 uc16[2];
UTF_US32::const_iterator it, end = us32.end();
for (it = us32.begin(); it != end; ++it)
{
if (!UTF_uc32_to_uc16(*it, uc16))
{
if (!t_default_char)
return false;
us16.push_back(UTF_STATIC_CAST(UTF_UC8, t_default_char));
continue;
}
us16.push_back(uc16[0]);
if (uc16[1])
{
us16.push_back(uc16[1]);
}
}
return true;
}
template <char t_default_char UTF_OPT_(UTF_DEFAULT_CHAR)>
inline bool
UTF_u8_to_u8(const UTF_US8& src, UTF_US8& dest)
{
dest = src;
return true;
}
template <char t_default_char UTF_OPT_(UTF_DEFAULT_CHAR)>
inline bool
UTF_u_to_u(const UTF_US16& src, UTF_US16& dest)
{
dest = src;
return true;
}
template <char t_default_char UTF_OPT_(UTF_DEFAULT_CHAR)>
inline bool
UTF_U_to_U(const UTF_US32& src, UTF_US32& dest)
{
dest = src;
return true;
}
template <typename T>
inline int UTF_cmp(const T *a, const T *b)
{
while (*a && *b)
{
if (*a < *b) return -1;
if (*a > *b) return 1;
++a;
++b;
}
if (*a < *b) return -1;
if (*a > *b) return 1;
return 0;
}
template <typename T>
inline int UTF_cmpn(const T *a, const T *b, size_t len)
{
while (*a && *b)
{
if (len-- == 0) return 0;
if (*a < *b) return -1;
if (*a > *b) return 1;
++a;
++b;
}
if (*a < *b) return -1;
if (*a > *b) return 1;
return 0;
}
template <typename UT>
inline UT *
UTF_fgets(UT *str, int count, FILE *fp)
{
size_t i, cw;
long diff;
if (count <= 0 || feof(fp))
return NULL;
cw = fread(str, sizeof(UT), UTF_STATIC_CAST(size_t, count), fp);
if (!cw)
return NULL;
for (i = 0; i < cw; ++i)
{
if (str[i] == '\n')
{
if (i && str[i - 1] == '\r')
{
str[i - 1] = '\n';
str[i] = 0;
++i;
}
else
{
if (i + 1 != count)
{
++i;
}
str[i] = 0;
}
break;
}
}
if (i == count)
{
--i;
str[i] = 0;
}
else if (i == cw && cw != count)
{
str[i] = 0;
}
if (i != cw)
{
diff = UTF_STATIC_CAST(long, i) - UTF_STATIC_CAST(long, cw);
diff *= UTF_STATIC_CAST(long, sizeof(UT));
if (fseek(fp, diff, SEEK_CUR) != 0)
return NULL;
}
return str[0] ? str : NULL;
}
#ifdef UTF_WIDE_IS_UTF16
#define UTF_L_to_U UTF_u_to_U
#define UTF_L_to_u UTF_u_to_u
#define UTF_L_to_u8 UTF_u_to_u8
#define UTF_u8_to_L UTF_u8_to_u
#define UTF_u_to_L UTF_u_to_u
#define UTF_U_to_L UTF_U_to_u
#define UTF_L_to_L UTF_u_to_u
#endif
#endif /* ndef UTF_HPP_ */