Fix condition for trailing bytes count in UTF-8 decoder. Test added to check if a replacement characters is added to output.

This commit is contained in:
Jan Wojciechowski 2023-03-05 09:40:34 +01:00 committed by Lukas Dürrenberger
parent 230c6a4d57
commit 692fe84331
2 changed files with 25 additions and 13 deletions

View File

@ -69,7 +69,7 @@ In Utf<8>::decode(In begin, In end, std::uint32_t& output, std::uint32_t replace
// decode the character // decode the character
int trailingBytes = trailing[static_cast<std::uint8_t>(*begin)]; int trailingBytes = trailing[static_cast<std::uint8_t>(*begin)];
if (begin + trailingBytes < end) if (trailingBytes < std::distance(begin, end))
{ {
output = 0; output = 0;

View File

@ -230,18 +230,30 @@ TEST_CASE("[System] sf::String")
SUBCASE("fromUtf8()") SUBCASE("fromUtf8()")
{ {
constexpr std::array<std::uint8_t, 4> characters{'w', 'x', 'y', 'z'}; SUBCASE("Nominal")
const sf::String string = sf::String::fromUtf8(characters.begin(), characters.end()); {
CHECK(std::string(string) == "wxyz"s); constexpr std::array<std::uint8_t, 4> characters{'w', 'x', 'y', 'z'};
CHECK(std::wstring(string) == L"wxyz"s); const sf::String string = sf::String::fromUtf8(characters.begin(), characters.end());
CHECK(string.toAnsiString() == "wxyz"s); CHECK(std::string(string) == "wxyz"s);
CHECK(string.toWideString() == L"wxyz"s); CHECK(std::wstring(string) == L"wxyz"s);
CHECK(string.toUtf8() == std::basic_string<std::uint8_t>{'w', 'x', 'y', 'z'}); CHECK(string.toAnsiString() == "wxyz"s);
CHECK(string.toUtf16() == u"wxyz"s); CHECK(string.toWideString() == L"wxyz"s);
CHECK(string.toUtf32() == U"wxyz"s); CHECK(string.toUtf8() == std::basic_string<std::uint8_t>{'w', 'x', 'y', 'z'});
CHECK(string.getSize() == 4); CHECK(string.toUtf16() == u"wxyz"s);
CHECK(!string.isEmpty()); CHECK(string.toUtf32() == U"wxyz"s);
CHECK(string.getData() != nullptr); CHECK(string.getSize() == 4);
CHECK(!string.isEmpty());
CHECK(string.getData() != nullptr);
}
SUBCASE("Insufficient input")
{
constexpr std::array<std::uint8_t, 1> characters{251};
const sf::String string = sf::String::fromUtf8(characters.begin(), characters.end());
constexpr char32_t defaultReplacementCharacter = 0;
CHECK(string.getSize() == 1);
CHECK(string[0] == defaultReplacementCharacter);
}
} }
SUBCASE("fromUtf16()") SUBCASE("fromUtf16()")