-
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 11 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,8 @@ | |
|
|
||
| #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/wchar/mbstate.h" | ||
| #include "src/__support/wchar/utf_ret.h" | ||
|
|
||
|
|
@@ -22,13 +24,69 @@ bool CharacterConverter::isComplete() { | |
| return state->bytes_processed == state->total_bytes; | ||
| } | ||
|
|
||
| int CharacterConverter::push(char8_t utf8_byte) {} | ||
|
|
||
| int CharacterConverter::push(char32_t utf32) {} | ||
| 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
|
||
| int numOnes = cpp::countl_one(utf8_byte); | ||
| switch (numOnes) { | ||
| // 1 byte total | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| case 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; | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Invalid first byte | ||
| default: | ||
| 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 | ||
| if (cpp::countl_one(utf8_byte) == 1 && !isComplete()) { | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| char32_t byte = utf8_byte & 0x3F; | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| state->partial = state->partial << 6; | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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; | ||
| } | ||
|
|
||
| utf_ret<char8_t> CharacterConverter::pop_utf8() {} | ||
| 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); | ||
|
|
||
| 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,195 @@ | ||
| //===-- 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), static_cast<char>(0x8E)}; // | ||
|
|
||
| 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)}; // ∑ | ||
|
|
||
| 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)}; // 🤡 | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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.