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
|
#include <BH/String.h>
#include <BH/Util.h>
#include "Inline/Unicode.h"
uint32_t decodeUtf8(uint32_t *unit,
unsigned char *state,
unsigned char byte)
{
unsigned char type, bits;
type = lookupUtf8Type[(size_t)byte];
bits = lookupUtf8Bits[(size_t)type];
*unit = (*unit << bits) | ((0xFF >> (8 - bits)) & byte);
return *state = lookupUtf8State[(size_t)type + *state];
}
uint32_t BH_UnicodeLower(uint32_t unit)
{
size_t i;
/* In convertable range */
if (unit >= 0xFFFF)
return unit;
/* Fasttrack and search lookup table */
i = lookupLowerIndex[unit >> 8];
while (i < sizeof(lookupLower) / sizeof(struct CaseMap))
{
if (lookupLower[i].from > unit)
return unit;
if (lookupLower[i].from == unit)
return lookupLower[i].to;
i++;
}
return unit;
}
uint32_t BH_UnicodeUpper(uint32_t unit)
{
size_t i;
/* In convertable range */
if (unit >= 0xFFFF)
return unit;
/* Fasttrack and search lookup table */
i = lookupUpperIndex[unit >> 8];
while (i < sizeof(lookupUpper) / sizeof(struct CaseMap))
{
if (lookupUpper[i].from > unit)
return unit;
if (lookupUpper[i].from == unit)
return lookupUpper[i].to;
i++;
}
return unit;
}
size_t BH_UnicodeDecodeUtf8(const char *string,
size_t size,
uint32_t *unit)
{
unsigned char state;
size_t i;
state = 0;
*unit = 0;
for (i = 0; i < size; i++)
{
switch (decodeUtf8(unit, &state, string[i]))
{
case UTF8_OK: return i + 1;
case UTF8_ERROR: *unit = 0xFFFFFFFF; return (i > 0 ? i : i + 1);
default: break;
}
}
return 0;
}
size_t BH_UnicodeEncodeUtf8(uint32_t unit,
char *string)
{
size_t result;
if (unit > 0xD7FF && unit < 0xE000)
return 0;
result = 0;
if (unit < 0x80ul)
{
string[0] = unit & 0x7F;
result = 1;
}
else if (unit < 0x800ul)
{
string[0] = 0xC0 | (unit >> 6);
string[1] = 0x80 | (unit & 0x3F);
result = 2;
}
else if (unit < 0x10000ul)
{
string[0] = 0xE0 | (unit >> 12);
string[1] = 0x80 | ((unit >> 6) & 0x3F);
string[2] = 0x80 | (unit & 0x3F);
result = 3;
}
else if (unit < 0x200000ul)
{
string[0] = 0xF0 | (unit >> 18);
string[1] = 0x80 | ((unit >> 12) & 0x3F);
string[2] = 0x80 | ((unit >> 6) & 0x3F);
string[3] = 0x80 | (unit & 0x3F);
result = 4;
}
return result;
}
static int classifyUtf16(uint16_t value)
{
if (value > 0xD7FF && value < 0xDC00)
return UTF16_LOWSUR;
else if (value > 0xDBFF && value < 0xE000)
return UTF16_HIGHSUR;
return UTF16_NORMAL;
}
size_t BH_UnicodeDecodeUtf16LE(const char *string,
size_t size,
uint32_t *unit)
{
uint16_t lower, upper;
if (size < 2)
return 0;
upper = BH_Read16LEu(string);
*unit = 0xFFFFFFFF;
if (classifyUtf16(upper) == UTF16_NORMAL)
*unit = upper;
else if (classifyUtf16(upper) == UTF16_LOWSUR)
{
if (size < 4)
return 0;
lower = BH_Read16LEu(string + 2);
if (classifyUtf16(lower) == UTF16_HIGHSUR)
{
*unit = (((upper & 0x3FF) << 10) | (lower & 0x3FF)) + 0x10000;
return 4;
}
}
return 2;
}
size_t BH_UnicodeDecodeUtf16BE(const char *string,
size_t size,
uint32_t *unit)
{
uint16_t lower, upper;
if (size < 2)
return 0;
upper = BH_Read16BEu(string);
*unit = 0xFFFFFFFF;
if (classifyUtf16(upper) == UTF16_NORMAL)
*unit = upper;
else if (classifyUtf16(upper) == UTF16_LOWSUR)
{
if (size < 4)
return 0;
lower = BH_Read16BEu(string + 2);
if (classifyUtf16(lower) == UTF16_HIGHSUR)
{
*unit = (((upper & 0x3FF) << 10) | (lower & 0x3FF)) + 0x10000;
return 4;
}
}
return 2;
}
size_t BH_UnicodeEncodeUtf16LE(uint32_t unit,
char *string)
{
if (unit > 0xD7FF && unit < 0xE000)
return 0;
if (unit < 0x10000)
{
BH_Write16LEu(string, unit);
return 2;
}
else if (unit < 0x200000)
{
unit -= 0x10000;
BH_Write16LEu(string, 0xD800 | (unit >> 10));
BH_Write16LEu(string + 2, 0xDC00 | (unit & 0x3FF));
return 4;
}
return 0;
}
size_t BH_UnicodeEncodeUtf16BE(uint32_t unit,
char *string)
{
if (unit > 0xD7FF && unit < 0xE000)
return 0;
if (unit < 0x10000)
{
BH_Write16BEu(string, unit);
return 2;
}
else if (unit < 0x200000)
{
unit -= 0x10000;
BH_Write16BEu(string, 0xD800 | (unit >> 10));
BH_Write16BEu(string + 2, 0xDC00 | (unit & 0x3FF));
return 4;
}
return 0;
}
size_t BH_UnicodeDecodeUtf32LE(const char *string,
size_t size,
uint32_t *unit)
{
if (size < 4)
return 0;
*unit = BH_Read32LEu(string);
return 4;
}
size_t BH_UnicodeDecodeUtf32BE(const char *string,
size_t size,
uint32_t *unit)
{
if (size < 4)
return 0;
*unit = BH_Read32BEu(string);
return 4;
}
size_t BH_UnicodeEncodeUtf32LE(uint32_t unit,
char *string)
{
if (unit > 0x1FFFFF || (unit > 0xD7FF && unit < 0xE000))
return 0;
BH_Write32LEu(string, unit);
return 4;
}
size_t BH_UnicodeEncodeUtf32BE(uint32_t unit,
char *string)
{
if (unit > 0x1FFFFF || (unit > 0xD7FF && unit < 0xE000))
return 0;
BH_Write32BEu(string, unit);
return 4;
}
|