diff options
author | crupest <crupest@outlook.com> | 2021-09-15 19:17:38 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-09-15 19:17:38 +0800 |
commit | cb981f5a337f3a8fc9d450b891c358c2b8dc29d3 (patch) | |
tree | 5216a09c127e534edfebca22f8b91d876812f6b4 | |
parent | 105e4ad880a810300bf4b3a0a0752ae58924667b (diff) | |
download | cru-cb981f5a337f3a8fc9d450b891c358c2b8dc29d3.tar.gz cru-cb981f5a337f3a8fc9d450b891c358c2b8dc29d3.tar.bz2 cru-cb981f5a337f3a8fc9d450b891c358c2b8dc29d3.zip |
...
-rw-r--r-- | include/cru/common/String.hpp | 54 | ||||
-rw-r--r-- | include/cru/platform/GraphicsBase.hpp (renamed from include/cru/platform/GraphBase.hpp) | 0 | ||||
-rw-r--r-- | include/cru/platform/Matrix.hpp | 2 | ||||
-rw-r--r-- | include/cru/platform/graphics/Base.hpp | 3 | ||||
-rw-r--r-- | include/cru/platform/gui/InputMethod.hpp | 4 | ||||
-rw-r--r-- | include/cru/platform/gui/Keyboard.hpp | 6 | ||||
-rw-r--r-- | include/cru/ui/render/MeasureRequirement.hpp | 5 | ||||
-rw-r--r-- | include/cru/ui/render/RenderObject.hpp | 1 | ||||
-rw-r--r-- | src/common/String.cpp | 9 | ||||
-rw-r--r-- | src/platform/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/platform/Color.cpp | 4 | ||||
-rw-r--r-- | src/platform/gui/Keyboard.cpp | 7 | ||||
-rw-r--r-- | src/ui/UiManager.cpp | 1 |
13 files changed, 60 insertions, 38 deletions
diff --git a/include/cru/common/String.hpp b/include/cru/common/String.hpp index 47b20147..ed3deb4c 100644 --- a/include/cru/common/String.hpp +++ b/include/cru/common/String.hpp @@ -7,9 +7,11 @@ #include <algorithm> #include <array> #include <charconv> +#include <initializer_list> #include <iterator> #include <limits> #include <stdexcept> +#include <string> #include <system_error> #include <type_traits> #include <vector> @@ -62,12 +64,25 @@ class CRU_BASE_API String { public: String() = default; - String(const std::uint16_t* str); + explicit String(const std::uint16_t* str); String(const std::uint16_t* str, Index size); - String(const char16_t* str); + explicit String(const char16_t* str); String(const char16_t* str, Index size); - String(const std::u16string& str) : String(str.data(), str.size()) {} + String(const std::u16string_view& str) : String(str.data(), str.size()) {} + + template <Index size> + constexpr String(const char16_t (&str)[size]) : String(str, size) {} + + template <typename Iter> + String(Iter start, Iter end) { + for (; start != end; start++) { + append(*start); + } + } + + String(const std::initializer_list<std::uint16_t>& l) + : String(l.begin(), l.end()) {} #ifdef CRU_PLATFORM_WINDOWS String(const wchar_t* str); @@ -147,14 +162,10 @@ class CRU_BASE_API String { void append(const std::uint16_t* str, Index size) { this->insert(cend(), str, size); } - void append(const String& other) { append(other.data(), other.size()); } inline void append(StringView str); public: - String& operator+=(const String& other) { - append(other); - return *this; - } + String& operator+=(const String& other); public: Utf16CodePointIterator CodePointIterator() const { @@ -201,8 +212,12 @@ inline String operator+(const String& left, const String& right) { return result; } -template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> -String ToString(T value) { +inline String ToString(bool value) { + return value ? String(u"true") : String(u"false"); +} + +template <typename T> +std::enable_if_t<std::is_integral_v<T>, String> ToString(T value) { std::array<char, 50> buffer; auto result = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value); @@ -219,6 +234,12 @@ String ToString(T value) { } } +template <typename T> +std::enable_if_t<std::is_floating_point_v<T>, String> ToString(T value) { + auto str = std::to_string(value); + return String(str.cbegin(), str.cend()); +} + inline String ToString(String value) { return std::move(value); } namespace details { @@ -285,18 +306,19 @@ class CRU_BASE_API StringView { StringView() = default; - StringView(const std::uint16_t* ptr); - StringView(const std::uint16_t* ptr, Index size) : ptr_(ptr), size_(size) {} + constexpr StringView(const std::uint16_t* ptr, Index size) + : ptr_(ptr), size_(size) {} - StringView(const char16_t* ptr) - : StringView(reinterpret_cast<const std::uint16_t*>(ptr)) {} + template <Index size> + StringView(const char16_t (&array)[size]) : StringView(array, size) {} StringView(const char16_t* ptr, Index size) - : StringView(reinterpret_cast<const std::uint16_t*>(ptr), size) {} + : ptr_(reinterpret_cast<const std::uint16_t*>(ptr)), size_(size) {} StringView(const String& str) : StringView(str.data(), str.size()) {} CRU_DEFAULT_COPY(StringView) - CRU_DEFAULT_MOVE(StringView) ~StringView() = default; + CRU_DEFAULT_MOVE(StringView) + ~StringView() = default; Index size() const { return size_; } const std::uint16_t* data() const { return ptr_; } diff --git a/include/cru/platform/GraphBase.hpp b/include/cru/platform/GraphicsBase.hpp index e7201ec0..e7201ec0 100644 --- a/include/cru/platform/GraphBase.hpp +++ b/include/cru/platform/GraphicsBase.hpp diff --git a/include/cru/platform/Matrix.hpp b/include/cru/platform/Matrix.hpp index 8ec5faaa..7985af0e 100644 --- a/include/cru/platform/Matrix.hpp +++ b/include/cru/platform/Matrix.hpp @@ -1,5 +1,5 @@ #pragma once -#include "GraphBase.hpp" +#include "GraphicsBase.hpp" #include <cmath> diff --git a/include/cru/platform/graphics/Base.hpp b/include/cru/platform/graphics/Base.hpp index 25881772..60af40ab 100644 --- a/include/cru/platform/graphics/Base.hpp +++ b/include/cru/platform/graphics/Base.hpp @@ -1,5 +1,6 @@ #pragma once -#include "../GraphBase.hpp" +#include "../Color.hpp" +#include "../GraphicsBase.hpp" #include "../Matrix.hpp" #include "../Resource.hpp" diff --git a/include/cru/platform/gui/InputMethod.hpp b/include/cru/platform/gui/InputMethod.hpp index c259e63c..923a2088 100644 --- a/include/cru/platform/gui/InputMethod.hpp +++ b/include/cru/platform/gui/InputMethod.hpp @@ -16,7 +16,7 @@ struct CompositionClause { using CompositionClauses = std::vector<CompositionClause>; struct CompositionText { - std::u16string text; + String text; CompositionClauses clauses; TextRange selection; }; @@ -49,7 +49,7 @@ struct IInputMethodContext : virtual IPlatformResource { // Triggered every time composition text changes. virtual IEvent<std::nullptr_t>* CompositionEvent() = 0; - virtual IEvent<std::u16string_view>* TextEvent() = 0; + virtual IEvent<StringView>* TextEvent() = 0; }; } // namespace cru::platform::gui diff --git a/include/cru/platform/gui/Keyboard.hpp b/include/cru/platform/gui/Keyboard.hpp index b93b44fa..b025d86e 100644 --- a/include/cru/platform/gui/Keyboard.hpp +++ b/include/cru/platform/gui/Keyboard.hpp @@ -123,7 +123,7 @@ struct KeyModifiers { static constexpr KeyModifier alt{0b100}; }; -CRU_PLATFORM_GUI_API std::u16string_view ToString(KeyCode key_code); -CRU_PLATFORM_GUI_API std::u16string ToString( - KeyModifier key_modifier, std::u16string_view separator = u"+"); +CRU_PLATFORM_GUI_API String ToString(KeyCode key_code); +CRU_PLATFORM_GUI_API String ToString(KeyModifier key_modifier, + StringView separator = u"+"); } // namespace cru::platform::gui diff --git a/include/cru/ui/render/MeasureRequirement.hpp b/include/cru/ui/render/MeasureRequirement.hpp index ff9dd6e3..90d02a02 100644 --- a/include/cru/ui/render/MeasureRequirement.hpp +++ b/include/cru/ui/render/MeasureRequirement.hpp @@ -115,9 +115,8 @@ class MeasureLength final { } } - std::u16string ToDebugString() const { - return IsSpecified() ? ToUtf16String(GetLengthOrUndefined()) - : u"UNSPECIFIED"; + String ToDebugString() const { + return IsSpecified() ? ToString(GetLengthOrUndefined()) : u"UNSPECIFIED"; } private: diff --git a/include/cru/ui/render/RenderObject.hpp b/include/cru/ui/render/RenderObject.hpp index 622c27f2..0dd95c71 100644 --- a/include/cru/ui/render/RenderObject.hpp +++ b/include/cru/ui/render/RenderObject.hpp @@ -4,7 +4,6 @@ #include "MeasureRequirement.hpp" #include "cru/common/Base.hpp" #include "cru/common/Event.hpp" -#include "cru/platform/GraphBase.hpp" #include "cru/ui/Base.hpp" #include <cstddef> diff --git a/src/common/String.cpp b/src/common/String.cpp index 602bcc51..3d3e4a3c 100644 --- a/src/common/String.cpp +++ b/src/common/String.cpp @@ -222,6 +222,11 @@ Range String::RangeFromCodePointToCodeUnit(Range code_point_range) const { IndexFromCodePointToCodeUnit(code_point_range.GetEnd())); } +String& String::operator+=(const String& other) { + append(other); + return *this; +} + namespace { inline int Compare(std::uint16_t left, std::uint16_t right) { if (left < right) return -1; @@ -305,9 +310,6 @@ void FormatAppendFromFormatTokenList( } } // namespace details -StringView::StringView(const std::uint16_t* ptr) - : ptr_(ptr), size_(GetStrSize(ptr)) {} - int StringView::Compare(const StringView& other) const { const_iterator i1 = cbegin(); const_iterator i2 = other.cbegin(); @@ -340,6 +342,7 @@ StringView StringView::substr(Index pos) { StringView StringView::substr(Index pos, Index size) { Expects(pos >= 0 && pos < size_); + return StringView(ptr_ + pos, std::min(size, size_ - pos)); } diff --git a/src/platform/CMakeLists.txt b/src/platform/CMakeLists.txt index e831c544..0288bed0 100644 --- a/src/platform/CMakeLists.txt +++ b/src/platform/CMakeLists.txt @@ -8,7 +8,7 @@ target_sources(cru_platform_base PUBLIC ${CRU_PLATFORM_BASE_INCLUDE_DIR}/Check.hpp ${CRU_PLATFORM_BASE_INCLUDE_DIR}/Color.hpp ${CRU_PLATFORM_BASE_INCLUDE_DIR}/Exception.hpp - ${CRU_PLATFORM_BASE_INCLUDE_DIR}/GraphBase.hpp + ${CRU_PLATFORM_BASE_INCLUDE_DIR}/GraphicsBase.hpp ${CRU_PLATFORM_BASE_INCLUDE_DIR}/HeapDebug.hpp ${CRU_PLATFORM_BASE_INCLUDE_DIR}/Matrix.hpp ${CRU_PLATFORM_BASE_INCLUDE_DIR}/Resource.hpp diff --git a/src/platform/Color.cpp b/src/platform/Color.cpp index 52534048..cced498c 100644 --- a/src/platform/Color.cpp +++ b/src/platform/Color.cpp @@ -13,11 +13,11 @@ String Color::ToString() const { return v >= 10 ? v - 10 + u'a' : v + u'0'; }; - auto to_two_hex_digit = [to_hex](std::uint8_t v) -> std::u16string { + auto to_two_hex_digit = [to_hex](std::uint8_t v) -> String { return {to_hex(v /= 16), to_hex(v %= 16)}; }; - std::u16string result = u"#"; + String result = u"#"; result.append(to_two_hex_digit(red)); result.append(to_two_hex_digit(green)); result.append(to_two_hex_digit(blue)); diff --git a/src/platform/gui/Keyboard.cpp b/src/platform/gui/Keyboard.cpp index 24880e00..7d4903e5 100644 --- a/src/platform/gui/Keyboard.cpp +++ b/src/platform/gui/Keyboard.cpp @@ -5,8 +5,7 @@ #include <string_view> namespace cru::platform::gui { -constexpr std::array<std::u16string_view, - static_cast<int>(KeyCode::NumPad9) + 1> +const std::array<StringView, static_cast<int>(KeyCode::NumPad9) + 1> key_code_string_list{u"Unknown", u"LeftButton", u"MiddleButton", @@ -106,13 +105,13 @@ constexpr std::array<std::u16string_view, u"NumPad8", u"NumPad9"}; -std::u16string_view ToString(KeyCode key_code) { +String ToString(KeyCode key_code) { if (static_cast<int>(key_code) < 0 || static_cast<int>(key_code) >= static_cast<int>(key_code_string_list.size())) return u"UNKNOWN_KEYCODENAME"; - return key_code_string_list[static_cast<int>(key_code)]; + return key_code_string_list[static_cast<int>(key_code)].ToString(); } std::u16string ToString(KeyModifier key_modifier, diff --git a/src/ui/UiManager.cpp b/src/ui/UiManager.cpp index 45dd1775..b0cc769a 100644 --- a/src/ui/UiManager.cpp +++ b/src/ui/UiManager.cpp @@ -2,7 +2,6 @@ #include <optional> #include "Helper.hpp" -#include "cru/platform/GraphBase.hpp" #include "cru/platform/graphics/Brush.hpp" #include "cru/platform/graphics/Factory.hpp" #include "cru/platform/graphics/Font.hpp" |