mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-12 17:30:38 +00:00
LibC: Implement mbrlen
This commit is contained in:
parent
259ef76504
commit
e618602433
Notes:
sideshowbarker
2024-07-18 02:03:53 +09:00
Author: https://github.com/timschumi Commit: https://github.com/SerenityOS/serenity/commit/e6186024332 Pull-request: https://github.com/SerenityOS/serenity/pull/10566
2 changed files with 42 additions and 3 deletions
|
@ -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 = {};
|
||||||
|
}
|
||||||
|
|
|
@ -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*)
|
||||||
|
|
Loading…
Reference in a new issue