This repository was archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutf8.c
More file actions
423 lines (370 loc) · 10.7 KB
/
utf8.c
File metadata and controls
423 lines (370 loc) · 10.7 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
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
/**
* Copyright (c) 2016 Richard Selneck (@TheCodeInside)
*
* 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 "utf8.h"
#include <string.h> // For memcpy
/* Get a casting helper macro */
#if defined(__cplusplus)
#define UTF8_CAST(Type, Expr) static_cast<Type>(Expr)
#else
#define UTF8_CAST(Type, Expr) (Type)(Expr)
#endif
/* Declare an instance of the UTF8_BOM */
static const char* const UTF8_BOM_INST = UTF8_BOM;
/**
* The iterator type used for iterating strings.
*/
typedef struct utf8_iterator_t
{
const char* str;
size_t str_len;
const char* iter;
utf8_codepoint_t cp;
int32_t cp_mask;
size_t cp_len;
} utf8_iterator_t;
/**
* An enumeration of UTF-8 iterator statuses.
*/
typedef enum utf8_iterator_status_t
{
UTF8_ITERATOR_END = 0,
UTF8_ITERATOR_OK = 1,
UTF8_ITERATOR_INVALID_CODEPOINT
} utf8_iterator_status_t;
/**
* \brief Initializes an iterator.
*
* \param iter The iterator.
* \param str The string to iterate.
*/
void utf8_iterate_begin(utf8_iterator_t* iter, const char* str);
/**
* \brief Moves the iterator to the next character.
*
* \param iter The iterator.
* \return A utf8_iterator_status_t describing the iterator's status.
*/
int32_t utf8_iterate_move(utf8_iterator_t* iter);
/**
* \brief Gets information about the given codepoint.
*
* \param str The codepoint string.
* \param mask [out] The mask to use when building the codepoint.
* \param length [out] The number of bytes expected for the codepoint.
*/
bool_t utf8_get_codepoint_info(const char* str, int32_t* mask, int32_t* length)
{
uint8_t ch = UTF8_CAST(uint8_t, *str);
// TODO - Optimize this into a loop
if (ch < 128)
{
*mask = 0b01111111;
*length = 1;
}
else if ((ch & 0b11100000) == 0b11000000)
{
*mask = 0b00011111;
*length = 2;
}
else if ((ch & 0b11110000) == 0b11100000)
{
*mask = 0b00001111;
*length = 3;
}
else if ((ch & 0b11111000) == 0b11110000)
{
*mask = 0b00000111;
*length = 4;
}
else if ((ch & 0b11111100) == 0b11111000)
{
*mask = 0b00000011;
*length = 5;
}
else if ((ch & 0b11111110) == 0b11111100)
{
*mask = 0b0000001;
*length = 6;
}
else
{
*mask = 0b0000000;
*length = UTF8_INVALID_LENGTH;
return false;
}
return true;
}
/**
* \brief Encodes the given string data into a UTF-8 codepoint.
*
* \param str The string.
* \param mask The codepoint mask retrieved from utf8_get_codepoint_info.
* \param length The codepoint length retrieved from utf8_get_codepoint_info.
* \return The encoded codepoint.
*/
utf8_codepoint_t utf8_encode_info(const char* str, int32_t mask, int32_t length)
{
utf8_codepoint_t codepoint = 0;
/* So, we at least know that the first character exists and is legit */
codepoint = *str & mask;
/* codepoint <<= (7 - length); */ /* Not necessary because of how the loop is structured */
/* All bytes after the first have the same 0b10xxxxxx format */
mask = 0b00111111;
while (--length && ++str)
{
codepoint <<= 6;
codepoint |= *str & mask;
}
/* Let's just make sure that we finished reading the full character */
if (length)
{
return UTF8_INVALID_CODEPOINT;
}
return codepoint;
}
/**
* \brief Encodes a single codepoint.
*
* \param str The containing the codepoint.
* \return The encoded codepoint. Can return UTF8_INVALID_CODEPOINT if \p str
* does not contain a valid UTF-8 codepoint.
*/
utf8_codepoint_t utf8_encode(const char* str)
{
int32_t mask = 0;
int32_t length = 0;
utf8_get_codepoint_info(str, &mask, &length);
if (length == UTF8_INVALID_LENGTH)
{
return UTF8_INVALID_CODEPOINT;
}
return utf8_encode_info(str, mask, length);
}
/**
* \brief Encodes an entire string into a series of UTF-8 codepoints.
*
* \param [in] str The string to encode.
* \param [out] length The length of the UTF-8 string.
* \return The encoded UTF-8 string, or NULL if the string is invalid.
* The returned string is "null-terminated," meaning that the
* last valid codepoint is followed by 0. This string is
* allocated via UTF8_MALLOC, and therefore should be freed
* using UTF8_FREE when possible.
*/
utf8_codepoint_t* utf8_encode_string(const char* str, size_t* length)
{
utf8_iterator_t iter;
utf8_codepoint_t* cpstr = NULL;
size_t cplen = 0;
int32_t status = 0;
/* Get the codepoint string length */
cplen = utf8_strlen(str);
if (cplen == UTF8_INVALID_STRING)
{
return NULL;
}
/* Allocate the memory for the string */
cpstr = UTF8_CAST(utf8_codepoint_t*, UTF8_MALLOC(cplen + 1));
cpstr[cplen] = 0;
/* Iterate the string */
utf8_iterate_begin(&iter, str);
while ((status = utf8_iterate_move(&iter)) == UTF8_ITERATOR_OK)
{
cpstr[iter.str_len - 1] = iter.cp;
}
*length = cplen;
return cpstr;
}
/**
* \brief Checks to see if the given string contains the UTF-8 BOM.
*
* \param str The UTF-8 string.
* \return True if the string begins with the UTF-8 BOM, otherwise false.
*/
bool_t utf8_has_bom(const char* str)
{
if (str[0] && str[1] && str[2])
{
return str[0] == UTF8_BOM_INST[0]
&& str[1] == UTF8_BOM_INST[1]
&& str[2] == UTF8_BOM_INST[2];
}
return false;
}
/**
* \brief Initializes an iterator.
*
* \param iter The iterator.
* \param str The string to iterate.
*/
void utf8_iterate_begin(utf8_iterator_t* iter, const char* str)
{
iter->str = str;
iter->str_len = 0;
iter->iter = str;
iter->cp = 0;
iter->cp_mask = 0;
iter->cp_len = 0;
}
/**
* \brief Moves the iterator to the next character.
*
* \param iter The iterator.
* \return A utf8_iterator_status_t describing the iterator's status.
*/
int32_t utf8_iterate_move(utf8_iterator_t* iter)
{
int32_t cp_len = 0;
if (!*iter->iter)
{
return UTF8_ITERATOR_END;
}
if (!utf8_get_codepoint_info(iter->iter, &iter->cp_mask, &cp_len))
{
return UTF8_ITERATOR_INVALID_CODEPOINT;
}
iter->cp_len = UTF8_CAST(size_t, cp_len);
iter->cp = utf8_encode_info(iter->iter, iter->cp_mask, iter->cp_len);
if (iter->cp == UTF8_INVALID_CODEPOINT)
{
return UTF8_ITERATOR_INVALID_CODEPOINT;
}
iter->iter += iter->cp_len;
iter->str_len++;
return UTF8_ITERATOR_OK;
}
/**
* \brief Gets the length of a UTF-8 string.
*
* \param str The string.
* \return The length of \p str. Can return UTF8_INVALID_STRING if the string is not
* valid UTF-8.
*/
size_t utf8_strlen(const char* str)
{
#if 1
utf8_iterator_t iter;
const char* str_iter = str;
int32_t status = 0;
/* Skip the BOM if it is present */
if (utf8_has_bom(str_iter))
{
str_iter += UTF8_BOM_STRLEN;
}
/* Iteration keeps track of the actual string length for us */
utf8_iterate_begin(&iter, str_iter);
while ((status = utf8_iterate_move(&iter)) == UTF8_ITERATOR_OK);
if (status == UTF8_ITERATOR_INVALID_CODEPOINT)
{
return UTF8_CAST(size_t, UTF8_INVALID_STRING);
}
return iter.str_len;
#else
const char* iter = str;
int32_t mask = 0;
int32_t length_cp = 0;
size_t length_str = 0;
utf8_codepoint_t codepoint = 0;
/* Skip the BOM if it is present */
if (utf8_has_bom(str))
{
iter += UTF8_BOM_STRLEN;
}
while (*iter)
{
// Get current character info
utf8_get_codepoint_info(iter, &mask, &length_cp);
// Ensure there's a codepoint
if (length_cp == UTF8_INVALID_LENGTH)
{
return UTF8_CAST(size_t, UTF8_INVALID_STRING);
}
// Get the codepoint and validate it
codepoint = utf8_encode_info(iter, mask, length_cp);
if (codepoint == UTF8_INVALID_CODEPOINT)
{
return UTF8_CAST(size_t, UTF8_INVALID_STRING);
}
length_str++;
iter += length_cp;
}
return length_str;
#endif
}
/**
* \brief Checks to see if the given string is valid UTF-8.
*
* \param str The string.
* \return True if the string is valid UTF-8, otherwise false.
*/
bool_t utf8_valid(const char* str)
{
#if 1
utf8_iterator_t iter;
int32_t status = 0;
utf8_iterate_begin(&iter, str);
while ((status = utf8_iterate_move(&iter)) == UTF8_ITERATOR_OK);
if (status == UTF8_ITERATOR_INVALID_CODEPOINT)
{
return false;
}
return true;
#else
const char* iter = str;
int32_t mask = 0;
int32_t length = 0;
utf8_codepoint_t codepoint = 0;
while (*iter)
{
// Get current character info
utf8_get_codepoint_info(iter, &mask, &length);
// Ensure there's a codepoint
if (length == UTF8_INVALID_LENGTH)
{
return false;
}
// Get the codepoint and validate it
codepoint = utf8_encode_info(iter, mask, length);
if (codepoint == UTF8_INVALID_CODEPOINT)
{
return false;
}
iter += length;
}
return true;
#endif
}
/**
* \brief Checks to see if the given Unicode codepoint is valid.
*
* \param codepoint The Unicode codepoint.
* \return True if the codepoint is valid, otherwise false.
*/
bool_t utf8_valid_codepoint(utf8_codepoint_t codepoint)
{
uint32_t cp = (uint32_t)codepoint;
/**
* These two checks cover:
* 1) That the codepoint is not above 0x10FFFF, which are not encodable by UTF-16, and
* 2) That the codepoint is not 0xD800 to 0xDFFF, which are UTF-16 surrogates
*/
return (cp < 0x110000)
&& ((cp < 0xD800) || (cp > 0xDFFF));
}