38 template <
typename In>
39 In
Utf<8>::decode(In begin, In end, Uint32& output, Uint32 replacement)
42 static const int trailing[256] =
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
51 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5
53 static const Uint32 offsets[6] =
55 0x00000000, 0x00003080, 0x000E2080, 0x03C82080, 0xFA082080, 0x82082080
59 int trailingBytes = trailing[
static_cast<Uint8
>(*begin)];
60 if (begin + trailingBytes < end)
63 switch (trailingBytes)
65 case 5 : output +=
static_cast<Uint8
>(*begin++); output <<= 6;
66 case 4 : output +=
static_cast<Uint8
>(*begin++); output <<= 6;
67 case 3 : output +=
static_cast<Uint8
>(*begin++); output <<= 6;
68 case 2 : output +=
static_cast<Uint8
>(*begin++); output <<= 6;
69 case 1 : output +=
static_cast<Uint8
>(*begin++); output <<= 6;
70 case 0 : output +=
static_cast<Uint8
>(*begin++);
72 output -= offsets[trailingBytes];
86 template <
typename Out>
90 static const Uint8 firstBytes[7] =
92 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
96 if ((input > 0x0010FFFF) || ((input >= 0xD800) && (input <= 0xDBFF)))
100 *output++ = replacement;
107 int bytestoWrite = 1;
108 if (input < 0x80) bytestoWrite = 1;
109 else if (input < 0x800) bytestoWrite = 2;
110 else if (input < 0x10000) bytestoWrite = 3;
111 else if (input <= 0x0010FFFF) bytestoWrite = 4;
115 switch (bytestoWrite)
117 case 4 : bytes[3] =
static_cast<Uint8
>((input | 0x80) & 0xBF); input >>= 6;
118 case 3 : bytes[2] =
static_cast<Uint8
>((input | 0x80) & 0xBF); input >>= 6;
119 case 2 : bytes[1] =
static_cast<Uint8
>((input | 0x80) & 0xBF); input >>= 6;
120 case 1 : bytes[0] =
static_cast<Uint8
> (input | firstBytes[bytestoWrite]);
124 const Uint8* currentByte = bytes;
125 switch (bytestoWrite)
127 case 4 : *output++ = *currentByte++;
128 case 3 : *output++ = *currentByte++;
129 case 2 : *output++ = *currentByte++;
130 case 1 : *output++ = *currentByte++;
139 template <
typename In>
143 return decode(begin, end, codepoint);
148 template <
typename In>
151 std::size_t length = 0;
154 begin = next(begin, end);
163 template <
typename In,
typename Out>
164 Out
Utf<8>::fromAnsi(In begin, In end, Out output,
const std::locale& locale)
169 output = encode(codepoint, output);
177 template <
typename In,
typename Out>
183 output = encode(codepoint, output);
191 template <
typename In,
typename Out>
197 output = encode(*begin++, output);
204 template <
typename In,
typename Out>
205 Out
Utf<8>::toAnsi(In begin, In end, Out output,
char replacement,
const std::locale& locale)
210 begin = decode(begin, end, codepoint);
219 template <
typename In,
typename Out>
220 Out
Utf<8>::toWide(In begin, In end, Out output,
wchar_t replacement)
225 begin = decode(begin, end, codepoint);
234 template <
typename In,
typename Out>
242 begin = decode(begin, end, codepoint);
243 *output++ = codepoint < 256 ? static_cast<char>(codepoint) : replacement;
251 template <
typename In,
typename Out>
255 *output++ = *begin++;
262 template <
typename In,
typename Out>
268 begin = decode(begin, end, codepoint);
277 template <
typename In,
typename Out>
283 begin = decode(begin, end, codepoint);
284 *output++ = codepoint;
292 template <
typename In>
293 In
Utf<16>::decode(In begin, In end, Uint32& output, Uint32 replacement)
295 Uint16 first = *begin++;
298 if ((first >= 0xD800) && (first <= 0xDBFF))
302 Uint32 second = *begin++;
303 if ((second >= 0xDC00) && (second <= 0xDFFF))
306 output =
static_cast<Uint32
>(((first - 0xD800) << 10) + (second - 0xDC00) + 0x0010000);
311 output = replacement;
318 output = replacement;
332 template <
typename Out>
338 if ((input >= 0xD800) && (input <= 0xDFFF))
342 *output++ = replacement;
347 *output++ =
static_cast<Uint16
>(input);
350 else if (input > 0x0010FFFF)
354 *output++ = replacement;
360 *output++ =
static_cast<Uint16
>((input >> 10) + 0xD800);
361 *output++ =
static_cast<Uint16
>((input & 0x3FFUL) + 0xDC00);
369 template <
typename In>
373 return decode(begin, end, codepoint);
378 template <
typename In>
381 std::size_t length = 0;
384 begin = next(begin, end);
393 template <
typename In,
typename Out>
399 output = encode(codepoint, output);
407 template <
typename In,
typename Out>
413 output = encode(codepoint, output);
421 template <
typename In,
typename Out>
427 *output++ = *begin++;
434 template <
typename In,
typename Out>
435 Out
Utf<16>::toAnsi(In begin, In end, Out output,
char replacement,
const std::locale& locale)
440 begin = decode(begin, end, codepoint);
449 template <
typename In,
typename Out>
455 begin = decode(begin, end, codepoint);
464 template <
typename In,
typename Out>
471 *output++ = *begin < 256 ? static_cast<char>(*begin) : replacement;
480 template <
typename In,
typename Out>
486 begin = decode(begin, end, codepoint);
495 template <
typename In,
typename Out>
499 *output++ = *begin++;
506 template <
typename In,
typename Out>
512 begin = decode(begin, end, codepoint);
513 *output++ = codepoint;
521 template <
typename In>
530 template <
typename Out>
539 template <
typename In>
547 template <
typename In>
555 template <
typename In,
typename Out>
559 *output++ = decodeAnsi(*begin++, locale);
566 template <
typename In,
typename Out>
570 *output++ = decodeWide(*begin++);
577 template <
typename In,
typename Out>
583 *output++ = *begin++;
590 template <
typename In,
typename Out>
591 Out
Utf<32>::toAnsi(In begin, In end, Out output,
char replacement,
const std::locale& locale)
594 output = encodeAnsi(*begin++, output, replacement, locale);
601 template <
typename In,
typename Out>
605 output = encodeWide(*begin++, output, replacement);
612 template <
typename In,
typename Out>
619 *output++ = *begin < 256 ? static_cast<char>(*begin) : replacement;
628 template <
typename In,
typename Out>
638 template <
typename In,
typename Out>
649 template <
typename In,
typename Out>
653 *output++ = *begin++;
660 template <
typename In>
668 #if defined(SFML_SYSTEM_WINDOWS) && \
669 (defined(__GLIBCPP__) || defined (__GLIBCXX__)) && \
670 !(defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION))
674 wchar_t character = 0;
675 mbtowc(&character, &input, 1);
676 return static_cast<Uint32
>(character);
681 const std::ctype<wchar_t>& facet = std::use_facet< std::ctype<wchar_t> >(locale);
684 return static_cast<Uint32
>(facet.widen(input));
691 template <
typename In>
705 template <
typename Out>
706 Out
Utf<32>::encodeAnsi(Uint32 codepoint, Out output,
char replacement,
const std::locale& locale)
713 #if defined(SFML_SYSTEM_WINDOWS) && \
714 (defined(__GLIBCPP__) || defined (__GLIBCXX__)) && \
715 !(defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION))
720 if (wctomb(&character, static_cast<wchar_t>(codepoint)) >= 0)
721 *output++ = character;
722 else if (replacement)
723 *output++ = replacement;
730 const std::ctype<wchar_t>& facet = std::use_facet< std::ctype<wchar_t> >(locale);
733 *output++ = facet.narrow(static_cast<wchar_t>(codepoint), replacement);
742 template <
typename Out>
751 switch (
sizeof(
wchar_t))
755 *output++ =
static_cast<wchar_t>(codepoint);
761 if ((codepoint <= 0xFFFF) && ((codepoint < 0xD800) || (codepoint > 0xDFFF)))
763 *output++ =
static_cast<wchar_t>(codepoint);
765 else if (replacement)
767 *output++ = replacement;