diff options
author | crupest <crupest@outlook.com> | 2021-11-14 22:44:59 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-11-14 22:44:59 +0800 |
commit | 63b4956c3a802ee1c0fd92d1ce56e9330b6de4d2 (patch) | |
tree | f13f592d810b797c4c64803719acbd258565ebce | |
parent | a744ed1fea0eaf4d946909da7fdc0a4c0f9c5ec0 (diff) | |
download | cru-63b4956c3a802ee1c0fd92d1ce56e9330b6de4d2.tar.gz cru-63b4956c3a802ee1c0fd92d1ce56e9330b6de4d2.tar.bz2 cru-63b4956c3a802ee1c0fd92d1ce56e9330b6de4d2.zip |
...
-rw-r--r-- | include/cru/common/String.hpp | 5 | ||||
-rw-r--r-- | include/cru/common/StringUtil.hpp | 5 | ||||
-rw-r--r-- | include/cru/ui/ThemeManager.hpp | 5 | ||||
-rw-r--r-- | include/cru/ui/render/ScrollBar.hpp | 5 | ||||
-rw-r--r-- | src/common/String.cpp | 13 | ||||
-rw-r--r-- | src/common/StringUtil.cpp | 12 | ||||
-rw-r--r-- | src/ui/ThemeManager.cpp | 6 | ||||
-rw-r--r-- | src/ui/render/ScrollBar.cpp | 6 | ||||
-rw-r--r-- | test/common/StringUtilTest.cpp | 61 |
9 files changed, 68 insertions, 50 deletions
diff --git a/include/cru/common/String.hpp b/include/cru/common/String.hpp index 0a8d88a0..9996f617 100644 --- a/include/cru/common/String.hpp +++ b/include/cru/common/String.hpp @@ -273,6 +273,8 @@ class CRU_BASE_API StringView { return std::u16string_view(data(), size()); } + std::string ToUtf8() const; + private: const char16_t* ptr_; Index size_; @@ -402,6 +404,9 @@ inline Index Utf16NextWord(StringView str, Index position, bool* is_space = nullptr) { return Utf16NextWord(str.data(), str.size(), position, is_space); } + +String CRU_BASE_API ToLower(StringView s); +String CRU_BASE_API ToUpper(StringView s); } // namespace cru template <> diff --git a/include/cru/common/StringUtil.hpp b/include/cru/common/StringUtil.hpp index a24cf924..6c6b47b8 100644 --- a/include/cru/common/StringUtil.hpp +++ b/include/cru/common/StringUtil.hpp @@ -74,8 +74,7 @@ class CodePointIterator { public: bool operator==(const CodePointIterator& other) const { // You should compare iterator that iterate on the same string. - Expects(this->string_.data() == other.string_.data() && - this->string_.size() == other.string_.size()); + Expects(this->ptr_ == other.ptr_ && this->size_ == other.size_); return this->position_ == other.position_; } bool operator!=(const CodePointIterator& other) const { @@ -222,6 +221,4 @@ Index CRU_BASE_API Utf16NextWord(const char16_t* ptr, Index size, char16_t CRU_BASE_API ToLower(char16_t c); char16_t CRU_BASE_API ToUpper(char16_t c); -std::u16string CRU_BASE_API ToLower(std::u16string_view s); -std::u16string CRU_BASE_API ToUpper(std::u16string_view s); } // namespace cru diff --git a/include/cru/ui/ThemeManager.hpp b/include/cru/ui/ThemeManager.hpp index 1da29a0e..9908658c 100644 --- a/include/cru/ui/ThemeManager.hpp +++ b/include/cru/ui/ThemeManager.hpp @@ -35,7 +35,7 @@ class ThemeManager : public Object { } gsl::not_null<std::shared_ptr<platform::graphics::IBrush>> GetBrush( - std::u16string key); + StringView key); private: void Init(); @@ -43,8 +43,7 @@ class ThemeManager : public Object { private: Event<std::nullptr_t> theme_resource_change_event_; boost::property_tree::ptree theme_tree_; - std::unordered_map<std::u16string, - std::shared_ptr<platform::graphics::IBrush>> + std::unordered_map<String, std::shared_ptr<platform::graphics::IBrush>> brushes_; }; } // namespace cru::ui diff --git a/include/cru/ui/render/ScrollBar.hpp b/include/cru/ui/render/ScrollBar.hpp index ce6f1b98..d6042719 100644 --- a/include/cru/ui/render/ScrollBar.hpp +++ b/include/cru/ui/render/ScrollBar.hpp @@ -13,7 +13,6 @@ #include "cru/ui/controls/Control.hpp" #include "cru/ui/helper/ClickDetector.hpp" - #include <gsl/pointers> #include <memory> #include <optional> @@ -42,8 +41,8 @@ enum class ScrollBarAreaKind { enum class ScrollBarBrushUsageKind { Arrow, ArrowBackground, Slot, Thumb }; enum class ScrollBarBrushStateKind { Normal, Hover, Press, Disable }; -std::u16string GenerateScrollBarThemeColorKey(ScrollBarBrushUsageKind usage, - ScrollBarBrushStateKind state); +String GenerateScrollBarThemeColorKey(ScrollBarBrushUsageKind usage, + ScrollBarBrushStateKind state); class ScrollBar : public Object { public: diff --git a/src/common/String.cpp b/src/common/String.cpp index 8b53b16b..743a33fd 100644 --- a/src/common/String.cpp +++ b/src/common/String.cpp @@ -376,4 +376,17 @@ StringView StringView::substr(Index pos, Index size) { return StringView(ptr_ + pos, std::min(size, size_ - pos)); } +std::string StringView::ToUtf8() const { return cru::ToUtf8(ptr_, size_); } + +String ToLower(StringView s) { + String result; + for (auto c : s) result.push_back(ToLower(c)); + return result; +} + +String ToUpper(StringView s) { + String result; + for (auto c : s) result.push_back(ToUpper(c)); + return result; +} } // namespace cru diff --git a/src/common/StringUtil.cpp b/src/common/StringUtil.cpp index 850524b5..c828fa21 100644 --- a/src/common/StringUtil.cpp +++ b/src/common/StringUtil.cpp @@ -252,16 +252,4 @@ char16_t ToUpper(char16_t c) { } return c; } - -std::u16string ToLower(std::u16string_view s) { - std::u16string result; - for (auto c : s) result.push_back(ToLower(c)); - return result; -} - -std::u16string ToUpper(std::u16string_view s) { - std::u16string result; - for (auto c : s) result.push_back(ToUpper(c)); - return result; -} } // namespace cru diff --git a/src/ui/ThemeManager.cpp b/src/ui/ThemeManager.cpp index 4a0e08a5..a5280d40 100644 --- a/src/ui/ThemeManager.cpp +++ b/src/ui/ThemeManager.cpp @@ -36,15 +36,15 @@ void ThemeManager::Init() { } gsl::not_null<std::shared_ptr<platform::graphics::IBrush>> -ThemeManager::GetBrush(std::u16string key) { - std::u16string k = ToLower(key); +ThemeManager::GetBrush(StringView key) { + String k = ToLower(key); auto cached_brush_iter = brushes_.find(k); if (cached_brush_iter != brushes_.cend()) { return cached_brush_iter->second; } auto color_string = - String::FromUtf8(theme_tree_.get<std::string>(ToUtf8(key))); + String::FromUtf8(theme_tree_.get<std::string>(key.ToUtf8())); auto color = Color::Parse(color_string); if (!color) throw BadThemeResourceException("Value is not a valid color."); std::shared_ptr<platform::graphics::IBrush> brush = diff --git a/src/ui/render/ScrollBar.cpp b/src/ui/render/ScrollBar.cpp index 0d644a1f..aeecd9e2 100644 --- a/src/ui/render/ScrollBar.cpp +++ b/src/ui/render/ScrollBar.cpp @@ -39,9 +39,9 @@ constexpr std::array<ScrollBarAreaKind, 5> kScrollBarAreaKindList{ ScrollBarAreaKind::UpSlot, ScrollBarAreaKind::DownSlot, ScrollBarAreaKind::Thumb}; -std::u16string GenerateScrollBarThemeColorKey(ScrollBarBrushUsageKind usage, - ScrollBarBrushStateKind state) { - std::u16string result = u"scrollbar."; +String GenerateScrollBarThemeColorKey(ScrollBarBrushUsageKind usage, + ScrollBarBrushStateKind state) { + String result = u"scrollbar."; switch (usage) { case ScrollBarBrushUsageKind::Arrow: result.append(u"arrow"); diff --git a/test/common/StringUtilTest.cpp b/test/common/StringUtilTest.cpp index 351b1923..5e1753b8 100644 --- a/test/common/StringUtilTest.cpp +++ b/test/common/StringUtilTest.cpp @@ -8,12 +8,18 @@ TEST(StringUtil, Utf8NextCodePoint) { using cru::Utf8NextCodePoint; std::string_view text = "aπ你🤣!"; gsl::index current = 0; - ASSERT_EQ(Utf8NextCodePoint(text, current, ¤t), 0x0061); - ASSERT_EQ(Utf8NextCodePoint(text, current, ¤t), 0x03C0); - ASSERT_EQ(Utf8NextCodePoint(text, current, ¤t), 0x4F60); - ASSERT_EQ(Utf8NextCodePoint(text, current, ¤t), 0x1F923); - ASSERT_EQ(Utf8NextCodePoint(text, current, ¤t), 0x0021); - ASSERT_EQ(Utf8NextCodePoint(text, current, ¤t), k_invalid_code_point); + ASSERT_EQ(Utf8NextCodePoint(text.data(), text.size(), current, ¤t), + 0x0061); + ASSERT_EQ(Utf8NextCodePoint(text.data(), text.size(), current, ¤t), + 0x03C0); + ASSERT_EQ(Utf8NextCodePoint(text.data(), text.size(), current, ¤t), + 0x4F60); + ASSERT_EQ(Utf8NextCodePoint(text.data(), text.size(), current, ¤t), + 0x1F923); + ASSERT_EQ(Utf8NextCodePoint(text.data(), text.size(), current, ¤t), + 0x0021); + ASSERT_EQ(Utf8NextCodePoint(text.data(), text.size(), current, ¤t), + k_invalid_code_point); ASSERT_EQ(current, static_cast<gsl::index>(text.size())); } @@ -21,12 +27,18 @@ TEST(StringUtil, Utf16NextCodePoint) { using cru::Utf16NextCodePoint; std::u16string_view text = u"aπ你🤣!"; gsl::index current = 0; - ASSERT_EQ(Utf16NextCodePoint(text, current, ¤t), 0x0061); - ASSERT_EQ(Utf16NextCodePoint(text, current, ¤t), 0x03C0); - ASSERT_EQ(Utf16NextCodePoint(text, current, ¤t), 0x4F60); - ASSERT_EQ(Utf16NextCodePoint(text, current, ¤t), 0x1F923); - ASSERT_EQ(Utf16NextCodePoint(text, current, ¤t), 0x0021); - ASSERT_EQ(Utf16NextCodePoint(text, current, ¤t), k_invalid_code_point); + ASSERT_EQ(Utf16NextCodePoint(text.data(), text.size(), current, ¤t), + 0x0061); + ASSERT_EQ(Utf16NextCodePoint(text.data(), text.size(), current, ¤t), + 0x03C0); + ASSERT_EQ(Utf16NextCodePoint(text.data(), text.size(), current, ¤t), + 0x4F60); + ASSERT_EQ(Utf16NextCodePoint(text.data(), text.size(), current, ¤t), + 0x1F923); + ASSERT_EQ(Utf16NextCodePoint(text.data(), text.size(), current, ¤t), + 0x0021); + ASSERT_EQ(Utf16NextCodePoint(text.data(), text.size(), current, ¤t), + k_invalid_code_point); ASSERT_EQ(current, static_cast<gsl::index>(text.size())); } @@ -34,12 +46,17 @@ TEST(StringUtil, Utf16PreviousCodePoint) { using cru::Utf16PreviousCodePoint; std::u16string_view text = u"aπ你🤣!"; gsl::index current = text.size(); - ASSERT_EQ(Utf16PreviousCodePoint(text, current, ¤t), 0x0021); - ASSERT_EQ(Utf16PreviousCodePoint(text, current, ¤t), 0x1F923); - ASSERT_EQ(Utf16PreviousCodePoint(text, current, ¤t), 0x4F60); - ASSERT_EQ(Utf16PreviousCodePoint(text, current, ¤t), 0x03C0); - ASSERT_EQ(Utf16PreviousCodePoint(text, current, ¤t), 0x0061); - ASSERT_EQ(Utf16PreviousCodePoint(text, current, ¤t), + ASSERT_EQ(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t), + 0x0021); + ASSERT_EQ(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t), + 0x1F923); + ASSERT_EQ(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t), + 0x4F60); + ASSERT_EQ(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t), + 0x03C0); + ASSERT_EQ(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t), + 0x0061); + ASSERT_EQ(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t), k_invalid_code_point); ASSERT_EQ(current, 0); } @@ -49,7 +66,7 @@ TEST(StringUtil, Utf8CodePointIterator) { std::string_view text = "aπ你🤣!"; std::vector<cru::CodePoint> code_points; - for (auto cp : Utf8CodePointIterator{text}) { + for (auto cp : Utf8CodePointIterator(text.data(), text.size())) { code_points.push_back(cp); } @@ -64,7 +81,7 @@ TEST(StringUtil, Utf16CodePointIterator) { std::u16string_view text = u"aπ你🤣!"; std::vector<cru::CodePoint> code_points; - for (auto cp : Utf16CodePointIterator{text}) { + for (auto cp : Utf16CodePointIterator(text.data(), text.size())) { code_points.push_back(cp); } @@ -79,7 +96,7 @@ TEST(StringUtil, ToUtf8) { std::u16string_view utf16_text = u"aπ你🤣!"; std::string_view utf8_text = "aπ你🤣!"; - ASSERT_EQ(ToUtf8(utf16_text), utf8_text); + ASSERT_EQ(ToUtf8(utf16_text.data(), utf16_text.size()), utf8_text); } TEST(StringUtil, ToUtf16) { @@ -87,7 +104,7 @@ TEST(StringUtil, ToUtf16) { std::u16string_view utf16_text = u"aπ你🤣!"; std::string_view utf8_text = "aπ你🤣!"; - ASSERT_EQ(ToUtf16(utf8_text), utf16_text); + ASSERT_EQ(ToUtf16(utf8_text.data(), utf8_text.size()), utf16_text); } // TEST(WinString, IndexUtf8ToUtf16) { |