From 403c885d67f79c637ebcb303722adfd6a4b8195e Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Tue, 28 Dec 2021 12:46:30 +0100 Subject: Optimize UTF validation --- include/unicode.h | 95 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 65 insertions(+), 30 deletions(-) (limited to 'include/unicode.h') diff --git a/include/unicode.h b/include/unicode.h index 4064233..be91d77 100644 --- a/include/unicode.h +++ b/include/unicode.h @@ -47,12 +47,6 @@ namespace unicode::detail { using namespace std::string_literals; - template - inline bool is_utf8_followup_byte(value_type b) noexcept - { - return (b & 0b11000000) == 0b10000000; - } - template inline bool is_utf8_leading_byte(value_type byte) noexcept { @@ -65,22 +59,26 @@ namespace unicode::detail { } } + template + inline bool is_utf8_followup_byte(value_type b) noexcept + { + return (b & 0b11000000) == 0b10000000; + } + template inline bool is_utf8_sequence(value_type byte0, Tbytes... bytes) noexcept { constexpr auto n{sizeof...(Tbytes) + 1}; - static_assert(n <= 4); + static_assert(n <= 4, "UTF-8 sequences of 1 through 4 code units are supported"); return is_utf8_leading_byte(byte0) && - (is_utf8_followup_byte(bytes) && ...); + (... && is_utf8_followup_byte(bytes)); // left fold for linear evaluation from left to right } - template - inline bool validate_utf8(const std::basic_string& s) + template = true> + inline bool validate_utf(const std::basic_string& s) { - static_assert(sizeof(T) == 1); - int i{}; auto size{s.size()}; while (i < size) { @@ -103,6 +101,48 @@ namespace unicode::detail { return true; } + template + inline bool is_utf16_sequence(value_type word0, Twords... words) noexcept + { + constexpr auto n{sizeof...(Twords) + 1}; + + static_assert(n <= 2, "UTF-16 sequences of only 1 or 2 code units are supported"); + + if constexpr(n == 1) { + return is_valid_unicode(word0); + } else { + char16_t unit0 {static_cast(word0)}; + char16_t unit1 {static_cast((words, ...))}; + return (unit0 & 0xFC00) == 0xD800 && (unit1 & 0xFC00) == 0xDC00; + } + } + + template = true> + inline bool validate_utf(const std::basic_string& s) + { + int i{}; + auto size{s.size()}; + while (i < size) { + if (is_utf16_sequence(s[i])) { + i++; + } else if ((i < size - 1) && is_utf16_sequence(s[i], s[i + 1])) { + i += 2; + } else { + return false; + } + } + return true; + } + + template = true> + inline bool validate_utf(const std::basic_string& s) + { + for (auto i: s) + if (!is_valid_unicode(i)) + return false; + return true; + } + template inline char32_t continuation_value(value_type b) noexcept { @@ -160,7 +200,7 @@ namespace unicode::detail { } } - template::type = true> + template = true> inline internal_type calculate_value() { utf8_t byte0 {static_cast(get_code_unit<0>())}; @@ -201,7 +241,7 @@ namespace unicode::detail { } } - template::type = true> + template = true> inline internal_type calculate_value() { char16_t unit0 {static_cast(get_code_unit<0>())}; @@ -222,7 +262,7 @@ namespace unicode::detail { } } - template::type = true> + template = true> inline internal_type calculate_value() { internal_type result {static_cast(get_code_unit<0>())}; @@ -348,7 +388,7 @@ namespace unicode::detail { } } - template::type = true> + template = true> inline void append_utf(const internal_type& value) { if (value < 0x80) { // 1 byte @@ -363,7 +403,7 @@ namespace unicode::detail { throw std::runtime_error("Invalid internal Unicode value: "s + std::to_string(static_cast(value))); } - template::type = true> + template = true> inline void append_utf(const internal_type& value) { if (value <= 0xFFFF) { // expect value to be already valid Unicode values (checked in input iterator) @@ -374,7 +414,7 @@ namespace unicode::detail { } } - template::type = true> + template = true> inline void append_utf(const internal_type& value) { // expect value to be already valid Unicode values (checked in input iterator) @@ -741,12 +781,12 @@ namespace unicode { template::value, bool> = true> typename To::string_type convert(const typename From::string_type& s) { - if constexpr(sizeof(typename From::value_type) == 1 && sizeof(typename To::value_type) == 1 && std::is_same_v && std::is_same_v) { - if (validate_utf8(s)) { - if constexpr (std::is_same_v) - return s; - else - return typename To::string_type{s.begin(), s.end()}; + // if input type == output type, only validate and return input, is appropriate + if constexpr(sizeof(typename From::value_type) == sizeof(typename To::value_type) == 1 && + std::is_same_v, utf_back_insert_iterator>> && + std::is_same_v, utf_back_insert_iterator>>) { + if (validate_utf(s)) { + return s; } else { throw std::invalid_argument("Invalid UTF-8"); } @@ -848,12 +888,7 @@ namespace unicode { template::value, bool> = true> bool is_valid_utf(const typename Facet::string_type& s) { - try { - std::for_each(Facet::begin(s), Facet::end(s), [](const char32_t& c){}); - } catch (const std::invalid_argument&) { - return false; - } - return true; + return validate_utf(s); } } // namespace unicode -- cgit v1.2.3