LibC: Implement mbrlen

This commit is contained in:
Tim Schumacher 2021-10-21 23:45:10 +02:00 committed by Brian Gianforcaro
parent 259ef76504
commit e618602433
Notes: sideshowbarker 2024-07-18 02:03:53 +09:00
2 changed files with 42 additions and 3 deletions

View file

@ -439,3 +439,38 @@ TEST_CASE(wcslcpy)
ret = wcslcpy(nullptr, L"abc", 0); ret = wcslcpy(nullptr, L"abc", 0);
EXPECT_EQ(ret, 3ul); EXPECT_EQ(ret, 3ul);
} }
TEST_CASE(mbrlen)
{
size_t ret = 0;
mbstate_t state = {};
// Ensure that we can parse normal ASCII characters.
ret = mbrlen("Hello", 5, &state);
EXPECT_EQ(ret, 1ul);
// Try two three-byte codepoints (™™), only one of which should be consumed.
ret = mbrlen("\xe2\x84\xa2\xe2\x84\xa2", 6, &state);
EXPECT_EQ(ret, 3ul);
// Try a null character, which should return 0 and reset the state to the initial state.
ret = mbrlen("\x00\x00", 2, &state);
EXPECT_EQ(ret, 0ul);
EXPECT_NE(mbsinit(&state), 0);
// Try an incomplete multibyte character.
ret = mbrlen("\xe2\x84", 2, &state);
EXPECT_EQ(ret, -2ul);
EXPECT_EQ(mbsinit(&state), 0);
// Finish the previous multibyte character.
ret = mbrlen("\xa2", 1, &state);
EXPECT_EQ(ret, 1ul);
// Try an invalid multibyte sequence.
// Reset the state afterwards because the effects are undefined.
ret = mbrlen("\xff", 1, &state);
EXPECT_EQ(ret, -1ul);
EXPECT_EQ(errno, EILSEQ);
state = {};
}

View file

@ -300,10 +300,14 @@ size_t mbrtowc(wchar_t* pwc, const char* s, size_t n, mbstate_t* state)
return consumed_bytes; return consumed_bytes;
} }
size_t mbrlen(const char*, size_t, mbstate_t*) size_t mbrlen(const char* s, size_t n, mbstate_t* ps)
{ {
dbgln("FIXME: Implement mbrlen()"); static mbstate_t anonymous_state = {};
TODO();
if (ps == nullptr)
ps = &anonymous_state;
return mbrtowc(nullptr, s, n, ps);
} }
size_t wcrtomb(char* s, wchar_t wc, mbstate_t*) size_t wcrtomb(char* s, wchar_t wc, mbstate_t*)