Skip to content

Commit

Permalink
Use auto
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZhihao-723 committed Jun 26, 2024
1 parent d47fc77 commit ebc1c4f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions components/core/src/clp/utf8_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ auto is_ascii_char(uint8_t byte) -> bool {
}

auto is_valid_utf8_continuation_byte(uint8_t byte) -> bool {
return (byte & cContinuationByteMask) == cContinuationByte;
return (byte & cUtf8ContinuationByteMask) == cUtf8ContinuationByteHeader;
}

auto parse_continuation_byte(uint32_t code_point, uint8_t continuation_byte) -> uint32_t {
return (code_point << cNumContinuationByteCodePointBits)
+ (continuation_byte & cContinuationByteCodePointMask);
return (code_point << cUtf8NumContinuationByteCodePointBits)
+ (continuation_byte & cUtf8ContinuationByteCodePointMask);
}
} // namespace utf8_utils_internal
} // namespace clp
19 changes: 10 additions & 9 deletions components/core/src/clp/utf8_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
namespace clp {
// Constants
// Lead byte signature
constexpr uint8_t cFourByteUtf8CharHeaderMask{0xF8}; // 0b1111_1xxx
constexpr uint8_t cFourByteUtf8CharHeader{0xF0}; // 0b1111_0xxx
constexpr uint8_t cThreeByteUtf8CharHeaderMask{0xF0}; // 0b1111_xxxx
constexpr uint8_t cThreeByteUtf8CharHeader{0xE0}; // 0b1110_xxxx
constexpr uint8_t cTwoByteUtf8CharHeaderMask{0xE0}; // 0b111x_xxxx
constexpr uint8_t cTwoByteUtf8CharHeader{0xC0}; // 0b110x_xxxx
constexpr uint8_t cThreeByteUtf8CharHeaderMask{0xF0}; // 0b1111_xxxx
constexpr uint8_t cThreeByteUtf8CharHeader{0xE0}; // 0b1110_xxxx
constexpr uint8_t cFourByteUtf8CharHeaderMask{0xF8}; // 0b1111_1xxx
constexpr uint8_t cFourByteUtf8CharHeader{0xF0}; // 0b1111_0xxx

// Code point ranges (inclusive)
constexpr uint32_t cOneByteUtf8CharCodePointLowerBound{0};
Expand All @@ -26,10 +26,10 @@ constexpr uint32_t cFourByteUtf8CharCodePointLowerBound{0x1'0000};
constexpr uint32_t cFourByteUtf8CharCodePointUpperBound{0x10'FFFF};

// Continuation byte
constexpr uint32_t cContinuationByteMask{0xC0};
constexpr uint32_t cContinuationByte{0x80};
constexpr uint32_t cContinuationByteCodePointMask{0x3F};
constexpr uint8_t cNumContinuationByteCodePointBits{6};
constexpr uint32_t cUtf8ContinuationByteMask{0xC0};
constexpr uint32_t cUtf8ContinuationByteHeader{0x80};
constexpr uint32_t cUtf8ContinuationByteCodePointMask{0x3F};
constexpr uint8_t cUtf8NumContinuationByteCodePointBits{6};

/**
* Validates whether the given string is UTF-8 encoded, optionally escaping ASCII characters using
Expand Down Expand Up @@ -101,7 +101,8 @@ auto validate_utf8_string(std::string_view src, EscapeHandler escape_handler) ->
uint32_t code_point_lower_bound{};
uint32_t code_point_upper_bound{};

for (std::string_view::const_iterator it{src.cbegin()}; it != src.cend(); ++it) {
// NOLINTNEXTLINE(readability-qualified-auto)
for (auto it{src.cbegin()}; it != src.cend(); ++it) {
auto const byte{static_cast<uint8_t>(*it)};
if (0 == num_continuation_bytes_to_validate) {
if (utf8_utils_internal::is_ascii_char(byte)) {
Expand Down
5 changes: 3 additions & 2 deletions components/core/tests/test-utf8_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ auto generate_utf8_byte_sequence(uint32_t code_point, size_t num_continuation_by
while (encoded_bytes.size() < num_continuation_bytes) {
auto const least_significant_byte{static_cast<uint8_t>(code_point)};
encoded_bytes.push_back(static_cast<char>(
(least_significant_byte & ~clp::cContinuationByteMask) | clp::cContinuationByte
(least_significant_byte & ~clp::cUtf8ContinuationByteMask)
| clp::cUtf8ContinuationByteHeader
));
code_point >>= clp::cNumContinuationByteCodePointBits;
code_point >>= clp::cUtf8NumContinuationByteCodePointBits;
}

uint8_t lead_byte_code_point_mask{};
Expand Down

0 comments on commit ebc1c4f

Please sign in to comment.