diff options
author | crupest <crupest@outlook.com> | 2021-09-15 17:12:51 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-09-15 17:12:51 +0800 |
commit | 105e4ad880a810300bf4b3a0a0752ae58924667b (patch) | |
tree | da9fb12923160312d2d989514b0e4df243907b66 /src | |
parent | d6cd1c46889e94b325244e5ba5ccea97cc856952 (diff) | |
download | cru-105e4ad880a810300bf4b3a0a0752ae58924667b.tar.gz cru-105e4ad880a810300bf4b3a0a0752ae58924667b.tar.bz2 cru-105e4ad880a810300bf4b3a0a0752ae58924667b.zip |
...
Diffstat (limited to 'src')
-rw-r--r-- | src/common/String.cpp | 41 | ||||
-rw-r--r-- | src/platform/Color.cpp | 28 |
2 files changed, 46 insertions, 23 deletions
diff --git a/src/common/String.cpp b/src/common/String.cpp index 6d958688..602bcc51 100644 --- a/src/common/String.cpp +++ b/src/common/String.cpp @@ -1,8 +1,11 @@ #include "cru/common/String.hpp" #include "cru/common/StringUtil.hpp" +#include <gsl/gsl> + #include <algorithm> #include <cstring> +#include <functional> #include <string_view> namespace cru { @@ -302,4 +305,42 @@ 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(); + + const_iterator end1 = cend(); + const_iterator end2 = other.cend(); + + while (i1 != end1 && i2 != end2) { + int r = cru::Compare(*i1, *i2); + if (r != 0) return r; + i1++; + i2++; + } + + if (i1 == end1) { + if (i2 == end2) { + return 0; + } else { + return -1; + } + } else { + return 1; + } +} + +StringView StringView::substr(Index pos) { + Expects(pos >= 0 && pos < size_); + return StringView(ptr_ + pos, size_ - pos); +} + +StringView StringView::substr(Index pos, Index size) { + Expects(pos >= 0 && pos < size_); + return StringView(ptr_ + pos, std::min(size, size_ - pos)); +} + } // namespace cru diff --git a/src/platform/Color.cpp b/src/platform/Color.cpp index c4cc511b..52534048 100644 --- a/src/platform/Color.cpp +++ b/src/platform/Color.cpp @@ -8,24 +8,7 @@ #include <string_view> namespace cru::platform { -std::string Color::ToUtf8String() const { - auto to_hex = [](std::uint8_t v) -> char { - return v >= 10 ? v - 10 + 'a' : v + '0'; - }; - - auto to_two_hex_digit = [to_hex](std::uint8_t v) -> std::string { - return {to_hex(v /= 16), to_hex(v %= 16)}; - }; - - std::string result = "#"; - result.append(to_two_hex_digit(red)); - result.append(to_two_hex_digit(green)); - result.append(to_two_hex_digit(blue)); - result.append(to_two_hex_digit(alpha)); - return result; -} - -std::u16string Color::ToString() const { +String Color::ToString() const { auto to_hex = [](std::uint8_t v) -> char16_t { return v >= 10 ? v - 10 + u'a' : v + u'0'; }; @@ -42,7 +25,7 @@ std::u16string Color::ToString() const { return result; } -std::optional<Color> Color::Parse(std::u16string_view string, +std::optional<Color> Color::Parse(StringView string, bool parse_predefined_color) { if (parse_predefined_color) { auto optional_predefined_color = GetPredefinedColorByName(string); @@ -58,8 +41,7 @@ std::optional<Color> Color::Parse(std::u16string_view string, return std::nullopt; }; - auto get_num_for_two_digit = - [get_num](std::u16string_view str) -> std::optional<int> { + auto get_num_for_two_digit = [get_num](StringView str) -> std::optional<int> { int num = 0; auto d1 = get_num(str[0]); if (!d1) return std::nullopt; @@ -102,7 +84,7 @@ std::optional<Color> Color::Parse(std::u16string_view string, } namespace details { -const std::unordered_map<std::u16string_view, Color> predefined_name_color_map{ +const std::unordered_map<StringView, Color> predefined_name_color_map{ {u"transparent", colors::transparent}, {u"black", colors::black}, {u"silver", colors::silver}, @@ -255,7 +237,7 @@ const std::unordered_map<std::u16string_view, Color> predefined_name_color_map{ }; } // namespace details -std::optional<Color> GetPredefinedColorByName(std::u16string_view name) { +std::optional<Color> GetPredefinedColorByName(StringView name) { auto result = details::predefined_name_color_map.find(name); if (result != details::predefined_name_color_map.cend()) { return result->second; |