Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions libc/src/__support/wchar/character_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,30 @@ int CharacterConverter::push(char8_t utf8_byte) {
// Checking the first byte if first push
if (state->bytes_processed == 0 && state->total_bytes == 0) {
state->partial = static_cast<char32_t>(0);
int numOnes = cpp::countl_one(utf8_byte);
switch (numOnes) {
uint8_t numOnes = static_cast<uint8_t>(cpp::countl_one(utf8_byte));
// 1 byte total
case 0:
if (numOnes == 0) {
state->total_bytes = 1;
break;
// 2 bytes total
case 2:
state->total_bytes = 2;
utf8_byte &= 0x1F;
break;
// 3 bytes total
case 3:
state->total_bytes = 3;
utf8_byte &= 0x0F;
break;
// 4 bytes total
case 4:
state->total_bytes = 4;
utf8_byte &= 0x07;
break;
}
// 2 through 4 bytes total
else if (numOnes >= 2 && numOnes <= 4) {
state->total_bytes = numOnes;
utf8_byte &= (0x7F >> numOnes);
}
// Invalid first byte
default:
else {
return -1;
}
state->partial = static_cast<char32_t>(utf8_byte);
state->bytes_processed++;
return 0;
}
// Any subsequent push
// Adding 6 more bits so need to left shift
const int shift_amount = 6;
if (cpp::countl_one(utf8_byte) == 1 && !isComplete()) {
char32_t byte = utf8_byte & 0x3F;
state->partial = state->partial << 6;
state->partial = state->partial << shift_amount;
state->partial |= byte;
state->bytes_processed++;
return 0;
Expand Down
8 changes: 5 additions & 3 deletions libc/test/src/__support/wchar/utf8_to_32_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ TEST(LlvmLibcCharacterConverterUTF8To32Test, TwoBytes) {
LIBC_NAMESPACE::internal::mbstate state;
state.bytes_processed = 0;
state.total_bytes = 0;
const char ch[2] = {static_cast<char>(0xC2), static_cast<char>(0x8E)}; // Ž
const char ch[2] = {static_cast<char>(0xC2),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dumb question: Are these static casts required? I would have expected them to be implicit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unfortunately not :( it wouldn't build without the casting

static_cast<char>(0x8E)}; // Ž car symbol

LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state);
char_conv.push(static_cast<char8_t>(ch[0]));
Expand All @@ -47,7 +48,7 @@ TEST(LlvmLibcCharacterConverterUTF8To32Test, ThreeBytes) {
state.bytes_processed = 0;
state.total_bytes = 0;
const char ch[3] = {static_cast<char>(0xE2), static_cast<char>(0x88),
static_cast<char>(0x91)}; // ∑
static_cast<char>(0x91)}; // ∑ sigma symbol

LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state);
char_conv.push(static_cast<char8_t>(ch[0]));
Expand All @@ -64,7 +65,8 @@ TEST(LlvmLibcCharacterConverterUTF8To32Test, FourBytes) {
state.bytes_processed = 0;
state.total_bytes = 0;
const char ch[4] = {static_cast<char>(0xF0), static_cast<char>(0x9F),
static_cast<char>(0xA4), static_cast<char>(0xA1)}; // 🤡
static_cast<char>(0xA4),
static_cast<char>(0xA1)}; // 🤡 clown emoji

LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state);
char_conv.push(static_cast<char8_t>(ch[0]));
Expand Down
Loading