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
int trailingBytes = trailing[static_cast<std::uint8_t>(*begin)];
if (begin + trailingBytes < end)
if (trailingBytes < std::distance(begin, end))
{
output = 0;

View File

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