diff options
author | crupest <crupest@outlook.com> | 2022-01-20 22:25:16 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-01-20 22:25:16 +0800 |
commit | b68d2027fe7505c14899db9fa1496b5ace09e25f (patch) | |
tree | 8361112fdf5a245794070ae8d58279c4b91cc54b /include/cru/common | |
parent | 3c3a08a02a0f8fc56dc2da3374f025d4fdaf62c5 (diff) | |
download | cru-b68d2027fe7505c14899db9fa1496b5ace09e25f.tar.gz cru-b68d2027fe7505c14899db9fa1496b5ace09e25f.tar.bz2 cru-b68d2027fe7505c14899db9fa1496b5ace09e25f.zip |
...
Diffstat (limited to 'include/cru/common')
-rw-r--r-- | include/cru/common/Format.hpp | 3 | ||||
-rw-r--r-- | include/cru/common/String.hpp | 68 | ||||
-rw-r--r-- | include/cru/common/StringUtil.hpp | 36 |
3 files changed, 57 insertions, 50 deletions
diff --git a/include/cru/common/Format.hpp b/include/cru/common/Format.hpp index c2fd2e09..1ad71262 100644 --- a/include/cru/common/Format.hpp +++ b/include/cru/common/Format.hpp @@ -2,7 +2,8 @@ #include "Exception.hpp" #include "String.hpp" -#include "cru/common/Base.hpp" + +#include <charconv> namespace cru { inline String ToString(bool value) { diff --git a/include/cru/common/String.hpp b/include/cru/common/String.hpp index dac81e8d..1d660623 100644 --- a/include/cru/common/String.hpp +++ b/include/cru/common/String.hpp @@ -5,18 +5,10 @@ #include "StringUtil.hpp" #include <double-conversion/double-conversion.h> -#include <algorithm> -#include <array> -#include <charconv> #include <initializer_list> #include <iterator> -#include <limits> -#include <stdexcept> #include <string> #include <string_view> -#include <system_error> -#include <tuple> -#include <type_traits> #include <vector> namespace cru { @@ -24,9 +16,6 @@ class StringView; class CRU_BASE_API String { public: - static double_conversion::StringToDoubleConverter - kDefaultStringToDoubleConverter; - using value_type = char16_t; using size_type = Index; using difference_type = Index; @@ -181,24 +170,21 @@ class CRU_BASE_API String { return String(this->buffer_ + start, size); } - public: String& operator+=(value_type value) { this->append(value); return *this; } String& operator+=(StringView other); + public: operator std::u16string_view() const { return std::u16string_view(data(), size()); } + StringView View() const; + public: Index Find(value_type value, Index start = 0) const; - - String& TrimStart(); - String& TrimEnd(); - String& Trim(); - std::vector<String> Split(value_type separator, bool remove_space_line = false) const; std::vector<String> SplitToLines(bool remove_space_line = false) const; @@ -206,6 +192,10 @@ class CRU_BASE_API String { bool StartWith(StringView str) const; bool EndWith(StringView str) const; + String& TrimStart(); + String& TrimEnd(); + String& Trim(); + public: void AppendCodePoint(CodePoint code_point); @@ -218,6 +208,11 @@ class CRU_BASE_API String { Range RangeFromCodeUnitToCodePoint(Range code_unit_range) const; Range RangeFromCodePointToCodeUnit(Range code_point_range) const; + float ParseToFloat(Index* processed_characters_count = nullptr) const; + double ParseToDouble(Index* processed_characters_count = nullptr) const; + std::vector<float> ParseToFloatList(value_type separator = u' ') const; + std::vector<double> ParseToDoubleList(value_type separator = u' ') const; + #ifdef CRU_PLATFORM_WINDOWS const wchar_t* WinCStr() const { return reinterpret_cast<const wchar_t*>(c_str()); @@ -231,11 +226,6 @@ class CRU_BASE_API String { int Compare(const String& other) const; - float ParseToFloat(Index* processed_characters_count = nullptr) const; - double ParseToDouble(Index* processed_characters_count = nullptr) const; - std::vector<float> ParseToFloatList(value_type separator = u' '); - std::vector<double> ParseToDoubleList(value_type separator = u' '); - private: static char16_t kEmptyBuffer[1]; @@ -247,6 +237,9 @@ class CRU_BASE_API String { class CRU_BASE_API StringView { public: + static double_conversion::StringToDoubleConverter + kDefaultStringToDoubleConverter; + using value_type = char16_t; using size_type = Index; using difference_type = Index; @@ -307,16 +300,39 @@ class CRU_BASE_API StringView { StringView substr(Index pos); StringView substr(Index pos, Index size); - int Compare(const StringView& other) const; - - String ToString() const { return String(ptr_, size_); } - value_type operator[](Index index) const { return ptr_[index]; } operator std::u16string_view() const { return std::u16string_view(data(), size()); } + public: + int Compare(const StringView& other) const; + + String ToString() const { return String(ptr_, size_); } + + Utf16CodePointIterator CodePointIterator() const { + return Utf16CodePointIterator(ptr_, size_); + } + + Index Find(value_type value, Index start = 0) const; + std::vector<String> Split(value_type separator, + bool remove_space_line = false) const; + std::vector<String> SplitToLines(bool remove_space_line = false) const; + + bool StartWith(StringView str) const; + bool EndWith(StringView str) const; + + Index IndexFromCodeUnitToCodePoint(Index code_unit_index) const; + Index IndexFromCodePointToCodeUnit(Index code_point_index) const; + Range RangeFromCodeUnitToCodePoint(Range code_unit_range) const; + Range RangeFromCodePointToCodeUnit(Range code_point_range) const; + + float ParseToFloat(Index* processed_characters_count = nullptr) const; + double ParseToDouble(Index* processed_characters_count = nullptr) const; + std::vector<float> ParseToFloatList(value_type separator = u' ') const; + std::vector<double> ParseToDoubleList(value_type separator = u' ') const; + std::string ToUtf8() const; private: diff --git a/include/cru/common/StringUtil.hpp b/include/cru/common/StringUtil.hpp index 2d53fc2b..c840960d 100644 --- a/include/cru/common/StringUtil.hpp +++ b/include/cru/common/StringUtil.hpp @@ -2,9 +2,6 @@ #include "Base.hpp" #include <functional> -#include <stdexcept> -#include <string> -#include <string_view> namespace cru { using CodePoint = std::int32_t; @@ -118,9 +115,6 @@ using Utf8CodePointIterator = CodePointIterator<char, &Utf8NextCodePoint>; using Utf16CodePointIterator = CodePointIterator<char16_t, &Utf16NextCodePoint>; -void CRU_BASE_API Utf8EncodeCodePointAppend(CodePoint code_point, - std::string& str); - namespace details { template <typename UInt, int number_of_bit, typename ReturnType> inline std::enable_if_t<std::is_unsigned_v<UInt>, ReturnType> ExtractBits( @@ -129,18 +123,18 @@ inline std::enable_if_t<std::is_unsigned_v<UInt>, ReturnType> ExtractBits( } } // namespace details -template <typename TAppend> -bool Utf8EncodeCodePointAppendWithFunc(CodePoint code_point, TAppend&& append) { - auto write_continue_byte = [&append](std::uint8_t byte6) { - append((1u << 7) + (((1u << 6) - 1) & byte6)); +template <typename TStr> +bool Utf8EncodeCodePointAppend(CodePoint code_point, TStr& 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) { - append(static_cast<char>(code_point)); + str.push_back(static_cast<char>(code_point)); return true; } else if (code_point >= 0x0080 && code_point <= 0x07FF) { std::uint32_t unsigned_code_point = code_point; - append( + str.push_back( static_cast<char>(details::ExtractBits<std::uint32_t, 5, std::uint8_t>( (unsigned_code_point >> 6)) + 0b11000000)); @@ -149,7 +143,7 @@ bool Utf8EncodeCodePointAppendWithFunc(CodePoint code_point, TAppend&& append) { return true; } else if (code_point >= 0x0800 && code_point <= 0xFFFF) { std::uint32_t unsigned_code_point = code_point; - append( + str.push_back( static_cast<char>(details::ExtractBits<std::uint32_t, 4, std::uint8_t>( (unsigned_code_point >> (6 * 2))) + 0b11100000)); @@ -160,7 +154,7 @@ bool Utf8EncodeCodePointAppendWithFunc(CodePoint code_point, TAppend&& append) { return true; } else if (code_point >= 0x10000 && code_point <= 0x10FFFF) { std::uint32_t unsigned_code_point = code_point; - append( + str.push_back( static_cast<char>(details::ExtractBits<std::uint32_t, 3, std::uint8_t>( (unsigned_code_point >> (6 * 3))) + 0b11110000)); @@ -176,19 +170,18 @@ bool Utf8EncodeCodePointAppendWithFunc(CodePoint code_point, TAppend&& append) { } } -template <typename TAppend> -bool Utf16EncodeCodePointAppendWithFunc(CodePoint code_point, - TAppend&& append) { +template <typename TStr> +bool Utf16EncodeCodePointAppend(CodePoint code_point, TStr& str) { if ((code_point >= 0 && code_point <= 0xD7FF) || (code_point >= 0xE000 && code_point <= 0xFFFF)) { - append(static_cast<char16_t>(code_point)); + str.push_back(static_cast<char16_t>(code_point)); return true; } else if (code_point >= 0x10000 && code_point <= 0x10FFFF) { std::uint32_t u = code_point - 0x10000; - append(static_cast<char16_t>( + str.push_back(static_cast<char16_t>( details::ExtractBits<std::uint32_t, 10, std::uint32_t>(u >> 10) + 0xD800u)); - append(static_cast<char16_t>( + str.push_back(static_cast<char16_t>( details::ExtractBits<std::uint32_t, 10, std::uint32_t>(u) + 0xDC00u)); return true; } else { @@ -196,9 +189,6 @@ bool Utf16EncodeCodePointAppendWithFunc(CodePoint code_point, } } -std::string CRU_BASE_API ToUtf8(const char16_t* ptr, Index size); -std::u16string CRU_BASE_API ToUtf16(const char* s, Index size); - // If given s is not a valid utf16 string, return value is UD. bool CRU_BASE_API Utf16IsValidInsertPosition(const char16_t* ptr, Index size, Index position); |