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
455
456
457
|
// **************************************************************************
// * This file is part of the zenXML project. It is distributed under the *
// * Boost Software License, Version 1.0. See accompanying file *
// * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt. *
// * Copyright (C) ZenJu (zhnmju123 AT gmx DOT de) - All Rights Reserved *
// **************************************************************************
#ifndef STRING_UTF8_HEADER_01832479146991573473545
#define STRING_UTF8_HEADER_01832479146991573473545
#include <cstdint>
#include <iterator>
#include "string_tools.h" //copyStringTo
namespace zen
{
//convert all(!) char- and wchar_t-based "string-like" objects applying a UTF8 conversions (but only if necessary!)
template <class TargetString, class SourceString>
TargetString utfCvrtTo(const SourceString& str);
const char BYTE_ORDER_MARK_UTF8[] = "\xEF\xBB\xBF";
//---- explicit conversion: wide <-> utf8 ----
template <class CharString, class WideString>
CharString wideToUtf8(const WideString& str); //example: std::string tmp = wideToUtf8<std::string>(L"abc");
template <class WideString, class CharString>
WideString utf8ToWide(const CharString& str); //std::wstring tmp = utf8ToWide<std::wstring>("abc");
//access unicode characters in UTF-encoded string (char- or wchar_t-based)
template <class UtfString>
size_t unicodeLength(const UtfString& str); //return number of code points for UTF-encoded string
template <class UtfString>
size_t findUnicodePos(const UtfString& str, size_t unicodePos); //return position of unicode char in UTF-encoded string
//----------------------- implementation ----------------------------------
namespace implementation
{
typedef std::uint_fast32_t CodePoint; //must be at least four bytes
typedef std::uint_fast16_t Char16; //we need an unsigned type
typedef unsigned char Char8;
const CodePoint CODE_POINT_MAX = 0x10ffff;
const CodePoint HIGH_SURROGATE = 0xd800;
const CodePoint HIGH_SURROGATE_MAX = 0xdbff;
const CodePoint LOW_SURROGATE = 0xdc00;
const CodePoint LOW_SURROGATE_MAX = 0xdfff;
template <class Function> inline
void codePointToUtf16(CodePoint cp, Function writeOutput) //"writeOutput" is a unary function taking a Char16
{
//http://en.wikipedia.org/wiki/UTF-16
assert(cp < HIGH_SURROGATE || LOW_SURROGATE_MAX < cp); //code points [0xd800, 0xdfff] are not allowed for UTF-16
assert(cp <= CODE_POINT_MAX);
if (cp < 0x10000)
writeOutput(static_cast<Char16>(cp));
else
{
cp -= 0x10000;
writeOutput(static_cast<Char16>((cp >> 10) + HIGH_SURROGATE));
writeOutput(static_cast<Char16>((cp & 0x3ff) + LOW_SURROGATE));
}
}
inline
size_t getUtf16Len(Char16 ch) //ch must be first code unit!
{
const CodePoint cp = ch;
if (HIGH_SURROGATE <= cp && cp <= HIGH_SURROGATE_MAX)
return 2;
else
{
assert(cp < LOW_SURROGATE || LOW_SURROGATE_MAX < cp); //NO low surrogate expected
return 1;
}
}
template <class CharIterator, class Function> inline
void utf16ToCodePoint(CharIterator first, CharIterator last, Function writeOutput) //"writeOutput" is a unary function taking a CodePoint
{
assert_static(sizeof(typename std::iterator_traits<CharIterator>::value_type) == 2);
for ( ; first != last; ++first)
{
CodePoint cp = static_cast<Char16>(*first);
if (HIGH_SURROGATE <= cp && cp <= HIGH_SURROGATE_MAX)
{
if (++first == last)
{
assert(false); //low surrogate expected
return;
}
assert(LOW_SURROGATE <= static_cast<Char16>(*first) && static_cast<Char16>(*first) <= LOW_SURROGATE_MAX); //low surrogate expected
cp = ((cp - HIGH_SURROGATE) << 10) + static_cast<Char16>(*first) - LOW_SURROGATE + 0x10000;
}
else
assert(cp < LOW_SURROGATE || LOW_SURROGATE_MAX < cp); //NO low surrogate expected
writeOutput(cp);
}
}
template <class Function> inline
void codePointToUtf8(CodePoint cp, Function writeOutput) //"writeOutput" is a unary function taking a Char8
{
//http://en.wikipedia.org/wiki/UTF-8
if (cp < 0x80)
writeOutput(static_cast<Char8>(cp));
else if (cp < 0x800)
{
writeOutput(static_cast<Char8>((cp >> 6 ) | 0xc0));
writeOutput(static_cast<Char8>((cp & 0x3f) | 0x80));
}
else if (cp < 0x10000)
{
writeOutput(static_cast<Char8>((cp >> 12 ) | 0xe0));
writeOutput(static_cast<Char8>(((cp >> 6) & 0x3f) | 0x80));
writeOutput(static_cast<Char8>((cp & 0x3f ) | 0x80));
}
else
{
assert(cp <= CODE_POINT_MAX);
writeOutput(static_cast<Char8>((cp >> 18 ) | 0xf0));
writeOutput(static_cast<Char8>(((cp >> 12) & 0x3f) | 0x80));
writeOutput(static_cast<Char8>(((cp >> 6) & 0x3f) | 0x80));
writeOutput(static_cast<Char8>((cp & 0x3f ) | 0x80));
}
}
inline
size_t getUtf8Len(unsigned char ch) //ch must be first code unit!
{
if (ch < 0x80)
return 1;
if (ch >> 5 == 0x6)
return 2;
if (ch >> 4 == 0xe)
return 3;
if (ch >> 3 == 0x1e)
return 4;
assert(false); //no valid begin of UTF8 encoding
return 1;
}
template <class CharIterator, class Function> inline
void utf8ToCodePoint(CharIterator first, CharIterator last, Function writeOutput) //"writeOutput" is a unary function taking a CodePoint
{
assert_static(sizeof(typename std::iterator_traits<CharIterator>::value_type) == 1);
for ( ; first != last; ++first)
{
auto getChar = [&](Char8& ch) -> bool
{
if (++first == last)
{
assert(false); //low surrogate expected
return false;
}
ch = static_cast<Char8>(*first);
assert(ch >> 6 == 0x2);
return true;
};
Char8 ch = static_cast<Char8>(*first);
switch (getUtf8Len(ch))
{
case 1:
writeOutput(ch);
break;
case 2:
{
CodePoint cp = (ch & 0x1f) << 6;
if (!getChar(ch)) return;
cp += ch & 0x3f;
writeOutput(cp);
}
break;
case 3:
{
CodePoint cp = (ch & 0xf) << 12;
if (!getChar(ch)) return;
cp += (ch & 0x3f) << 6;
if (!getChar(ch)) return;
cp += ch & 0x3f;
writeOutput(cp);
}
break;
case 4:
{
CodePoint cp = (ch & 0x7) << 18;
if (!getChar(ch)) return;
cp += (ch & 0x3f) << 12;
if (!getChar(ch)) return;
cp += (ch & 0x3f) << 6;
if (!getChar(ch)) return;
cp += ch & 0x3f;
writeOutput(cp);
}
break;
default:
assert(false);
}
}
}
template <class CharString> inline
size_t unicodeLength(const CharString& str, char) //utf8
{
typedef typename GetCharType<CharString>::Type CharType;
const CharType* strFirst = strBegin(str);
const CharType* const strLast = strFirst + strLength(str);
size_t len = 0;
while (strFirst < strLast) //[!]
{
++len;
strFirst += getUtf8Len(*strFirst); //[!]
}
return len;
}
template <class WideString> inline
size_t unicodeLengthWide(const WideString& str, Int2Type<2>) //windows: utf16-wchar_t
{
typedef typename GetCharType<WideString>::Type CharType;
const CharType* strFirst = strBegin(str);
const CharType* const strLast = strFirst + strLength(str);
size_t len = 0;
while (strFirst < strLast) //[!]
{
++len;
strFirst += getUtf16Len(*strFirst); //[!]
}
return len;
}
template <class WideString> inline
size_t unicodeLengthWide(const WideString& str, Int2Type<4>) //other OS: utf32-wchar_t
{
return strLength(str);
}
template <class WideString> inline
size_t unicodeLength(const WideString& str, wchar_t)
{
return unicodeLengthWide(str, Int2Type<sizeof(wchar_t)>());
}
}
template <class UtfString> inline
size_t unicodeLength(const UtfString& str) //return number of code points
{
return implementation::unicodeLength(str, typename GetCharType<UtfString>::Type());
}
namespace implementation
{
template <class CharString> inline
size_t findUnicodePos(const CharString& str, size_t unicodePos, char) //utf8-char
{
typedef typename GetCharType<CharString>::Type CharType;
const CharType* strFirst = strBegin(str);
const size_t strLen = strLength(str);
size_t utfPos = 0;
while (unicodePos-- > 0)
{
utfPos += getUtf8Len(strFirst[utfPos]);
if (utfPos >= strLen)
return strLen;
}
return utfPos;
}
template <class WideString> inline
size_t findUnicodePosWide(const WideString& str, size_t unicodePos, Int2Type<2>) //windows: utf16-wchar_t
{
typedef typename GetCharType<WideString>::Type CharType;
const CharType* strFirst = strBegin(str);
const size_t strLen = strLength(str);
size_t utfPos = 0;
while (unicodePos-- > 0)
{
utfPos += getUtf16Len(strFirst[utfPos]);
if (utfPos >= strLen)
return strLen;
}
return utfPos;
}
template <class WideString> inline
size_t findUnicodePosWide(const WideString& str, size_t unicodePos, Int2Type<4>) //other OS: utf32-wchar_t
{
return std::min(strLength(str), unicodePos);
}
template <class UtfString> inline
size_t findUnicodePos(const UtfString& str, size_t unicodePos, wchar_t)
{
return findUnicodePosWide(str, unicodePos, Int2Type<sizeof(wchar_t)>());
}
}
template <class UtfString> inline
size_t findUnicodePos(const UtfString& str, size_t unicodePos) //return position of unicode char in UTF-encoded string
{
return implementation::findUnicodePos(str, unicodePos, typename GetCharType<UtfString>::Type());
}
//-------------------------------------------------------------------------------------------
namespace implementation
{
template <class WideString, class CharString> inline
WideString utf8ToWide(const CharString& str, Int2Type<2>) //windows: convert utf8 to utf16-wchar_t
{
WideString output;
utf8ToCodePoint(strBegin(str), strBegin(str) + strLength(str),
[&](CodePoint cp) { codePointToUtf16(cp, [&](Char16 c) { output += static_cast<wchar_t>(c); }); });
return output;
}
template <class WideString, class CharString> inline
WideString utf8ToWide(const CharString& str, Int2Type<4>) //other OS: convert utf8 to utf32-wchar_t
{
WideString output;
utf8ToCodePoint(strBegin(str), strBegin(str) + strLength(str),
[&](CodePoint cp) { output += static_cast<wchar_t>(cp); });
return output;
}
template <class CharString, class WideString> inline
CharString wideToUtf8(const WideString& str, Int2Type<2>) //windows: convert utf16-wchar_t to utf8
{
CharString output;
utf16ToCodePoint(strBegin(str), strBegin(str) + strLength(str),
[&](CodePoint cp) { codePointToUtf8(cp, [&](char c) { output += c; }); });
return output;
}
template <class CharString, class WideString> inline
CharString wideToUtf8(const WideString& str, Int2Type<4>) //other OS: convert utf32-wchar_t to utf8
{
CharString output;
std::for_each(strBegin(str), strBegin(str) + strLength(str),
[&](CodePoint cp) { codePointToUtf8(cp, [&](char c) { output += c; }); });
return output;
}
}
template <class WideString, class CharString> inline
WideString utf8ToWide(const CharString& str)
{
assert_static((IsSameType<typename GetCharType<CharString>::Type, char >::value));
assert_static((IsSameType<typename GetCharType<WideString>::Type, wchar_t>::value));
return implementation::utf8ToWide<WideString>(str, Int2Type<sizeof(wchar_t)>());
}
template <class CharString, class WideString> inline
CharString wideToUtf8(const WideString& str)
{
assert_static((IsSameType<typename GetCharType<CharString>::Type, char >::value));
assert_static((IsSameType<typename GetCharType<WideString>::Type, wchar_t>::value));
return implementation::wideToUtf8<CharString>(str, Int2Type<sizeof(wchar_t)>());
}
//-------------------------------------------------------------------------------------------
template <class TargetString, class SourceString> inline
TargetString utfCvrtTo(const SourceString& str, char, wchar_t) { return utf8ToWide<TargetString>(str); }
template <class TargetString, class SourceString> inline
TargetString utfCvrtTo(const SourceString& str, wchar_t, char) { return wideToUtf8<TargetString>(str); }
template <class TargetString, class SourceString> inline
TargetString utfCvrtTo(const SourceString& str, char, char) { return copyStringTo<TargetString>(str); }
template <class TargetString, class SourceString> inline
TargetString utfCvrtTo(const SourceString& str, wchar_t, wchar_t) { return copyStringTo<TargetString>(str); }
template <class TargetString, class SourceString> inline
TargetString utfCvrtTo(const SourceString& str)
{
return utfCvrtTo<TargetString>(str,
typename GetCharType<SourceString>::Type(),
typename GetCharType<TargetString>::Type());
}
}
#endif //STRING_UTF8_HEADER_01832479146991573473545
|