From e68e0d9a5130e8bc0b634572b7fd44b9bfc0f8ef Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 30 Oct 2021 21:08:43 +0800 Subject: ... --- src/common/StringUtil.cpp | 59 +++++------------------------------------------ 1 file changed, 6 insertions(+), 53 deletions(-) (limited to 'src/common/StringUtil.cpp') diff --git a/src/common/StringUtil.cpp b/src/common/StringUtil.cpp index b1f1ed4b..7492bdfd 100644 --- a/src/common/StringUtil.cpp +++ b/src/common/StringUtil.cpp @@ -1,15 +1,10 @@ #include "cru/common/StringUtil.hpp" +#include #include "cru/common/Base.hpp" #include "cru/common/Exception.hpp" namespace cru { -namespace { -template -inline std::enable_if_t, ReturnType> ExtractBits( - UInt n) { - return static_cast(n & ((1u << number_of_bit) - 1)); -} -} // namespace +using details::ExtractBits; CodePoint Utf8NextCodePoint(std::string_view str, Index current, Index* next_position) { @@ -154,57 +149,15 @@ CodePoint Utf16PreviousCodePoint(std::u16string_view str, Index current, } void Utf8EncodeCodePointAppend(CodePoint code_point, std::string& str) { - auto write_continue_byte = [&str](std::uint8_t byte6) { - str.push_back((1u << 7) + (((1u << 6) - 1) & byte6)); - }; - - if (code_point >= 0 && code_point <= 0x007F) { - str.push_back(static_cast(code_point)); - } else if (code_point >= 0x0080 && code_point <= 0x07FF) { - std::uint32_t unsigned_code_point = code_point; - str.push_back(static_cast(ExtractBits( - (unsigned_code_point >> 6)) + - 0b11000000)); - write_continue_byte( - ExtractBits(unsigned_code_point)); - } else if (code_point >= 0x0800 && code_point <= 0xFFFF) { - std::uint32_t unsigned_code_point = code_point; - str.push_back(static_cast(ExtractBits( - (unsigned_code_point >> (6 * 2))) + - 0b11100000)); - write_continue_byte( - ExtractBits(unsigned_code_point >> 6)); - write_continue_byte( - ExtractBits(unsigned_code_point)); - } else if (code_point >= 0x10000 && code_point <= 0x10FFFF) { - std::uint32_t unsigned_code_point = code_point; - str.push_back(static_cast(ExtractBits( - (unsigned_code_point >> (6 * 3))) + - 0b11110000)); - write_continue_byte(ExtractBits( - unsigned_code_point >> (6 * 2))); - write_continue_byte( - ExtractBits(unsigned_code_point >> 6)); - write_continue_byte( - ExtractBits(unsigned_code_point)); - } else { + if (!Utf8EncodeCodePointAppendWithFunc(code_point, + [&str](char c) { str.push_back(c); })) throw TextEncodeException(u"Code point out of range."); - } } void Utf16EncodeCodePointAppend(CodePoint code_point, std::u16string& str) { - if ((code_point >= 0 && code_point <= 0xD7FF) || - (code_point >= 0xE000 && code_point <= 0xFFFF)) { - str.push_back(static_cast(code_point)); - } else if (code_point >= 0x10000 && code_point <= 0x10FFFF) { - std::uint32_t u = code_point - 0x10000; - str.push_back(static_cast( - ExtractBits(u >> 10) + 0xD800u)); - str.push_back(static_cast( - ExtractBits(u) + 0xDC00u)); - } else { + if (!Utf16EncodeCodePointAppendWithFunc( + code_point, [&str](char16_t c) { str.push_back(c); })) throw TextEncodeException(u"Code point out of range."); - } } std::string ToUtf8(std::u16string_view s) { -- cgit v1.2.3