-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[libc] utf8 to 32 CharacterConverter #143973
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
Changes from 17 commits
9561ab5
54c64e0
a82f9ee
4087711
84913d8
20cd8e5
a188c4b
9535c5b
82bff49
b1e17ad
3ad6866
3323d0d
19fabd3
b46a878
077525a
80d3a83
4f59bc7
60db657
25ab213
72db39d
0a5f434
039429d
8b2e2a9
50d690b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,9 @@ | |
|
|
||
| #include "hdr/types/char32_t.h" | ||
| #include "hdr/types/char8_t.h" | ||
| #include "src/__support/CPP/bit.h" | ||
| #include "src/__support/error_or.h" | ||
| #include "src/__support/math_extras.h" | ||
| #include "src/__support/wchar/mbstate.h" | ||
| #include "src/__support/wchar/utf_ret.h" | ||
|
|
||
|
|
@@ -22,13 +25,65 @@ bool CharacterConverter::isComplete() { | |
| return state->bytes_processed == state->total_bytes; | ||
| } | ||
|
|
||
| int CharacterConverter::push(char8_t utf8_byte) {} | ||
| int CharacterConverter::push(char8_t utf8_byte) { | ||
| // Checking the first byte if first push | ||
| if (state->bytes_processed == 0 && state->total_bytes == 0) { | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| state->partial = static_cast<char32_t>(0); | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| uint8_t numOnes = static_cast<uint8_t>(cpp::countl_one(utf8_byte)); | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // 1 byte total | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (numOnes == 0) { | ||
| state->total_bytes = 1; | ||
| } | ||
| // 2 through 4 bytes total | ||
| else if (numOnes >= 2 && numOnes <= 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; | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| state->total_bytes = numOnes; | ||
| utf8_byte &= | ||
| (mask_trailing_ones<uint32_t, significant_bits>() >> numOnes); | ||
| } | ||
| // Invalid first byte | ||
| else { | ||
| return -1; | ||
sribee8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| 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; | ||
sribee8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (cpp::countl_one(utf8_byte) == 1 && !isComplete()) { | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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++; | ||
sribee8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return 0; | ||
| } | ||
| // Invalid byte -> reset if we didn't get successful complete read | ||
| if (!isComplete()) { | ||
|
||
| state->partial = static_cast<char32_t>(0); | ||
| state->bytes_processed = 0; | ||
| state->total_bytes = 0; | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| int CharacterConverter::push(char32_t utf32) {} | ||
| ErrorOr<char32_t> CharacterConverter::pop_utf32() { | ||
| // if pop is called too early | ||
| if (!isComplete()) | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return Error(-1); | ||
|
|
||
| utf_ret<char8_t> CharacterConverter::pop_utf8() {} | ||
| char32_t utf32 = state->partial; | ||
|
|
||
| utf_ret<char32_t> CharacterConverter::pop_utf32() {} | ||
| // reset if successful pop | ||
| state->bytes_processed = 0; | ||
| state->total_bytes = 0; | ||
| state->partial = static_cast<char32_t>(0); | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return utf32; | ||
| } | ||
|
|
||
| } // namespace internal | ||
| } // namespace LIBC_NAMESPACE_DECL | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| 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 | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| //===-- Unittests for character_converter utf8->utf32 ---------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/__support/error_or.h" | ||
| #include "src/__support/wchar/character_converter.h" | ||
| #include "src/__support/wchar/mbstate.h" | ||
| #include "src/__support/wchar/utf_ret.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| TEST(LlvmLibcCharacterConverterUTF8To32Test, OneByte) { | ||
| LIBC_NAMESPACE::internal::mbstate state; | ||
| state.bytes_processed = 0; | ||
| state.total_bytes = 0; | ||
| char ch = 'A'; | ||
|
|
||
| LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state); | ||
| int err = char_conv.push(static_cast<char8_t>(ch)); | ||
| auto wch = char_conv.pop_utf32(); | ||
|
|
||
| ASSERT_EQ(err, 0); | ||
| ASSERT_TRUE(wch.has_value()); | ||
| ASSERT_EQ(static_cast<int>(wch.value()), 65); | ||
| } | ||
|
|
||
| TEST(LlvmLibcCharacterConverterUTF8To32Test, TwoBytes) { | ||
| LIBC_NAMESPACE::internal::mbstate state; | ||
| state.bytes_processed = 0; | ||
| state.total_bytes = 0; | ||
| const char ch[2] = {static_cast<char>(0xC2), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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])); | ||
| char_conv.push(static_cast<char8_t>(ch[1])); | ||
| auto wch = char_conv.pop_utf32(); | ||
|
|
||
| ASSERT_TRUE(wch.has_value()); | ||
| ASSERT_EQ(static_cast<int>(wch.value()), 142); | ||
| } | ||
|
|
||
| TEST(LlvmLibcCharacterConverterUTF8To32Test, ThreeBytes) { | ||
| LIBC_NAMESPACE::internal::mbstate state; | ||
| state.bytes_processed = 0; | ||
| state.total_bytes = 0; | ||
| const char ch[3] = {static_cast<char>(0xE2), static_cast<char>(0x88), | ||
| static_cast<char>(0x91)}; // ∑ sigma symbol | ||
|
|
||
| LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state); | ||
| char_conv.push(static_cast<char8_t>(ch[0])); | ||
| char_conv.push(static_cast<char8_t>(ch[1])); | ||
| char_conv.push(static_cast<char8_t>(ch[2])); | ||
| auto wch = char_conv.pop_utf32(); | ||
|
|
||
| ASSERT_TRUE(wch.has_value()); | ||
| ASSERT_EQ(static_cast<int>(wch.value()), 8721); | ||
| } | ||
|
|
||
| TEST(LlvmLibcCharacterConverterUTF8To32Test, FourBytes) { | ||
| LIBC_NAMESPACE::internal::mbstate state; | ||
| 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)}; // 🤡 clown emoji | ||
|
|
||
| LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state); | ||
| char_conv.push(static_cast<char8_t>(ch[0])); | ||
| char_conv.push(static_cast<char8_t>(ch[1])); | ||
| char_conv.push(static_cast<char8_t>(ch[2])); | ||
| char_conv.push(static_cast<char8_t>(ch[3])); | ||
| auto wch = char_conv.pop_utf32(); | ||
|
|
||
| ASSERT_TRUE(wch.has_value()); | ||
| ASSERT_EQ(static_cast<int>(wch.value()), 129313); | ||
| } | ||
|
|
||
| TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidByte) { | ||
| LIBC_NAMESPACE::internal::mbstate state; | ||
| state.bytes_processed = 0; | ||
| state.total_bytes = 0; | ||
| const char ch = static_cast<char>(0x80); // invalid starting bit sequence | ||
|
|
||
| LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state); | ||
| int err = char_conv.push(static_cast<char8_t>(ch)); | ||
|
|
||
| ASSERT_EQ(err, -1); | ||
| } | ||
|
|
||
| TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidMultiByte) { | ||
| LIBC_NAMESPACE::internal::mbstate state; | ||
| state.bytes_processed = 0; | ||
| state.total_bytes = 0; | ||
| const char ch[4] = { | ||
| static_cast<char>(0x80), static_cast<char>(0x00), static_cast<char>(0x80), | ||
| static_cast<char>(0x00)}; // first, third, and last bytes are invalid | ||
|
|
||
| LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state); | ||
| int err = char_conv.push(static_cast<char8_t>(ch[0])); | ||
| ASSERT_EQ(err, -1); | ||
| err = char_conv.push(static_cast<char8_t>(ch[1])); | ||
| ASSERT_EQ(err, 0); | ||
| // Prev byte was single byte so trying to read another should error. | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| err = char_conv.push(static_cast<char8_t>(ch[2])); | ||
| ASSERT_EQ(err, -1); | ||
| err = char_conv.push(static_cast<char8_t>(ch[3])); | ||
| ASSERT_EQ(err, -1); | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidLastByte) { | ||
| LIBC_NAMESPACE::internal::mbstate state; | ||
| state.bytes_processed = 0; | ||
| state.total_bytes = 0; | ||
| const char ch[4] = {static_cast<char>(0xF1), static_cast<char>(0x80), | ||
| static_cast<char>(0x80), | ||
| static_cast<char>(0xC0)}; // invalid last byte | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state); | ||
| int err = char_conv.push(static_cast<char8_t>(ch[0])); | ||
| ASSERT_EQ(err, 0); | ||
| err = char_conv.push(static_cast<char8_t>(ch[1])); | ||
| ASSERT_EQ(err, 0); | ||
| err = char_conv.push(static_cast<char8_t>(ch[2])); | ||
| ASSERT_EQ(err, 0); | ||
| err = char_conv.push(static_cast<char8_t>(ch[3])); | ||
| ASSERT_EQ(err, -1); | ||
| } | ||
|
|
||
| TEST(LlvmLibcCharacterConverterUTF8To32Test, ValidTwoByteWithExtraRead) { | ||
| LIBC_NAMESPACE::internal::mbstate state; | ||
| state.bytes_processed = 0; | ||
| state.total_bytes = 0; | ||
| const char ch[3] = {static_cast<char>(0xC2), static_cast<char>(0x8E), | ||
| static_cast<char>(0x80)}; | ||
|
|
||
| LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state); | ||
| int err = char_conv.push(static_cast<char8_t>(ch[0])); | ||
| ASSERT_EQ(err, 0); | ||
| err = char_conv.push(static_cast<char8_t>(ch[1])); | ||
| ASSERT_EQ(err, 0); | ||
| // Should produce an error on 3rd byte | ||
| err = char_conv.push(static_cast<char8_t>(ch[2])); | ||
| ASSERT_EQ(err, -1); | ||
|
|
||
| auto wch = char_conv.pop_utf32(); | ||
| ASSERT_TRUE(wch.has_value()); | ||
| // Should still output the correct result. | ||
| ASSERT_EQ(static_cast<int>(wch.value()), 142); | ||
| } | ||
|
|
||
| TEST(LlvmLibcCharacterConverterUTF8To32Test, TwoValidTwoBytes) { | ||
| LIBC_NAMESPACE::internal::mbstate state; | ||
| state.bytes_processed = 0; | ||
| state.total_bytes = 0; | ||
| const char ch[4] = {static_cast<char>(0xC2), static_cast<char>(0x8E), | ||
| static_cast<char>(0xC7), static_cast<char>(0x8C)}; | ||
|
|
||
| LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state); | ||
| int err = char_conv.push(static_cast<char8_t>(ch[0])); | ||
| ASSERT_EQ(err, 0); | ||
| err = char_conv.push(static_cast<char8_t>(ch[1])); | ||
| ASSERT_EQ(err, 0); | ||
| auto wch = char_conv.pop_utf32(); | ||
| ASSERT_TRUE(wch.has_value()); | ||
| ASSERT_EQ(static_cast<int>(wch.value()), 142); | ||
|
|
||
| // Second two byte character | ||
| err = char_conv.push(static_cast<char8_t>(ch[2])); | ||
| ASSERT_EQ(err, 0); | ||
| err = char_conv.push(static_cast<char8_t>(ch[3])); | ||
| ASSERT_EQ(err, 0); | ||
| wch = char_conv.pop_utf32(); | ||
| ASSERT_TRUE(wch.has_value()); | ||
| ASSERT_EQ(static_cast<int>(wch.value()), 460); | ||
| } | ||
|
|
||
| TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidPop) { | ||
| LIBC_NAMESPACE::internal::mbstate state; | ||
| state.bytes_processed = 0; | ||
| state.total_bytes = 0; | ||
| LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state); | ||
| const char ch[2] = {static_cast<char>(0xC2), static_cast<char>(0x8E)}; | ||
| int err = char_conv.push(static_cast<char8_t>(ch[0])); | ||
| ASSERT_EQ(err, 0); | ||
| auto wch = char_conv.pop_utf32(); | ||
| ASSERT_FALSE( | ||
| wch.has_value()); // Should fail since we have not read enough bytes | ||
| err = char_conv.push(static_cast<char8_t>(ch[1])); | ||
| ASSERT_EQ(err, 0); | ||
| wch = char_conv.pop_utf32(); | ||
| ASSERT_TRUE(wch.has_value()); | ||
| ASSERT_EQ(static_cast<int>(wch.value()), 142); | ||
| } | ||
sribee8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.