diff --git a/include/SFML/System/Utf.inl b/include/SFML/System/Utf.inl index 7c9d2b69d..bca49243f 100644 --- a/include/SFML/System/Utf.inl +++ b/include/SFML/System/Utf.inl @@ -51,9 +51,13 @@ OutputIt priv::copy(InputIt first, InputIt last, OutputIt dFirst) return dFirst; } + +//////////////////////////////////////////////////////////// template In Utf<8>::decode(In begin, In end, char32_t& output, char32_t replacement) { + static_assert(sizeof(*begin) == 1, "Character type must be 1 byte wide"); + // clang-format off // Some useful precomputed data static constexpr std::array trailing = @@ -158,6 +162,8 @@ Out Utf<8>::encode(char32_t input, Out output, std::uint8_t replacement) template In Utf<8>::next(In begin, In end) { + static_assert(sizeof(*begin) == 1, "Character type must be 1 byte wide"); + char32_t codepoint = 0; return decode(begin, end, codepoint); } @@ -167,6 +173,8 @@ In Utf<8>::next(In begin, In end) template std::size_t Utf<8>::count(In begin, In end) { + static_assert(sizeof(*begin) == 1, "Character type must be 1 byte wide"); + std::size_t length = 0; while (begin != end) { @@ -182,6 +190,8 @@ std::size_t Utf<8>::count(In begin, In end) template Out Utf<8>::fromAnsi(In begin, In end, Out output, const std::locale& locale) { + static_assert(sizeof(*begin) == 1, "Character type must be 1 byte wide"); + while (begin != end) { const char32_t codepoint = Utf<32>::decodeAnsi(*begin++, locale); @@ -196,6 +206,8 @@ Out Utf<8>::fromAnsi(In begin, In end, Out output, const std::locale& locale) template Out Utf<8>::fromWide(In begin, In end, Out output) { + static_assert(sizeof(*begin) == 1, "Character type must be 1 byte wide"); + while (begin != end) { char32_t codepoint = Utf<32>::decodeWide(*begin++); @@ -210,6 +222,8 @@ Out Utf<8>::fromWide(In begin, In end, Out output) template Out Utf<8>::fromLatin1(In begin, In end, Out output) { + static_assert(sizeof(*begin) == 1, "Character type must be 1 byte wide"); + // Latin-1 is directly compatible with Unicode encodings, // and can thus be treated as (a sub-range of) UTF-32 while (begin != end) @@ -223,6 +237,8 @@ Out Utf<8>::fromLatin1(In begin, In end, Out output) template Out Utf<8>::toAnsi(In begin, In end, Out output, char replacement, const std::locale& locale) { + static_assert(sizeof(*begin) == 1, "Character type must be 1 byte wide"); + while (begin != end) { char32_t codepoint = 0; @@ -238,6 +254,8 @@ Out Utf<8>::toAnsi(In begin, In end, Out output, char replacement, const std::lo template Out Utf<8>::toWide(In begin, In end, Out output, wchar_t replacement) { + static_assert(sizeof(*begin) == 1, "Character type must be 1 byte wide"); + while (begin != end) { char32_t codepoint = 0; @@ -253,6 +271,8 @@ Out Utf<8>::toWide(In begin, In end, Out output, wchar_t replacement) template Out Utf<8>::toLatin1(In begin, In end, Out output, char replacement) { + static_assert(sizeof(*begin) == 1, "Character type must be 1 byte wide"); + // Latin-1 is directly compatible with Unicode encodings, // and can thus be treated as (a sub-range of) UTF-32 while (begin != end) @@ -270,6 +290,8 @@ Out Utf<8>::toLatin1(In begin, In end, Out output, char replacement) template Out Utf<8>::toUtf8(In begin, In end, Out output) { + static_assert(sizeof(*begin) == 1, "Character type must be 1 byte wide"); + return priv::copy(begin, end, output); } @@ -278,6 +300,8 @@ Out Utf<8>::toUtf8(In begin, In end, Out output) template Out Utf<8>::toUtf16(In begin, In end, Out output) { + static_assert(sizeof(*begin) == 1, "Character type must be 1 byte wide"); + while (begin != end) { char32_t codepoint = 0; @@ -293,6 +317,8 @@ Out Utf<8>::toUtf16(In begin, In end, Out output) template Out Utf<8>::toUtf32(In begin, In end, Out output) { + static_assert(sizeof(*begin) == 1, "Character type must be 1 byte wide"); + while (begin != end) { char32_t codepoint = 0; @@ -308,6 +334,8 @@ Out Utf<8>::toUtf32(In begin, In end, Out output) template In Utf<16>::decode(In begin, In end, char32_t& output, char32_t replacement) { + static_assert(sizeof(*begin) == 2, "Character type must be 2 bytes wide"); + const char16_t first = *begin++; // If it's a surrogate pair, first convert to a single UTF-32 character @@ -385,6 +413,8 @@ Out Utf<16>::encode(char32_t input, Out output, char16_t replacement) template In Utf<16>::next(In begin, In end) { + static_assert(sizeof(*begin) == 2, "Character type must be 2 bytes wide"); + char32_t codepoint = 0; return decode(begin, end, codepoint); } @@ -394,6 +424,8 @@ In Utf<16>::next(In begin, In end) template std::size_t Utf<16>::count(In begin, In end) { + static_assert(sizeof(*begin) == 2, "Character type must be 2 bytes wide"); + std::size_t length = 0; while (begin != end) { @@ -409,6 +441,8 @@ std::size_t Utf<16>::count(In begin, In end) template Out Utf<16>::fromAnsi(In begin, In end, Out output, const std::locale& locale) { + static_assert(sizeof(*begin) == 2, "Character type must be 2 bytes wide"); + while (begin != end) { char32_t codepoint = Utf<32>::decodeAnsi(*begin++, locale); @@ -423,6 +457,8 @@ Out Utf<16>::fromAnsi(In begin, In end, Out output, const std::locale& locale) template Out Utf<16>::fromWide(In begin, In end, Out output) { + static_assert(sizeof(*begin) == 2, "Character type must be 2 bytes wide"); + while (begin != end) { char32_t codepoint = Utf<32>::decodeWide(*begin++); @@ -437,6 +473,8 @@ Out Utf<16>::fromWide(In begin, In end, Out output) template Out Utf<16>::fromLatin1(In begin, In end, Out output) { + static_assert(sizeof(*begin) == 2, "Character type must be 2 bytes wide"); + // Latin-1 is directly compatible with Unicode encodings, // and can thus be treated as (a sub-range of) UTF-32 return priv::copy(begin, end, output); @@ -447,6 +485,8 @@ Out Utf<16>::fromLatin1(In begin, In end, Out output) template Out Utf<16>::toAnsi(In begin, In end, Out output, char replacement, const std::locale& locale) { + static_assert(sizeof(*begin) == 2, "Character type must be 2 bytes wide"); + while (begin != end) { char32_t codepoint = 0; @@ -462,6 +502,8 @@ Out Utf<16>::toAnsi(In begin, In end, Out output, char replacement, const std::l template Out Utf<16>::toWide(In begin, In end, Out output, wchar_t replacement) { + static_assert(sizeof(*begin) == 2, "Character type must be 2 bytes wide"); + while (begin != end) { char32_t codepoint = 0; @@ -477,6 +519,8 @@ Out Utf<16>::toWide(In begin, In end, Out output, wchar_t replacement) template Out Utf<16>::toLatin1(In begin, In end, Out output, char replacement) { + static_assert(sizeof(*begin) == 2, "Character type must be 2 bytes wide"); + // Latin-1 is directly compatible with Unicode encodings, // and can thus be treated as (a sub-range of) UTF-32 while (begin != end) @@ -493,6 +537,8 @@ Out Utf<16>::toLatin1(In begin, In end, Out output, char replacement) template Out Utf<16>::toUtf8(In begin, In end, Out output) { + static_assert(sizeof(*begin) == 2, "Character type must be 2 bytes wide"); + while (begin != end) { char32_t codepoint = 0; @@ -508,6 +554,8 @@ Out Utf<16>::toUtf8(In begin, In end, Out output) template Out Utf<16>::toUtf16(In begin, In end, Out output) { + static_assert(sizeof(*begin) == 2, "Character type must be 2 bytes wide"); + return priv::copy(begin, end, output); } @@ -516,6 +564,8 @@ Out Utf<16>::toUtf16(In begin, In end, Out output) template Out Utf<16>::toUtf32(In begin, In end, Out output) { + static_assert(sizeof(*begin) == 2, "Character type must be 2 bytes wide"); + while (begin != end) { char32_t codepoint = 0; @@ -531,6 +581,8 @@ Out Utf<16>::toUtf32(In begin, In end, Out output) template In Utf<32>::decode(In begin, In /*end*/, char32_t& output, char32_t /*replacement*/) { + static_assert(sizeof(*begin) == 4, "Character type must be 4 bytes wide"); + output = *begin++; return begin; } @@ -549,6 +601,8 @@ Out Utf<32>::encode(char32_t input, Out output, char32_t /*replacement*/) template In Utf<32>::next(In begin, In /*end*/) { + static_assert(sizeof(*begin) == 4, "Character type must be 4 bytes wide"); + return ++begin; } @@ -557,6 +611,8 @@ In Utf<32>::next(In begin, In /*end*/) template std::size_t Utf<32>::count(In begin, In end) { + static_assert(sizeof(*begin) == 4, "Character type must be 4 bytes wide"); + return static_cast(end - begin); } @@ -565,6 +621,8 @@ std::size_t Utf<32>::count(In begin, In end) template Out Utf<32>::fromAnsi(In begin, In end, Out output, const std::locale& locale) { + static_assert(sizeof(*begin) == 1, "Character type must be 1 byte wide"); + while (begin != end) *output++ = decodeAnsi(*begin++, locale); @@ -576,6 +634,8 @@ Out Utf<32>::fromAnsi(In begin, In end, Out output, const std::locale& locale) template Out Utf<32>::fromWide(In begin, In end, Out output) { + static_assert(sizeof(*begin) == sizeof(wchar_t), "Character type must be same size as wchar_t"); + while (begin != end) *output++ = decodeWide(*begin++); @@ -587,6 +647,8 @@ Out Utf<32>::fromWide(In begin, In end, Out output) template Out Utf<32>::fromLatin1(In begin, In end, Out output) { + static_assert(sizeof(*begin) == 4, "Character type must be 4 bytes wide"); + // Latin-1 is directly compatible with Unicode encodings, // and can thus be treated as (a sub-range of) UTF-32 return priv::copy(begin, end, output); @@ -597,6 +659,8 @@ Out Utf<32>::fromLatin1(In begin, In end, Out output) template Out Utf<32>::toAnsi(In begin, In end, Out output, char replacement, const std::locale& locale) { + static_assert(sizeof(*begin) == 4, "Character type must be 4 bytes wide"); + while (begin != end) output = encodeAnsi(*begin++, output, replacement, locale); @@ -608,6 +672,8 @@ Out Utf<32>::toAnsi(In begin, In end, Out output, char replacement, const std::l template Out Utf<32>::toWide(In begin, In end, Out output, wchar_t replacement) { + static_assert(sizeof(*begin) == 4, "Character type must be 4 bytes wide"); + while (begin != end) output = encodeWide(*begin++, output, replacement); @@ -619,6 +685,8 @@ Out Utf<32>::toWide(In begin, In end, Out output, wchar_t replacement) template Out Utf<32>::toLatin1(In begin, In end, Out output, char replacement) { + static_assert(sizeof(*begin) == 4, "Character type must be 4 bytes wide"); + // Latin-1 is directly compatible with Unicode encodings, // and can thus be treated as (a sub-range of) UTF-32 while (begin != end) @@ -635,6 +703,8 @@ Out Utf<32>::toLatin1(In begin, In end, Out output, char replacement) template Out Utf<32>::toUtf8(In begin, In end, Out output) { + static_assert(sizeof(*begin) == 4, "Character type must be 4 bytes wide"); + while (begin != end) output = Utf<8>::encode(*begin++, output); @@ -645,6 +715,8 @@ Out Utf<32>::toUtf8(In begin, In end, Out output) template Out Utf<32>::toUtf16(In begin, In end, Out output) { + static_assert(sizeof(*begin) == 4, "Character type must be 4 bytes wide"); + while (begin != end) output = Utf<16>::encode(*begin++, output); @@ -656,6 +728,8 @@ Out Utf<32>::toUtf16(In begin, In end, Out output) template Out Utf<32>::toUtf32(In begin, In end, Out output) { + static_assert(sizeof(*begin) == 4, "Character type must be 4 bytes wide"); + return priv::copy(begin, end, output); }