Skip to content

Revert "[libc] utf8 to 32 CharacterConverter" #144446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
55 changes: 0 additions & 55 deletions libc/src/__support/wchar/character_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "hdr/types/char32_t.h"
#include "hdr/types/char8_t.h"
#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/math_extras.h"
Expand All @@ -31,49 +30,6 @@ bool CharacterConverter::isComplete() {
return state->bytes_processed == state->total_bytes;
}

int CharacterConverter::push(char8_t utf8_byte) {
uint8_t num_ones = static_cast<uint8_t>(cpp::countl_one(utf8_byte));
// Checking the first byte if first push
if (state->bytes_processed == 0) {
// UTF-8 char has 1 byte total
if (num_ones == 0) {
state->total_bytes = 1;
}
// UTF-8 char has 2 through 4 bytes total
else if (num_ones >= 2 && num_ones <= 4) {
/* Since the format is 110xxxxx, 1110xxxx, and 11110xxx for 2, 3, and 4,
we will make the base mask with 7 ones and right shift it as necessary. */
constexpr size_t SIGNIFICANT_BITS = 7;
uint32_t base_mask = mask_trailing_ones<uint32_t, SIGNIFICANT_BITS>();
state->total_bytes = num_ones;
utf8_byte &= (base_mask >> num_ones);
}
// Invalid first byte
else {
// bytes_processed and total_bytes will always be 0 here
state->partial = static_cast<char32_t>(0);
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
constexpr size_t ENCODED_BITS_PER_UTF8 = 6;
if (num_ones == 1 && !isComplete()) {
char32_t byte =
utf8_byte & mask_trailing_ones<uint32_t, ENCODED_BITS_PER_UTF8>();
state->partial = state->partial << ENCODED_BITS_PER_UTF8;
state->partial |= byte;
state->bytes_processed++;
return 0;
}
// Invalid byte -> reset the state
clear();
return -1;
}

int CharacterConverter::push(char32_t utf32) {
// we can't be partially through a conversion when pushing a utf32 value
if (!isComplete())
Expand All @@ -98,17 +54,6 @@ int CharacterConverter::push(char32_t utf32) {
return -1;
}

ErrorOr<char32_t> CharacterConverter::pop_utf32() {
// If pop is called too early, do not reset the state, use error to determine
// whether enough bytes have been pushed
if (!isComplete() || state->bytes_processed == 0)
return Error(-1);
char32_t utf32 = state->partial;
// reset if successful pop
clear();
return utf32;
}

ErrorOr<char8_t> CharacterConverter::pop_utf8() {
if (isComplete())
return Error(-1);
Expand Down
5 changes: 3 additions & 2 deletions libc/test/src/__support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,9 @@ add_subdirectory(fixed_point)
add_subdirectory(HashTable)
add_subdirectory(time)
add_subdirectory(threads)
# Requires access to uchar header which is not on MacOS
# Cannot currently build this on MacOS in overlay mode

# Requires access to uchar header which is not on macos
# Therefore, cannot currently build this on macos in overlay mode
if(NOT(LIBC_TARGET_OS_IS_DARWIN))
add_subdirectory(wchar)
endif()
10 changes: 0 additions & 10 deletions libc/test/src/__support/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
add_custom_target(libc-support-wchar-tests)

add_libc_test(
utf8_to_32_test
SUITE
libc-support-tests
SRCS
utf8_to_32_test.cpp
DEPENDS
libc.src.__support.wchar.character_converter
)

add_libc_test(
utf32_to_8_test
SUITE
Expand Down
196 changes: 0 additions & 196 deletions libc/test/src/__support/wchar/utf8_to_32_test.cpp

This file was deleted.

Loading