Constrain some sf::Utf template parameters

This commit is contained in:
Chris Thrasher 2024-11-25 15:56:00 -07:00
parent dab1800f61
commit 485eb157f2
No known key found for this signature in database
GPG Key ID: 56FB686C9DFC8E2C

View File

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