From e68e0d9a5130e8bc0b634572b7fd44b9bfc0f8ef Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 30 Oct 2021 21:08:43 +0800 Subject: ... --- include/cru/common/String.hpp | 2 + include/cru/common/StringUtil.hpp | 78 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 2 deletions(-) (limited to 'include/cru/common') diff --git a/include/cru/common/String.hpp b/include/cru/common/String.hpp index c05ab6e0..544d24a4 100644 --- a/include/cru/common/String.hpp +++ b/include/cru/common/String.hpp @@ -171,6 +171,8 @@ class CRU_BASE_API String { } public: + void AppendCodePoint(CodePoint code_point); + Utf16CodePointIterator CodePointIterator() const { return Utf16CodePointIterator( std::u16string_view(reinterpret_cast(buffer_), size_)); diff --git a/include/cru/common/StringUtil.hpp b/include/cru/common/StringUtil.hpp index 4291a0da..cd2f4e16 100644 --- a/include/cru/common/StringUtil.hpp +++ b/include/cru/common/StringUtil.hpp @@ -2,6 +2,7 @@ #include "Base.hpp" #include +#include #include #include @@ -121,8 +122,81 @@ using Utf16CodePointIterator = void CRU_BASE_API Utf8EncodeCodePointAppend(CodePoint code_point, std::string& str); -void CRU_BASE_API Utf16EncodeCodePointAppend(CodePoint code_point, - std::u16string& str); + +namespace details { +template +inline std::enable_if_t, ReturnType> ExtractBits( + UInt n) { + return static_cast(n & ((1u << number_of_bit) - 1)); +} +} // namespace details + +template +bool Utf8EncodeCodePointAppendWithFunc(CodePoint code_point, TAppend&& append) { + auto write_continue_byte = [&append](std::uint8_t byte6) { + append((1u << 7) + (((1u << 6) - 1) & byte6)); + }; + + if (code_point >= 0 && code_point <= 0x007F) { + append(static_cast(code_point)); + return true; + } else if (code_point >= 0x0080 && code_point <= 0x07FF) { + std::uint32_t unsigned_code_point = code_point; + append( + static_cast(details::ExtractBits( + (unsigned_code_point >> 6)) + + 0b11000000)); + write_continue_byte(details::ExtractBits( + unsigned_code_point)); + return true; + } else if (code_point >= 0x0800 && code_point <= 0xFFFF) { + std::uint32_t unsigned_code_point = code_point; + append( + static_cast(details::ExtractBits( + (unsigned_code_point >> (6 * 2))) + + 0b11100000)); + write_continue_byte(details::ExtractBits( + unsigned_code_point >> 6)); + write_continue_byte(details::ExtractBits( + unsigned_code_point)); + return true; + } else if (code_point >= 0x10000 && code_point <= 0x10FFFF) { + std::uint32_t unsigned_code_point = code_point; + append( + static_cast(details::ExtractBits( + (unsigned_code_point >> (6 * 3))) + + 0b11110000)); + write_continue_byte(details::ExtractBits( + unsigned_code_point >> (6 * 2))); + write_continue_byte(details::ExtractBits( + unsigned_code_point >> 6)); + write_continue_byte(details::ExtractBits( + unsigned_code_point)); + return true; + } else { + return false; + } +} + +template +bool Utf16EncodeCodePointAppendWithFunc(CodePoint code_point, + TAppend&& append) { + if ((code_point >= 0 && code_point <= 0xD7FF) || + (code_point >= 0xE000 && code_point <= 0xFFFF)) { + append(static_cast(code_point)); + return true; + } else if (code_point >= 0x10000 && code_point <= 0x10FFFF) { + std::uint32_t u = code_point - 0x10000; + append(static_cast( + details::ExtractBits(u >> 10) + + 0xD800u)); + append(static_cast( + details::ExtractBits(u) + 0xDC00u)); + return true; + } else { + return false; + } +} std::string CRU_BASE_API ToUtf8(std::u16string_view s); std::u16string CRU_BASE_API ToUtf16(std::string_view s); -- cgit v1.2.3