Use std::invalid_argument in Utf8CharSizeAt

fix-squashed-planets
Evgeny Pestov 2022-02-14 17:17:13 +07:00
parent 4bce63e38d
commit d9e26c2516
2 changed files with 14 additions and 2 deletions

View File

@ -184,7 +184,12 @@ int StrUtils::Utf8CharSizeAt(const std::string &str, unsigned int pos)
if((c & 0b1111'1000) == 0b1111'0000) if((c & 0b1111'1000) == 0b1111'0000)
return 4; return 4;
return 1; // Invalid char - unexpected continuation byte
if (isUtf8ContinuationByte(c))
throw std::invalid_argument("Unexpected UTF-8 continuation byte");
// (c & 0b1111'1000) == 0b1111'1000 is true here
throw std::invalid_argument("Byte value has no sense in UTF-8");
} }
std::size_t StrUtils::Utf8StringLength(const std::string &str) std::size_t StrUtils::Utf8StringLength(const std::string &str)

View File

@ -937,7 +937,14 @@ int CText::GetCharSizeAt(Gfx::FontType font, const std::string& text, unsigned i
} }
else else
{ {
len = StrUtils::Utf8CharSizeAt(text, index); try
{
len = StrUtils::Utf8CharSizeAt(text, index);
}
catch (std::invalid_argument &e)
{
len = 1;
}
} }
return len; return len;
} }