BAN: Return UTF::invalid from byte_length instead of 0

This commit is contained in:
Bananymous 2025-06-02 10:46:15 +03:00
parent 4656b11256
commit e9f8471a28
4 changed files with 9 additions and 6 deletions

View File

@ -18,7 +18,7 @@ namespace BAN::UTF8
return 3;
if ((first_byte & 0xF8) == 0xF0)
return 4;
return 0;
return UTF8::invalid;
}
template<typename T> requires (sizeof(T) == 1)

View File

@ -560,9 +560,12 @@ int mblen(const char* s, size_t n)
case LOCALE_POSIX:
return 1;
case LOCALE_UTF8:
if (const auto bytes = BAN::UTF8::byte_length(*s); n >= bytes)
return bytes;
return -1;
const auto bytes = BAN::UTF8::byte_length(*s);
if (bytes == BAN::UTF8::invalid)
return -1;
if (n < bytes)
return -1;
return bytes;
}
ASSERT_NOT_REACHED();
}

View File

@ -176,7 +176,7 @@ namespace LibFont
uint32_t len = BAN::UTF8::byte_length(bytes[0]);
if (len == 0)
if (len == BAN::UTF8::invalid)
{
invalid_utf = true;
byte_index = 0;

View File

@ -748,7 +748,7 @@ Rectangle Terminal::putchar(uint8_t ch)
m_utf8_bytes[m_utf8_index++] = ch;
const size_t utf8_len = BAN::UTF8::byte_length(m_utf8_bytes[0]);
if (utf8_len == 0)
if (utf8_len == BAN::UTF8::invalid)
{
dwarnln("invalid utf8 leading byte 0x{2H}", ch);
m_utf8_index = 0;