diff options
author | crupest <crupest@outlook.com> | 2022-01-23 21:07:03 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-01-23 21:07:03 +0800 |
commit | da9bdf2baced1ff51350c98047b7ade68fcae930 (patch) | |
tree | c51b9c8538c82b19d142eb1340e6dac0e88ff4e4 | |
parent | 13860c88910c00478abe3001cc80125e76767381 (diff) | |
download | cru-da9bdf2baced1ff51350c98047b7ade68fcae930.tar.gz cru-da9bdf2baced1ff51350c98047b7ade68fcae930.tar.bz2 cru-da9bdf2baced1ff51350c98047b7ade68fcae930.zip |
...
-rw-r--r-- | include/cru/common/String.hpp | 2 | ||||
-rw-r--r-- | include/cru/xml/XmlNode.hpp | 14 | ||||
-rw-r--r-- | src/common/String.cpp | 31 | ||||
-rw-r--r-- | src/ui/mapper/BorderStyleMapper.cpp | 8 | ||||
-rw-r--r-- | src/ui/mapper/ColorMapper.cpp | 4 | ||||
-rw-r--r-- | src/ui/mapper/CornerRadiusMapper.cpp | 15 | ||||
-rw-r--r-- | src/ui/mapper/CursorMapper.cpp | 10 | ||||
-rw-r--r-- | src/ui/mapper/PointMapper.cpp | 7 | ||||
-rw-r--r-- | src/ui/mapper/SizeMapper.cpp | 7 | ||||
-rw-r--r-- | src/ui/mapper/ThicknessMapper.cpp | 7 |
10 files changed, 59 insertions, 46 deletions
diff --git a/include/cru/common/String.hpp b/include/cru/common/String.hpp index 1d660623..5891e929 100644 --- a/include/cru/common/String.hpp +++ b/include/cru/common/String.hpp @@ -225,6 +225,7 @@ class CRU_BASE_API String { std::string ToUtf8() const; int Compare(const String& other) const; + int CaseInsensitiveCompare(const String& other) const; private: static char16_t kEmptyBuffer[1]; @@ -308,6 +309,7 @@ class CRU_BASE_API StringView { public: int Compare(const StringView& other) const; + int CaseInsensitiveCompare(const StringView& other) const; String ToString() const { return String(ptr_, size_); } diff --git a/include/cru/xml/XmlNode.hpp b/include/cru/xml/XmlNode.hpp index 71753410..c9e538c0 100644 --- a/include/cru/xml/XmlNode.hpp +++ b/include/cru/xml/XmlNode.hpp @@ -91,6 +91,9 @@ class CRU_XML_API XmlElementNode : public XmlNode { Index GetChildCount() const { return children_.size(); } String GetAttribute(const String& key) const { return attributes_.at(key); } + String GetAttributeCaseInsensitive(const String& key) const { + return *GetOptionalAttributeCaseInsensitive(key); + } std::optional<String> GetOptionalAttribute(const String& key) const { auto it = attributes_.find(key); if (it == attributes_.end()) { @@ -99,6 +102,17 @@ class CRU_XML_API XmlElementNode : public XmlNode { return it->second; } + std::optional<String> GetOptionalAttributeCaseInsensitive( + const String& key) const { + for (auto it = attributes_.begin(); it != attributes_.end(); ++it) { + if (it->first.CaseInsensitiveCompare(key) == 0) { + return it->second; + } + } + + return std::nullopt; + } + XmlNode* GetChildAt(Index index) const { return children_[index]; } void AddAttribute(String key, String value); diff --git a/src/common/String.cpp b/src/common/String.cpp index 24fb4071..62943059 100644 --- a/src/common/String.cpp +++ b/src/common/String.cpp @@ -329,9 +329,26 @@ inline int Compare(char16_t left, char16_t right) { if (left > right) return 1; return 0; } + +inline int CaseInsensitiveCompare(char16_t left, char16_t right) { + return Compare(ToLower(left), ToLower(right)); +} } // namespace -int String::Compare(const String& other) const { +int String::Compare(const String& other) const { return View().Compare(other); } +int String::CaseInsensitiveCompare(const String& other) const { + return View().CaseInsensitiveCompare(other); +} + +double_conversion::StringToDoubleConverter + StringView::kDefaultStringToDoubleConverter( + double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES | + double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES | + double_conversion::StringToDoubleConverter:: + ALLOW_CASE_INSENSIBILITY, + 0.0, NAN, "infinity", "nan"); + +int StringView::Compare(const StringView& other) const { const_iterator i1 = cbegin(); const_iterator i2 = other.cbegin(); @@ -356,15 +373,7 @@ int String::Compare(const String& other) const { } } -double_conversion::StringToDoubleConverter - StringView::kDefaultStringToDoubleConverter( - double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES | - double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES | - double_conversion::StringToDoubleConverter:: - ALLOW_CASE_INSENSIBILITY, - 0.0, NAN, "infinity", "nan"); - -int StringView::Compare(const StringView& other) const { +int StringView::CaseInsensitiveCompare(const StringView& other) const { const_iterator i1 = cbegin(); const_iterator i2 = other.cbegin(); @@ -372,7 +381,7 @@ int StringView::Compare(const StringView& other) const { const_iterator end2 = other.cend(); while (i1 != end1 && i2 != end2) { - int r = cru::Compare(*i1, *i2); + int r = cru::CaseInsensitiveCompare(*i1, *i2); if (r != 0) return r; i1++; i2++; diff --git a/src/ui/mapper/BorderStyleMapper.cpp b/src/ui/mapper/BorderStyleMapper.cpp index 65dbfe3b..691f091d 100644 --- a/src/ui/mapper/BorderStyleMapper.cpp +++ b/src/ui/mapper/BorderStyleMapper.cpp @@ -11,7 +11,7 @@ using namespace xml; using ui::style::ApplyBorderStyleInfo; bool BorderStyleMapper::XmlElementIsOfThisType(xml::XmlElementNode* node) { - return node->GetTag() == u"BorderStyle"; + return node->GetTag().CaseInsensitiveCompare(u"BorderStyle") == 0; } ApplyBorderStyleInfo BorderStyleMapper::DoMapFromXml( @@ -35,11 +35,11 @@ ApplyBorderStyleInfo BorderStyleMapper::DoMapFromXml( } else if (color_mapper->XmlElementIsOfThisType(c)) { auto brush = GetGraphicsFactory()->CreateSolidColorBrush( color_mapper->MapFromXml(c)); - auto name = c->GetOptionalAttribute(u"name"); + auto name = c->GetOptionalAttributeCaseInsensitive(u"name"); if (name) { - if (name == u"foreground") { + if (name->CaseInsensitiveCompare(u"foreground") == 0) { result.foreground_brush = std::move(brush); - } else if (name == u"background") { + } else if (name->CaseInsensitiveCompare(u"background") == 0) { result.background_brush = std::move(brush); } else { log::Debug(u"Unknown brush name: {}", *name); diff --git a/src/ui/mapper/ColorMapper.cpp b/src/ui/mapper/ColorMapper.cpp index a5d2c5e3..ddfa6b9b 100644 --- a/src/ui/mapper/ColorMapper.cpp +++ b/src/ui/mapper/ColorMapper.cpp @@ -2,7 +2,7 @@ namespace cru::ui::mapper { bool ColorMapper::XmlElementIsOfThisType(xml::XmlElementNode* node) { - return node->GetTag() == u"Color"; + return node->GetTag().CaseInsensitiveCompare(u"Color") == 0; } Color ColorMapper::DoMapFromString(String str) { @@ -14,7 +14,7 @@ Color ColorMapper::DoMapFromString(String str) { } Color ColorMapper::DoMapFromXml(xml::XmlElementNode* node) { - auto value_attr = node->GetOptionalAttribute(u"value"); + auto value_attr = node->GetOptionalAttributeCaseInsensitive(u"value"); if (!value_attr) { return colors::transparent; } diff --git a/src/ui/mapper/CornerRadiusMapper.cpp b/src/ui/mapper/CornerRadiusMapper.cpp index 0c13db7c..9c8855a3 100644 --- a/src/ui/mapper/CornerRadiusMapper.cpp +++ b/src/ui/mapper/CornerRadiusMapper.cpp @@ -4,37 +4,34 @@ namespace cru::ui::mapper { bool CornerRadiusMapper::XmlElementIsOfThisType(xml::XmlElementNode* node) { - if (node->GetTag() == u"CornerRadius") { - return true; - } - return false; + return node->GetTag().CaseInsensitiveCompare(u"CornerRadius") == 0; } CornerRadius CornerRadiusMapper::DoMapFromXml(xml::XmlElementNode* node) { auto point_mapper = MapperRegistry::GetInstance()->GetMapper<Point>(); CornerRadius result; - auto all = node->GetOptionalAttribute(u"all"); + auto all = node->GetOptionalAttributeCaseInsensitive(u"all"); if (all) { result.SetAll(point_mapper->MapFromString(*all)); } - auto lefttop = node->GetOptionalAttribute(u"lefttop"); + auto lefttop = node->GetOptionalAttributeCaseInsensitive(u"lefttop"); if (lefttop) { result.left_top = point_mapper->MapFromString(*lefttop); } - auto righttop = node->GetOptionalAttribute(u"righttop"); + auto righttop = node->GetOptionalAttributeCaseInsensitive(u"righttop"); if (righttop) { result.right_top = point_mapper->MapFromString(*righttop); } - auto rightbottom = node->GetOptionalAttribute(u"rightbottom"); + auto rightbottom = node->GetOptionalAttributeCaseInsensitive(u"rightbottom"); if (rightbottom) { result.right_bottom = point_mapper->MapFromString(*rightbottom); } - auto leftbottom = node->GetOptionalAttribute(u"leftbottom"); + auto leftbottom = node->GetOptionalAttributeCaseInsensitive(u"leftbottom"); if (leftbottom) { result.left_bottom = point_mapper->MapFromString(*leftbottom); } diff --git a/src/ui/mapper/CursorMapper.cpp b/src/ui/mapper/CursorMapper.cpp index d77da2a5..ad9c2bbf 100644 --- a/src/ui/mapper/CursorMapper.cpp +++ b/src/ui/mapper/CursorMapper.cpp @@ -9,7 +9,7 @@ using cru::platform::gui::ICursor; using cru::platform::gui::SystemCursorType; bool CursorMapper::XmlElementIsOfThisType(xml::XmlElementNode *node) { - return node->GetTag() == u"Cursor"; + return node->GetTag().CaseInsensitiveCompare(u"Cursor") == 0; } std::shared_ptr<ICursor> CursorMapper::DoMapFromString(String str) { @@ -17,11 +17,11 @@ std::shared_ptr<ICursor> CursorMapper::DoMapFromString(String str) { auto cursor_manager = GetUiApplication()->GetCursorManager(); - if (str == u"arrow") { + if (str.CaseInsensitiveCompare(u"arrow") == 0) { return cursor_manager->GetSystemCursor(SystemCursorType::Arrow); - } else if (str == u"hand") { + } else if (str.CaseInsensitiveCompare(u"hand") == 0) { return cursor_manager->GetSystemCursor(SystemCursorType::Hand); - } else if (str == u"ibeam") { + } else if (str.CaseInsensitiveCompare(u"ibeam") == 0) { return cursor_manager->GetSystemCursor(SystemCursorType::IBeam); } else { throw Exception(u"Unsupported cursor type."); @@ -29,7 +29,7 @@ std::shared_ptr<ICursor> CursorMapper::DoMapFromString(String str) { } std::shared_ptr<ICursor> CursorMapper::DoMapFromXml(xml::XmlElementNode *node) { - auto value_attr = node->GetOptionalAttribute(u"value"); + auto value_attr = node->GetOptionalAttributeCaseInsensitive(u"value"); if (!value_attr) return nullptr; return DoMapFromString(*value_attr); } diff --git a/src/ui/mapper/PointMapper.cpp b/src/ui/mapper/PointMapper.cpp index bcf209fb..efafc6ce 100644 --- a/src/ui/mapper/PointMapper.cpp +++ b/src/ui/mapper/PointMapper.cpp @@ -2,10 +2,7 @@ namespace cru::ui::mapper { bool PointMapper::XmlElementIsOfThisType(xml::XmlElementNode* node) { - if (node->GetTag() == u"Point") { - return true; - } - return false; + return node->GetTag().CaseInsensitiveCompare(u"Point") == 0; } Point PointMapper::DoMapFromString(String str) { @@ -20,7 +17,7 @@ Point PointMapper::DoMapFromString(String str) { } Point PointMapper::DoMapFromXml(xml::XmlElementNode* node) { - auto value_attr = node->GetOptionalAttribute(u"value"); + auto value_attr = node->GetOptionalAttributeCaseInsensitive(u"value"); if (!value_attr) return {}; return DoMapFromString(*value_attr); } diff --git a/src/ui/mapper/SizeMapper.cpp b/src/ui/mapper/SizeMapper.cpp index 3e3355cd..dfc83cca 100644 --- a/src/ui/mapper/SizeMapper.cpp +++ b/src/ui/mapper/SizeMapper.cpp @@ -2,10 +2,7 @@ namespace cru::ui::mapper { bool SizeMapper::XmlElementIsOfThisType(xml::XmlElementNode* node) { - if (node->GetTag() == u"Size") { - return true; - } - return false; + return node->GetTag().CaseInsensitiveCompare(u"Size") == 0; } Size SizeMapper::DoMapFromString(String str) { @@ -20,7 +17,7 @@ Size SizeMapper::DoMapFromString(String str) { } Size SizeMapper::DoMapFromXml(xml::XmlElementNode* node) { - auto value_attr = node->GetOptionalAttribute(u"value"); + auto value_attr = node->GetOptionalAttributeCaseInsensitive(u"value"); if (!value_attr) return {}; return DoMapFromString(*value_attr); } diff --git a/src/ui/mapper/ThicknessMapper.cpp b/src/ui/mapper/ThicknessMapper.cpp index 1829d64b..b72f952c 100644 --- a/src/ui/mapper/ThicknessMapper.cpp +++ b/src/ui/mapper/ThicknessMapper.cpp @@ -3,10 +3,7 @@ namespace cru::ui::mapper { bool ThicknessMapper::XmlElementIsOfThisType(xml::XmlElementNode* node) { - if (node->GetTag() == u"Thickness") { - return true; - } - return false; + return node->GetTag().CaseInsensitiveCompare(u"Thickness") == 0; } Thickness ThicknessMapper::DoMapFromString(String str) { @@ -23,7 +20,7 @@ Thickness ThicknessMapper::DoMapFromString(String str) { } Thickness ThicknessMapper::DoMapFromXml(xml::XmlElementNode* node) { - auto value_attr = node->GetOptionalAttribute(u"value"); + auto value_attr = node->GetOptionalAttributeCaseInsensitive(u"value"); if (!value_attr) return {}; return DoMapFromString(*value_attr); } |