-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc] Added isValidState to CharacterConverter class to ensure a provided mbstate is valid #145564
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 2 commits
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 |
|---|---|---|
|
|
@@ -25,6 +25,9 @@ constexpr size_t ENCODED_BITS_PER_UTF8 = 6; | |
| // Information not metadata (# of bits excluding the byte headers) | ||
| constexpr uint32_t MASK_ENCODED_BITS = | ||
| mask_trailing_ones<uint32_t, ENCODED_BITS_PER_UTF8>(); | ||
| // Maximum value for utf-32 for a utf-8 sequence of a given length | ||
| constexpr char32_t MAX_VALUE_PER_UTF8_LEN[] = {0x7f, 0x7ff, 0xffff, 0x10ffff}; | ||
| constexpr int MAX_UTF8_LENGTH = 4; | ||
|
|
||
| CharacterConverter::CharacterConverter(mbstate *mbstate) { state = mbstate; } | ||
|
|
||
|
|
@@ -40,6 +43,17 @@ bool CharacterConverter::isFull() { | |
|
|
||
| bool CharacterConverter::isEmpty() { return state->bytes_stored == 0; } | ||
|
|
||
| bool CharacterConverter::isValidState() { | ||
|
Contributor
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. this seems useful, can you use this in any of the other functions to simplify their logic?
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. I don't see an obvious place to use this in any of the other functions, especially since the single character conversion functions (mbrtowc and wcrtomb) don't require checking this. |
||
| if (state->total_bytes > 4) | ||
| return false; | ||
|
|
||
| const char32_t max_utf32_value = | ||
| state->total_bytes == 0 ? 0 | ||
| : MAX_VALUE_PER_UTF8_LEN[state->total_bytes - 1]; | ||
uzairnawaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return state->bytes_stored <= state->total_bytes && | ||
| state->bytes_stored >= 0 && state->partial <= max_utf32_value; | ||
uzairnawaz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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 | ||
|
|
@@ -90,9 +104,7 @@ int CharacterConverter::push(char32_t utf32) { | |
| state->partial = utf32; | ||
|
|
||
| // determine number of utf-8 bytes needed to represent this utf32 value | ||
| constexpr char32_t MAX_VALUE_PER_UTF8_LEN[] = {0x7f, 0x7ff, 0xffff, 0x10ffff}; | ||
| constexpr int NUM_RANGES = 4; | ||
| for (uint8_t i = 0; i < NUM_RANGES; i++) { | ||
| for (uint8_t i = 0; i < MAX_UTF8_LENGTH; i++) { | ||
| if (state->partial <= MAX_VALUE_PER_UTF8_LEN[i]) { | ||
| state->total_bytes = i + 1; | ||
| state->bytes_stored = i + 1; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.