30 template <
typename In,
typename Out>
31 inline Out Unicode::UTF32ToANSI(In Begin, In End, Out Output,
char Replacement,
const std::locale& Locale)
41 if (wctomb(&Char, static_cast<wchar_t>(*Begin++)) >= 0)
44 *Output++ = Replacement;
50 const std::ctype<wchar_t>& Facet = std::use_facet< std::ctype<wchar_t> >(Locale);
54 *Output++ = Facet.narrow(static_cast<wchar_t>(*Begin++), Replacement);
66 template <
typename In,
typename Out>
67 inline Out Unicode::ANSIToUTF32(In Begin, In End, Out Output,
const std::locale& Locale)
77 mbtowc(&Char, &*Begin, 1);
79 *Output++ =
static_cast<Uint32
>(Char);
85 const std::ctype<wchar_t>& Facet = std::use_facet< std::ctype<wchar_t> >(Locale);
89 *Output++ =
static_cast<Uint32
>(Facet.widen(*Begin++));
101 template <
typename In,
typename Out>
102 inline Out Unicode::UTF8ToUTF16(In Begin, In End, Out Output, Uint16 Replacement)
107 int TrailingBytes = UTF8TrailingBytes[
static_cast<int>(*Begin)];
108 if (Begin + TrailingBytes < End)
111 switch (TrailingBytes)
113 case 5 : c += *Begin++; c <<= 6;
114 case 4 : c += *Begin++; c <<= 6;
115 case 3 : c += *Begin++; c <<= 6;
116 case 2 : c += *Begin++; c <<= 6;
117 case 1 : c += *Begin++; c <<= 6;
118 case 0 : c += *Begin++;
120 c -= UTF8Offsets[TrailingBytes];
126 if ((c >= 0xD800) && (c <= 0xDFFF))
130 *Output++ = Replacement;
135 *Output++ =
static_cast<Uint16
>(c);
138 else if (c > 0x0010FFFF)
142 *Output++ = Replacement;
148 *Output++ =
static_cast<Uint16
>((c >> 10) + 0xD800);
149 *Output++ =
static_cast<Uint16
>((c & 0x3FFUL) + 0xDC00);
162 template <
typename In,
typename Out>
163 inline Out Unicode::UTF8ToUTF32(In Begin, In End, Out Output, Uint32 Replacement)
168 int TrailingBytes = UTF8TrailingBytes[
static_cast<int>(*Begin)];
169 if (Begin + TrailingBytes < End)
172 switch (TrailingBytes)
174 case 5 : c += *Begin++; c <<= 6;
175 case 4 : c += *Begin++; c <<= 6;
176 case 3 : c += *Begin++; c <<= 6;
177 case 2 : c += *Begin++; c <<= 6;
178 case 1 : c += *Begin++; c <<= 6;
179 case 0 : c += *Begin++;
181 c -= UTF8Offsets[TrailingBytes];
184 if ((c < 0xD800) || (c > 0xDFFF))
193 *Output++ = Replacement;
206 template <
typename In,
typename Out>
207 inline Out Unicode::UTF16ToUTF8(In Begin, In End, Out Output, Uint8 Replacement)
214 if ((c >= 0xD800) && (c <= 0xDBFF))
220 if ((d >= 0xDC00) && (d <= 0xDFFF))
221 c =
static_cast<Uint32
>(((c - 0xD800) << 10) + (d - 0xDC00) + 0x0010000);
227 *Output++ = Replacement;
236 *Output++ = Replacement;
243 int BytesToWrite = 1;
244 if (c < 0x80) BytesToWrite = 1;
245 else if (c < 0x800) BytesToWrite = 2;
246 else if (c < 0x10000) BytesToWrite = 3;
247 else if (c <= 0x0010FFFF) BytesToWrite = 4;
251 switch (BytesToWrite)
253 case 4 : Bytes[3] =
static_cast<Uint8
>((c | 0x80) & 0xBF); c >>= 6;
254 case 3 : Bytes[2] =
static_cast<Uint8
>((c | 0x80) & 0xBF); c >>= 6;
255 case 2 : Bytes[1] =
static_cast<Uint8
>((c | 0x80) & 0xBF); c >>= 6;
256 case 1 : Bytes[0] =
static_cast<Uint8
> (c | UTF8FirstBytes[BytesToWrite]);
260 const Uint8* CurByte = Bytes;
261 switch (BytesToWrite)
263 case 4 : *Output++ = *CurByte++;
264 case 3 : *Output++ = *CurByte++;
265 case 2 : *Output++ = *CurByte++;
266 case 1 : *Output++ = *CurByte++;
279 template <
typename In,
typename Out>
280 inline Out Unicode::UTF16ToUTF32(In Begin, In End, Out Output, Uint32 Replacement)
285 if ((c >= 0xD800) && (c <= 0xDBFF))
291 if ((d >= 0xDC00) && (d <= 0xDFFF))
294 *Output++ =
static_cast<Uint32
>(((c - 0xD800) << 10) + (d - 0xDC00) + 0x0010000);
300 *Output++ = Replacement;
304 else if ((c >= 0xDC00) && (c <= 0xDFFF))
308 *Output++ = Replacement;
313 *Output++ =
static_cast<Uint32
>(c);
325 template <
typename In,
typename Out>
326 inline Out Unicode::UTF32ToUTF8(In Begin, In End, Out Output, Uint8 Replacement)
335 *Output++ = Replacement;
342 int BytesToWrite = 1;
343 if (c < 0x80) BytesToWrite = 1;
344 else if (c < 0x800) BytesToWrite = 2;
345 else if (c < 0x10000) BytesToWrite = 3;
346 else if (c <= 0x0010FFFF) BytesToWrite = 4;
350 switch (BytesToWrite)
352 case 4 : Bytes[3] =
static_cast<Uint8
>((c | 0x80) & 0xBF); c >>= 6;
353 case 3 : Bytes[2] =
static_cast<Uint8
>((c | 0x80) & 0xBF); c >>= 6;
354 case 2 : Bytes[1] =
static_cast<Uint8
>((c | 0x80) & 0xBF); c >>= 6;
355 case 1 : Bytes[0] =
static_cast<Uint8
> (c | UTF8FirstBytes[BytesToWrite]);
359 const Uint8* CurByte = Bytes;
360 switch (BytesToWrite)
362 case 4 : *Output++ = *CurByte++;
363 case 3 : *Output++ = *CurByte++;
364 case 2 : *Output++ = *CurByte++;
365 case 1 : *Output++ = *CurByte++;
378 template <
typename In,
typename Out>
379 inline Out Unicode::UTF32ToUTF16(In Begin, In End, Out Output, Uint16 Replacement)
387 if ((c >= 0xD800) && (c <= 0xDFFF))
391 *Output++ = Replacement;
396 *Output++ =
static_cast<Uint16
>(c);
399 else if (c > 0x0010FFFF)
403 *Output++ = Replacement;
409 *Output++ =
static_cast<Uint16
>((c >> 10) + 0xD800);
410 *Output++ =
static_cast<Uint16
>((c & 0x3FFUL) + 0xDC00);
421 template <
typename In>
422 inline std::size_t Unicode::GetUTF8Length(In Begin, In End)
424 std::size_t Length = 0;
427 int NbBytes = UTF8TrailingBytes[
static_cast<int>(*Begin)];
428 if (Begin + NbBytes < End)
431 Begin += NbBytes + 1;
441 template <
typename In>
442 inline std::size_t Unicode::GetUTF16Length(In Begin, In End)
444 std::size_t Length = 0;
447 if ((*Begin >= 0xD800) && (*Begin <= 0xDBFF))
450 if ((Begin < End) && ((*Begin >= 0xDC00) && (*Begin <= 0xDFFF)))
470 template <
typename In>
471 inline std::size_t Unicode::GetUTF32Length(In Begin, In End)