From a6b5b8b879a9a587ec0ad605722d5d6428d5e68c Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Fri, 17 Oct 2025 09:26:58 +0800 Subject: XML and mapper remove String. --- include/cru/base/StringUtil.h | 10 ++++++ include/cru/ui/mapper/ColorMapper.h | 2 +- include/cru/ui/mapper/CursorMapper.h | 2 +- include/cru/ui/mapper/Mapper.h | 4 +-- include/cru/ui/mapper/MeasureLengthMapper.h | 2 +- include/cru/ui/mapper/PointMapper.h | 2 +- include/cru/ui/mapper/SizeMapper.h | 2 +- include/cru/ui/mapper/StringMapper.h | 2 +- include/cru/ui/mapper/ThicknessMapper.h | 2 +- include/cru/xml/XmlNode.h | 52 ++++++++++++++--------------- include/cru/xml/XmlParser.h | 17 ++++------ 11 files changed, 51 insertions(+), 46 deletions(-) (limited to 'include') diff --git a/include/cru/base/StringUtil.h b/include/cru/base/StringUtil.h index ef6359ff..f2373444 100644 --- a/include/cru/base/StringUtil.h +++ b/include/cru/base/StringUtil.h @@ -2,12 +2,22 @@ #include "Base.h" #include "Bitmask.h" +#include #include #include +#include #include #include namespace cru { +namespace string { +std::weak_ordering CaseInsensitiveCompare(std::string_view left, + std::string_view right); +std::string TrimBegin(std::string_view str); +std::string TrimEnd(std::string_view str); +std::string Trim(std::string_view str); +} // namespace string + namespace details { struct SplitOptionsTag {}; } // namespace details diff --git a/include/cru/ui/mapper/ColorMapper.h b/include/cru/ui/mapper/ColorMapper.h index 785f1ca9..e3846712 100644 --- a/include/cru/ui/mapper/ColorMapper.h +++ b/include/cru/ui/mapper/ColorMapper.h @@ -13,7 +13,7 @@ class CRU_UI_API ColorMapper : public BasicMapper { bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; protected: - Color DoMapFromString(String str) override; + Color DoMapFromString(std::string str) override; Color DoMapFromXml(xml::XmlElementNode* node) override; }; } // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/CursorMapper.h b/include/cru/ui/mapper/CursorMapper.h index 8cce53c4..951291c1 100644 --- a/include/cru/ui/mapper/CursorMapper.h +++ b/include/cru/ui/mapper/CursorMapper.h @@ -14,7 +14,7 @@ class CRU_UI_API CursorMapper : public BasicSharedPtrMapper DoMapFromString(String str) override; + std::shared_ptr DoMapFromString(std::string str) override; std::shared_ptr DoMapFromXml( xml::XmlElementNode* node) override; }; diff --git a/include/cru/ui/mapper/Mapper.h b/include/cru/ui/mapper/Mapper.h index d391fac3..f7467120 100644 --- a/include/cru/ui/mapper/Mapper.h +++ b/include/cru/ui/mapper/Mapper.h @@ -64,7 +64,7 @@ class CRU_UI_API BasicMapper : public MapperBase { ~BasicMapper() override = default; - virtual T MapFromString(String str) { + virtual T MapFromString(std::string str) { if (!SupportMapFromString()) { throw Exception("This mapper does not support map from string."); } @@ -85,7 +85,7 @@ class CRU_UI_API BasicMapper : public MapperBase { } protected: - virtual T DoMapFromString(String str) { return {}; } + virtual T DoMapFromString(std::string str) { return {}; } virtual T DoMapFromXml(xml::XmlElementNode* node) { return {}; } }; diff --git a/include/cru/ui/mapper/MeasureLengthMapper.h b/include/cru/ui/mapper/MeasureLengthMapper.h index 9b7ecfd8..347d69ae 100644 --- a/include/cru/ui/mapper/MeasureLengthMapper.h +++ b/include/cru/ui/mapper/MeasureLengthMapper.h @@ -14,7 +14,7 @@ class CRU_UI_API MeasureLengthMapper bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; protected: - render::MeasureLength DoMapFromString(String str) override; + render::MeasureLength DoMapFromString(std::string str) override; render::MeasureLength DoMapFromXml(xml::XmlElementNode* node) override; }; } // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/PointMapper.h b/include/cru/ui/mapper/PointMapper.h index 83dd135b..b3c97df0 100644 --- a/include/cru/ui/mapper/PointMapper.h +++ b/include/cru/ui/mapper/PointMapper.h @@ -12,7 +12,7 @@ class CRU_UI_API PointMapper : public BasicMapper { bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; protected: - Point DoMapFromString(String str) override; + Point DoMapFromString(std::string str) override; Point DoMapFromXml(xml::XmlElementNode* node) override; }; } // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/SizeMapper.h b/include/cru/ui/mapper/SizeMapper.h index 6cf3eb9a..4d781ee3 100644 --- a/include/cru/ui/mapper/SizeMapper.h +++ b/include/cru/ui/mapper/SizeMapper.h @@ -12,7 +12,7 @@ class CRU_UI_API SizeMapper : public BasicMapper { bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; protected: - Size DoMapFromString(String str) override; + Size DoMapFromString(std::string str) override; Size DoMapFromXml(xml::XmlElementNode* node) override; }; } // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/StringMapper.h b/include/cru/ui/mapper/StringMapper.h index 8a907591..bfb7314b 100644 --- a/include/cru/ui/mapper/StringMapper.h +++ b/include/cru/ui/mapper/StringMapper.h @@ -12,7 +12,7 @@ class CRU_UI_API StringMapper : public BasicMapper { bool SupportMapFromXml() override { return true; } protected: - String DoMapFromString(String str) override; + String DoMapFromString(std::string str) override; String DoMapFromXml(xml::XmlElementNode* node) override; }; } // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/ThicknessMapper.h b/include/cru/ui/mapper/ThicknessMapper.h index fab1e5d3..84b6b036 100644 --- a/include/cru/ui/mapper/ThicknessMapper.h +++ b/include/cru/ui/mapper/ThicknessMapper.h @@ -16,7 +16,7 @@ class CRU_UI_API ThicknessMapper : public BasicMapper { bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; protected: - Thickness DoMapFromString(String str) override; + Thickness DoMapFromString(std::string str) override; Thickness DoMapFromXml(xml::XmlElementNode* node) override; }; } // namespace cru::ui::mapper diff --git a/include/cru/xml/XmlNode.h b/include/cru/xml/XmlNode.h index a7d1bf38..1574f6fa 100644 --- a/include/cru/xml/XmlNode.h +++ b/include/cru/xml/XmlNode.h @@ -2,11 +2,9 @@ #include "Base.h" -#include "cru/base/String.h" - -#include +#include +#include #include -#include #include namespace cru::xml { @@ -53,7 +51,7 @@ class CRU_XML_API XmlNode { class CRU_XML_API XmlTextNode : public XmlNode { public: XmlTextNode() : XmlNode(Type::Text) {} - explicit XmlTextNode(String text) + explicit XmlTextNode(std::string text) : XmlNode(Type::Text), text_(std::move(text)) {} CRU_DELETE_COPY(XmlTextNode) @@ -62,20 +60,20 @@ class CRU_XML_API XmlTextNode : public XmlNode { ~XmlTextNode() override = default; public: - String GetText() const { return text_; } - void SetText(String text) { text_ = std::move(text); } + std::string GetText() const { return text_; } + void SetText(std::string text) { text_ = std::move(text); } XmlNode* Clone() const override { return new XmlTextNode(text_); } private: - String text_; + std::string text_; }; class CRU_XML_API XmlElementNode : public XmlNode { public: XmlElementNode() : XmlNode(Type::Element) {} - explicit XmlElementNode(String tag, - std::unordered_map attributes = {}) + explicit XmlElementNode(std::string tag, + std::unordered_map attributes = {}) : XmlNode(Type::Element), tag_(std::move(tag)), attributes_(std::move(attributes)) {} @@ -86,24 +84,24 @@ class CRU_XML_API XmlElementNode : public XmlNode { ~XmlElementNode() override; public: - String GetTag() const { return tag_; } - void SetTag(String tag) { tag_ = std::move(tag); } - const std::unordered_map& GetAttributes() const { + std::string GetTag() const { return tag_; } + void SetTag(std::string tag) { tag_ = std::move(tag); } + const std::unordered_map& GetAttributes() const { return attributes_; } - void SetAttributes(std::unordered_map attributes) { + void SetAttributes(std::unordered_map attributes) { attributes_ = std::move(attributes); } const std::vector GetChildren() const { return children_; } Index GetChildCount() const { return children_.size(); } - String GetAttributeValue(const String& key) const { + std::string GetAttributeValue(const std::string& key) const { return attributes_.at(key); } - String GetAttributeValueCaseInsensitive(const String& key) const { + std::string GetAttributeValueCaseInsensitive(const std::string& key) const { return *GetOptionalAttributeValueCaseInsensitive(key); } - std::optional GetOptionalAttributeValue(const String& key) const { + std::optional GetOptionalAttributeValue(const std::string& key) const { auto it = attributes_.find(key); if (it == attributes_.end()) { return std::nullopt; @@ -111,10 +109,10 @@ class CRU_XML_API XmlElementNode : public XmlNode { return it->second; } - std::optional GetOptionalAttributeValueCaseInsensitive( - const String& key) const { + std::optional GetOptionalAttributeValueCaseInsensitive( + const std::string& key) const { for (auto it = attributes_.begin(); it != attributes_.end(); ++it) { - if (it->first.CaseInsensitiveCompare(key) == 0) { + if (cru::string::CaseInsensitiveCompare(it->first, key) == 0) { return it->second; } } @@ -124,7 +122,7 @@ class CRU_XML_API XmlElementNode : public XmlNode { XmlNode* GetChildAt(Index index) const { return children_[index]; } - void AddAttribute(String key, String value); + void AddAttribute(std::string key, std::string value); void AddChild(XmlNode* child); Index GetChildElementCount() const; @@ -133,15 +131,15 @@ class CRU_XML_API XmlElementNode : public XmlNode { XmlNode* Clone() const override; private: - String tag_; - std::unordered_map attributes_; + std::string tag_; + std::unordered_map attributes_; std::vector children_; }; class CRU_XML_API XmlCommentNode : public XmlNode { public: XmlCommentNode() : XmlNode(Type::Comment) {} - explicit XmlCommentNode(String text) + explicit XmlCommentNode(std::string text) : XmlNode(Type::Comment), text_(std::move(text)) {} CRU_DELETE_COPY(XmlCommentNode) @@ -149,12 +147,12 @@ class CRU_XML_API XmlCommentNode : public XmlNode { ~XmlCommentNode() override; - String GetText() const { return text_; } - void SetText(String text) { text_ = std::move(text); } + std::string GetText() const { return text_; } + void SetText(std::string text) { text_ = std::move(text); } XmlNode* Clone() const override; private: - String text_; + std::string text_; }; } // namespace cru::xml diff --git a/include/cru/xml/XmlParser.h b/include/cru/xml/XmlParser.h index ca4fb54a..b6589f1b 100644 --- a/include/cru/xml/XmlParser.h +++ b/include/cru/xml/XmlParser.h @@ -3,9 +3,6 @@ #include "XmlNode.h" #include "cru/base/Exception.h" -#include "cru/base/String.h" - -#include namespace cru::xml { class CRU_XML_API XmlParsingException : public Exception { @@ -15,7 +12,7 @@ class CRU_XML_API XmlParsingException : public Exception { class CRU_XML_API XmlParser { public: - explicit XmlParser(String xml); + explicit XmlParser(std::string xml); CRU_DELETE_COPY(XmlParser) CRU_DELETE_MOVE(XmlParser) @@ -28,19 +25,19 @@ class CRU_XML_API XmlParser { XmlElementNode* DoParse(); char16_t Read1(); - String ReadWithoutAdvance(int count = 1); + std::string ReadWithoutAdvance(int count = 1); void ReadSpacesAndDiscard(); - String ReadSpaces(); - String ReadIdenitifier(); - String ReadAttributeString(); + std::string ReadSpaces(); + std::string ReadIdenitifier(); + std::string ReadAttributeString(); private: - String xml_; + std::string xml_; XmlElementNode* cache_ = nullptr; // Consider the while file enclosed by a single tag called $root. - XmlElementNode* pseudo_root_node_ = new XmlElementNode(u"$root"); + XmlElementNode* pseudo_root_node_ = new XmlElementNode("$root"); XmlElementNode* current_ = pseudo_root_node_; int current_position_ = 0; }; -- cgit v1.2.3