-
Notifications
You must be signed in to change notification settings - Fork 7
/
string.cpp
464 lines (365 loc) · 13 KB
/
string.cpp
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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
///////////////////////////////////////////////////////////////////////////////
#include <eastl/internal/config.h>
#include <eastl/string.h>
#include <eastl/EABase/eabase.h>
#include <string.h>
namespace eastl
{
///////////////////////////////////////////////////////////////////////////////
// Converters for DecodePart
//
// For some decent documentation about conversions, see:
// http://tidy.sourceforge.net/cgi-bin/lxr/source/src/utf8.c
//
///////////////////////////////////////////////////////////////////////////////
// Requires that pDest have a capacity of at least 6 chars.
// Sets pResult to '\1' in the case that c is an invalid UCS4 char.
inline bool UCS4ToUTF8(uint32_t c, char*& pResult)
{
if(c < 0x00000080)
*pResult++ = (char)(uint8_t)c;
else if(c < 0x0800)
{
*pResult++ = (char)(uint8_t)(0xC0 | (c >> 6));
*pResult++ = (char)(uint8_t)(0x80 | (c & 0x3F));
}
else if(c <= 0x0000FFFF)
{
*pResult++ = (char)(uint8_t)(0xE0 | (c >> 12));
*pResult++ = (char)(uint8_t)(0x80 | ((c >> 6) & 0x3F));
*pResult++ = (char)(uint8_t)(0x80 | (c & 0x3F));
}
else if(c <= 0x001FFFFF)
{
*pResult++ = (char)(uint8_t)(0xF0 | (c >> 18));
*pResult++ = (char)(uint8_t)(0x80 | ((c >> 12) & 0x3F));
*pResult++ = (char)(uint8_t)(0x80 | ((c >> 6) & 0x3F));
*pResult++ = (char)(uint8_t)(0x80 | (c & 0x3F));
}
else if(c <= 0x003FFFFFF)
{
*pResult++ = (char)(uint8_t)(0xF8 | (c >> 24));
*pResult++ = (char)(uint8_t)(0x80 | (c >> 18));
*pResult++ = (char)(uint8_t)(0x80 | ((c >> 12) & 0x3F));
*pResult++ = (char)(uint8_t)(0x80 | ((c >> 6) & 0x3F));
*pResult++ = (char)(uint8_t)(0x80 | (c & 0x3F));
}
else if(c <= 0x7FFFFFFF)
{
*pResult++ = (char)(uint8_t)(0xFC | (c >> 30));
*pResult++ = (char)(uint8_t)(0x80 | ((c >> 24) & 0x3F));
*pResult++ = (char)(uint8_t)(0x80 | ((c >> 18) & 0x3F));
*pResult++ = (char)(uint8_t)(0x80 | ((c >> 12) & 0x3F));
*pResult++ = (char)(uint8_t)(0x80 | ((c >> 6) & 0x3F));
*pResult++ = (char)(uint8_t)(0x80 | (c & 0x3F));
}
else
{
// values >= 0x80000000 can't be converted to UTF8.
*pResult++ = '\1';
return false;
}
return true;
}
// Requires that pResult have a capacity of at least 3 chars.
// Sets pResult to '\1' in the case that c is an invalid UCS4 char.
inline bool UCS2ToUTF8(uint16_t c, char*& pResult)
{
return UCS4ToUTF8(c, pResult);
}
// Sets result to 0xffff in the case that the input UTF8 sequence is bad.
// 32 bit 0xffffffff is an invalid UCS4 code point, so we can't use that as an error return value.
inline bool UTF8ToUCS4(const char*& p, const char* pEnd, uint32_t& result)
{
// This could likely be implemented in a faster-executing way that uses tables.
bool success = true;
uint32_t c = 0xffff;
const char* pNext = NULL;
if(p < pEnd)
{
uint8_t cChar0((uint8_t)*p), cChar1, cChar2, cChar3;
// Asserts are disabled because we don't necessarily want to interrupt runtime execution due to this.
// EASTL_ASSERT((cChar0 != 0xFE) && (cChar0 != 0xFF)); // No byte can be 0xFE or 0xFF
// Code below will effectively catch this error as it goes.
if(cChar0 < 0x80)
{
pNext = p + 1;
c = cChar0;
}
else
{
//EASTL_ASSERT((cChar0 & 0xC0) == 0xC0); // The top two bits need to be equal to 1
if((cChar0 & 0xC0) != 0xC0)
{
success = false;
goto Failure;
}
if((cChar0 & 0xE0) == 0xC0)
{
pNext = p + 2;
if(pNext <= pEnd)
{
c = (uint32_t)((cChar0 & 0x1F) << 6);
cChar1 = static_cast<uint8_t>(p[1]);
c |= cChar1 & 0x3F;
//EASTL_ASSERT((cChar1 & 0xC0) == 0x80); // All subsequent code should be b10xxxxxx
//EASTL_ASSERT(c >= 0x0080 && c < 0x0800); // Check that we have the smallest coding
if(!((cChar1 & 0xC0) == 0x80) ||
!(c >= 0x0080 && c < 0x0800))
{
success = false;
goto Failure;
}
}
else
{
success = false;
goto Failure;
}
}
else if((cChar0 & 0xF0) == 0xE0)
{
pNext = p + 3;
if(pNext <= pEnd)
{
c = (uint32_t)((cChar0 & 0xF) << 12);
cChar1 = static_cast<uint8_t>(p[1]);
c |= (cChar1 & 0x3F) << 6;
cChar2 = static_cast<uint8_t>(p[2]);
c |= cChar2 & 0x3F;
//EASTL_ASSERT((cChar1 & 0xC0) == 0x80); // All subsequent code should be b10xxxxxx
//EASTL_ASSERT((cChar2 & 0xC0) == 0x80); // All subsequent code should be b10xxxxxx
//EASTL_ASSERT(c >= 0x00000800 && c < 0x00010000); // Check that we have the smallest coding
if(!((cChar1 & 0xC0) == 0x80) ||
!((cChar2 & 0xC0) == 0x80) ||
!(c >= 0x00000800 && c < 0x00010000))
{
success = false;
goto Failure;
}
}
else
{
success = false;
goto Failure;
}
}
else if((cChar0 & 0xF8) == 0xF0)
{
pNext = p + 4;
if(pNext <= pEnd)
{
c = (uint32_t)((cChar0 & 0x7) << 18);
cChar1 = static_cast<uint8_t>(p[1]);
c |= (uint32_t)((cChar1 & 0x3F) << 12);
cChar2 = static_cast<uint8_t>(p[2]);
c |= (cChar2 & 0x3F) << 6;
cChar3 = static_cast<uint8_t>(p[3]);
c |= cChar3 & 0x3F;
//EASTL_ASSERT((cChar0 & 0xf8) == 0xf0); // We handle the unicode but not UCS-4
//EASTL_ASSERT((cChar1 & 0xC0) == 0x80); // All subsequent code should be b10xxxxxx
//EASTL_ASSERT((cChar2 & 0xC0) == 0x80); // All subsequent code should be b10xxxxxx
//EASTL_ASSERT((cChar3 & 0xC0) == 0x80); // All subsequent code should be b10xxxxxx
//EASTL_ASSERT(c >= 0x00010000 && c <= 0x0010FFFF); // Check that we have the smallest coding, Unicode and not ucs-4
if(!((cChar0 & 0xf8) == 0xf0) ||
!((cChar1 & 0xC0) == 0x80) ||
!((cChar2 & 0xC0) == 0x80) ||
!(c >= 0x00010000 && c <= 0x0010FFFF))
{
success = false;
goto Failure;
}
}
else
{
success = false;
goto Failure;
}
}
else if((cChar0 & 0xFC) == 0xF8)
{
pNext = p + 4;
if(pNext <= pEnd)
{
// To do. We don't currently support extended UCS4 characters.
}
else
{
success = false;
goto Failure;
}
}
else if((cChar0 & 0xFE) == 0xFC)
{
pNext = p + 5;
if(pNext <= pEnd)
{
// To do. We don't currently support extended UCS4 characters.
}
else
{
success = false;
goto Failure;
}
}
else
{
success = false;
goto Failure;
}
}
}
else
success = false;
Failure:
if(success)
{
p = pNext;
result = c;
}
else
{
p = p + 1;
result = 0xffff;
}
return success;
}
// Sets result to 0xffff in the case that the input UTF8 sequence is bad.
// The effect of converting UTF8 codepoints > 0xffff to UCS2 (char16_t) is to set all
// such codepoints to 0xffff. EASTL doesn't have a concept of setting or maintaining
// error state for string conversions, though it does have a policy of converting
// impossible values to something without generating invalid strings or throwing exceptions.
inline bool UTF8ToUCS2(const char*& p, const char* pEnd, uint16_t& result)
{
uint32_t u32;
if(UTF8ToUCS4(p, pEnd, u32))
{
if(u32 <= 0xffff)
{
result = (uint16_t)u32;
return true;
}
}
result = 0xffff;
return false;
}
///////////////////////////////////////////////////////////////////////////
// DecodePart
///////////////////////////////////////////////////////////////////////////
EASTL_API bool DecodePart(const char*& pSrc, const char* pSrcEnd, char*& pDest, char* pDestEnd)
{
size_t sourceSize = (size_t)(pSrcEnd - pSrc);
size_t destSize = (size_t)(pDestEnd - pDest);
if(sourceSize > destSize)
sourceSize = destSize;
memmove(pDest, pSrc, sourceSize * sizeof(*pSrcEnd));
pSrc += sourceSize;
pDest += sourceSize; // Intentionally add sourceSize here.
return true;
}
EASTL_API bool DecodePart(const char*& pSrc, const char* pSrcEnd, char16_t*& pDest, char16_t* pDestEnd)
{
bool success = true;
while(success && (pSrc < pSrcEnd) && (pDest < pDestEnd))
success = UTF8ToUCS2(pSrc, pSrcEnd, (uint16_t&)*pDest++);
return success;
}
EASTL_API bool DecodePart(const char*& pSrc, const char* pSrcEnd, char32_t*& pDest, char32_t* pDestEnd)
{
bool success = true;
while(success && (pSrc < pSrcEnd) && (pDest < pDestEnd))
success = UTF8ToUCS4(pSrc, pSrcEnd, (uint32_t&)*pDest++);
return success;
}
EASTL_API bool DecodePart(const char16_t*& pSrc, const char16_t* pSrcEnd, char*& pDest, char* pDestEnd)
{
bool success = true;
EASTL_ASSERT((pDest + 6) < pDestEnd); // The user must provide ample buffer space, preferably 256 chars or more.
pDestEnd -= 6; // Do this so that we can avoid dest buffer size checking in the loop below and the function it calls.
while(success && (pSrc < pSrcEnd) && (pDest < pDestEnd))
success = UCS2ToUTF8(*pSrc++, pDest);
return success;
}
EASTL_API bool DecodePart(const char16_t*& pSrc, const char16_t* pSrcEnd, char16_t*& pDest, char16_t* pDestEnd)
{
size_t sourceSize = (size_t)(pSrcEnd - pSrc);
size_t destSize = (size_t)(pDestEnd - pDest);
if(sourceSize > destSize)
sourceSize = destSize;
memmove(pDest, pSrc, sourceSize * sizeof(*pSrcEnd));
pSrc += sourceSize;
pDest += sourceSize; // Intentionally add sourceSize here.
return true;
}
EASTL_API bool DecodePart(const char16_t*& pSrc, const char16_t* pSrcEnd, char32_t*& pDest, char32_t* pDestEnd)
{
size_t sourceSize = (size_t)(pSrcEnd - pSrc);
size_t destSize = (size_t)(pDestEnd - pDest);
if(sourceSize > destSize)
pSrcEnd = pSrc + destSize;
while(pSrc != pSrcEnd) // To consider: Improve this by unrolling this loop. Other tricks can improve its speed as well.
*pDest++ = (char32_t)*pSrc++;
return true;
}
EASTL_API bool DecodePart(const char32_t*& pSrc, const char32_t* pSrcEnd, char*& pDest, char* pDestEnd)
{
bool success = true;
EASTL_ASSERT((pDest + 6) < pDestEnd); // The user must provide ample buffer space, preferably 256 chars or more.
pDestEnd -= 6; // Do this so that we can avoid dest buffer size checking in the loop below and the function it calls.
while(success && (pSrc < pSrcEnd) && (pDest < pDestEnd))
success = UCS4ToUTF8(*pSrc++, pDest);
return success;
}
EASTL_API bool DecodePart(const char32_t*& pSrc, const char32_t* pSrcEnd, char16_t*& pDest, char16_t* pDestEnd)
{
size_t sourceSize = (size_t)(pSrcEnd - pSrc);
size_t destSize = (size_t)(pDestEnd - pDest);
if(sourceSize > destSize)
pSrcEnd = pSrc + destSize;
while(pSrc != pSrcEnd) // To consider: Improve this by unrolling this loop. Other tricks can improve its speed as well.
*pDest++ = (char16_t)*pSrc++; // This is potentially losing data. We are not converting to UTF16; we are converting to UCS2.
return true;
}
EASTL_API bool DecodePart(const char32_t*& pSrc, const char32_t* pSrcEnd, char32_t*& pDest, char32_t* pDestEnd)
{
size_t sourceSize = (size_t)(pSrcEnd - pSrc);
size_t destSize = (size_t)(pDestEnd - pDest);
if(sourceSize > destSize)
sourceSize = destSize;
memmove(pDest, pSrc, sourceSize * sizeof(*pSrcEnd));
pSrc += sourceSize;
pDest += sourceSize; // Intentionally add sourceSize here.
return true;
}
EASTL_API bool DecodePart(const int*& pSrc, const int* pSrcEnd, char*& pDest, char* pDestEnd)
{
bool success = true;
EASTL_ASSERT((pDest + 6) < pDestEnd); // The user must provide ample buffer space, preferably 256 chars or more.
pDestEnd -= 6; // Do this so that we can avoid dest buffer size checking in the loop below and the function it calls.
while(success && (pSrc < pSrcEnd) && (pDest < pDestEnd))
success = UCS4ToUTF8((uint32_t)(unsigned)*pSrc++, pDest);
return success;
}
EASTL_API bool DecodePart(const int*& pSrc, const int* pSrcEnd, char16_t*& pDest, char16_t* pDestEnd)
{
size_t sourceSize = (size_t)(pSrcEnd - pSrc);
size_t destSize = (size_t)(pDestEnd - pDest);
if(sourceSize > destSize)
pSrcEnd = pSrc + destSize;
while(pSrc != pSrcEnd) // To consider: Improve this by unrolling this loop. Other tricks can improve its speed as well.
*pDest++ = (char16_t)*pSrc++; // This is potentially losing data. We are not converting to UTF16; we are converting to UCS2.
return true;
}
EASTL_API bool DecodePart(const int*& pSrc, const int* pSrcEnd, char32_t*& pDest, char32_t* pDestEnd)
{
size_t sourceSize = (size_t)(pSrcEnd - pSrc);
size_t destSize = (size_t)(pDestEnd - pDest);
if(sourceSize > destSize)
pSrcEnd = pSrc + destSize;
while(pSrc != pSrcEnd) // To consider: Improve this by unrolling this loop. Other tricks can improve its speed as well.
*pDest++ = (char32_t)*pSrc++; // This is potentially losing data. We are not converting to UTF16; we are converting to UCS2.
return true;
}
} // namespace eastl