From bd0a9e6457256700eb09444a96d6281c900e2030 Mon Sep 17 00:00:00 2001 From: adamska <2639980868@qq.com> Date: Sun, 1 Sep 2024 22:00:41 +0800 Subject: [PATCH] feat(doc, elem): custom string literal --- README.md | 20 ++++++++++++++++++-- include/myxml/document.hpp | 7 ++++++- include/myxml/element.hpp | 6 ++++++ src/document.cpp | 7 ++++++- src/element.cpp | 5 +++++ tests/document_test.cpp | 7 +++++++ tests/element_test.cpp | 7 +++++++ 7 files changed, 55 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7f923ac..6d9ebd1 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ target_link_libraries(your_target PRIVATE myxml) ## Usage -Here’s a basic example: +Here’s a **basic example**: ```c++ #include @@ -111,7 +111,23 @@ int main() } ``` -Parse file: +**Create Element using custom string literal**: + +```C++ +#include +// enable literals by using namespace +using namespace xml::literals; + +int main() +{ + auto elem = ""_elem; + std::cout << elem->GetName() << std::endl; +} +``` + +Similar Approach for `myxml::Document`. + +**Parse file**: ```C++ #include diff --git a/include/myxml/document.hpp b/include/myxml/document.hpp index 932231c..8033b8c 100644 --- a/include/myxml/document.hpp +++ b/include/myxml/document.hpp @@ -42,7 +42,7 @@ namespace myxml std::shared_ptr FirstText(); /** Load */ - static Document Parse(std::string); + static Document Parse(std::string_view); static Document ParseFile(std::string fileName); /* Exportable */ @@ -58,4 +58,9 @@ namespace myxml bool isValidXmlEncoding(std::string_view); bool isValidXmlStandalone(std::string_view); } + + namespace literals + { + Document operator""_doc(const char *, std::size_t); + } } diff --git a/include/myxml/element.hpp b/include/myxml/element.hpp index 910fd33..9d1015a 100644 --- a/include/myxml/element.hpp +++ b/include/myxml/element.hpp @@ -49,4 +49,10 @@ namespace myxml virtual std::string ExportRaw() const override; virtual std::string ExportFormatted(int indentLevel = 0, int indentSize = 4) const override; }; + + namespace literals + { + // Custom String Literal for Element + std::shared_ptr operator""_elem(const char *, std::size_t); + } } diff --git a/src/document.cpp b/src/document.cpp index aa11a6c..88692e1 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -51,7 +51,7 @@ namespace myxml return this->root->FirstText(); } - Document Document::Parse(std::string input) + Document Document::Parse(std::string_view input) { return Parser(input).ParseDocument(); } @@ -153,4 +153,9 @@ namespace myxml return standalone == "yes" || standalone == "no"; } } + + Document literals::operator""_doc(const char *literal, std::size_t len) + { + return Document::Parse(std::string_view(literal, len)); + } } diff --git a/src/element.cpp b/src/element.cpp index bab930f..bf2cc52 100644 --- a/src/element.cpp +++ b/src/element.cpp @@ -101,4 +101,9 @@ namespace myxml builder += indent + "GetName()) + ">\n"; return builder; } + + std::shared_ptr literals::operator""_elem(const char *literal, std::size_t len) + { + return Element::Parse(std::string_view(literal, len)); + } } \ No newline at end of file diff --git a/tests/document_test.cpp b/tests/document_test.cpp index 53a2b9c..b5d7481 100644 --- a/tests/document_test.cpp +++ b/tests/document_test.cpp @@ -25,4 +25,11 @@ TEST_CASE("Simple document", "[document]") REQUIRE(doc.GetDeclartion().version == "1.0"); REQUIRE(doc.GetDeclartion().encoding == "UTF-8"); } +} + +TEST_CASE("Custom String Literal", "[document]") +{ + using namespace myxml::literals; + auto doc = ""_doc; + REQUIRE(doc.GetRoot()->GetName() == "root"); } \ No newline at end of file diff --git a/tests/element_test.cpp b/tests/element_test.cpp index 4d58d57..cbe6ad0 100644 --- a/tests/element_test.cpp +++ b/tests/element_test.cpp @@ -45,4 +45,11 @@ TEST_CASE("Element Functionality", "[element]") (*root)["hello"] = "bar"; REQUIRE((*root)["hello"] == "bar"); } +} + +TEST_CASE("Custom String Literal", "[Element]") +{ + using namespace myxml::literals; + auto elem = ""_elem; + REQUIRE(elem->GetName() == "root"); } \ No newline at end of file