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 --- debian/control | 5 +-- include/unicode.h | 95 +++++++++++++++++++++++++++++++++++----------------- src/test-unicode.cpp | 77 +++++++++++++++++++++++++++++++----------- 3 files changed, 126 insertions(+), 51 deletions(-) diff --git a/debian/control b/debian/control index a06886a..fcc0185 100644 --- a/debian/control +++ b/debian/control @@ -15,10 +15,11 @@ Description: Unicode conversion library UTF-8, UTF-16 and UTF-32. . Features: + - Tested on Debian 10+11, Ubuntu 2004 to 2204 + - C++17 and C++20 compatible - Additional support for ISO-8859-1 encoding (Latin-1) as subset of Unicode - Additional support for ISO-8859-15 - - Tested on Debian 10+11, Ubuntu 2004 to 2110 - - C++17 and C++20 compatible + - Header only Package: unicode-tools Architecture: any 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 diff --git a/src/test-unicode.cpp b/src/test-unicode.cpp index 2675989..99e164b 100644 --- a/src/test-unicode.cpp +++ b/src/test-unicode.cpp @@ -283,6 +283,9 @@ void test_utf_to_utf(std::tuple& t) // test facet interface result = unicode::convert::Facet, typename unicode::Encoding::Facet>(std::get(t)); BOOST_CHECK_MESSAGE(std::get(t) == result, "Facet: From " << typeid(From).name() << "(" << i << ", " << std::get(t) << ") to " << typeid(To).name() << "(" << j << ", " << std::get(t) << "), got " << result); + + // test actual results by comparing with boost::locale::conv results + BOOST_CHECK_EQUAL(result, (boost::locale::conv::utf_to_utf(std::get(t)))); // iterate over other combinations if constexpr (i + 1 < std::tuple_size::type>::value) @@ -650,26 +653,10 @@ BOOST_AUTO_TEST_CASE(convert) BOOST_CHECK((unicode::convert("äöü")) == std::u32string{U"äöü"}); -#ifdef _WIN32 - BOOST_CHECK(sizeof(wchar_t) == 2); -#else // Unix like - BOOST_CHECK(sizeof(wchar_t) == 4); -#endif - - // For the following checks, wchar_t size and encoding is system dependent: - // Windows: UTF-16 - // Linux: UTF-32 - BOOST_CHECK((unicode::convert("äöü")) == std::wstring{L"äöü"}); - BOOST_CHECK((unicode::convert("\u732b")) == std::wstring{L"\u732b"}); - BOOST_CHECK((unicode::convert("\U0001F63A")) == std::wstring{L"\U0001F63A"}); - BOOST_CHECK((unicode::convert(L"\U0001F63A")) == std::u32string{U"\U0001F63A"}); - BOOST_CHECK((unicode::convert(L"\U0001F63A")) == std::basic_string{(utf8_t*)"\U0001F63A"}); + // vector + BOOST_CHECK((unicode::convert, std::vector>(std::vector{})) == std::vector{}); + BOOST_CHECK((unicode::convert, std::vector>(std::vector{'\xc3', '\xa4', '\xc3', '\xb6', '\xc3', '\xbc'})) == (std::vector{u'ä', u'ö', u'ü'})); - BOOST_CHECK((unicode::convert(std::string{"äöü"})) == std::wstring{L"äöü"}); - - BOOST_CHECK((unicode::convert, std::vector>(std::vector{})) == std::vector{}); - BOOST_CHECK((unicode::convert, std::vector>(std::vector{'\xc3', '\xa4', '\xc3', '\xb6', '\xc3', '\xbc'})) == (std::vector{L'ä', L'ö', L'ü'})); - // deque BOOST_CHECK((unicode::convert, std::deque>(std::deque{})) == std::deque{}); BOOST_CHECK((unicode::convert, std::deque>(std::deque{'\xc3', '\xa4', '\xc3', '\xb6', '\xc3', '\xbc'})) == (std::deque{L'ä', L'ö', L'ü'})); @@ -703,6 +690,58 @@ BOOST_AUTO_TEST_CASE(convert) BOOST_CHECK((unicode::convert, std::list>(std::array{0xc3, 0xa4, 0xc3, 0xb6, 0xc3, 0xbc})) == (std::list{L'ä', L'ö', L'ü'})); } +// wchar_t specific tests: system dependent +BOOST_AUTO_TEST_CASE(convert_wstring) +{ +#ifdef _WIN32 + BOOST_CHECK(sizeof(wchar_t) == 2); +#else // Unix like + BOOST_CHECK(sizeof(wchar_t) == 4); +#endif + + // For the following checks, wchar_t size and encoding is system dependent: + // Windows: UTF-16 + // Linux: UTF-32 + BOOST_CHECK((unicode::convert("äöü")) == std::wstring{L"äöü"}); + BOOST_CHECK((unicode::convert("\u732b")) == std::wstring{L"\u732b"}); + BOOST_CHECK((unicode::convert("\U0001F63A")) == std::wstring{L"\U0001F63A"}); + BOOST_CHECK((unicode::convert(L"\U0001F63A")) == std::u32string{U"\U0001F63A"}); + BOOST_CHECK((unicode::convert(L"\U0001F63A")) == std::basic_string{(utf8_t*)"\U0001F63A"}); + + BOOST_CHECK((unicode::convert(std::string{"äöü"})) == std::wstring{L"äöü"}); + + BOOST_CHECK((unicode::convert, std::vector>(std::vector{})) == std::vector{}); + BOOST_CHECK((unicode::convert, std::vector>(std::vector{'\xc3', '\xa4', '\xc3', '\xb6', '\xc3', '\xbc'})) == (std::vector{L'ä', L'ö', L'ü'})); + + std::u16string u16_value{u"\U0001F63A"}; + std::u32string u32_value{U"\U0001F63A"}; + std::wstring w_value{L"\U0001F63A"}; + + std::u16string result_u16_value{unicode::convert(w_value)}; + std::u32string result_u32_value{unicode::convert(w_value)}; + std::wstring result_w_value_1{unicode::convert(u16_value)}; + std::wstring result_w_value_2{unicode::convert(u32_value)}; + + BOOST_CHECK_EQUAL(u16_value.size(), 2); + BOOST_CHECK_EQUAL(u32_value.size(), 1); + BOOST_CHECK_EQUAL(result_u16_value.size(), 2); + BOOST_CHECK_EQUAL(result_u32_value.size(), 1); + BOOST_CHECK_EQUAL(u16_value, result_u16_value); + BOOST_CHECK_EQUAL(u32_value, result_u32_value); + BOOST_CHECK(w_value == result_w_value_1); + BOOST_CHECK(w_value == result_w_value_2); +#ifdef _WIN32 + BOOST_CHECK_EQUAL(w_value.size(), 2); + BOOST_CHECK_EQUAL(result_w_value_1.size(), 2); + BOOST_CHECK_EQUAL(result_w_value_2.size(), 2); +#else // Unix like + BOOST_CHECK_EQUAL(w_value.size(), 1); + BOOST_CHECK_EQUAL(result_w_value_1.size(), 1); + BOOST_CHECK_EQUAL(result_w_value_2.size(), 1); +#endif + +} + BOOST_AUTO_TEST_CASE(is_valid_utf) { BOOST_CHECK(unicode::is_valid_utf(u"äöü")); -- cgit v1.2.3