Skip to content

Commit

Permalink
Merge pull request #27 from Adamska1008/26-feature-implement-custom-l…
Browse files Browse the repository at this point in the history
…iteral-operators-for-element-and-document

feat(doc, elem): custom string literal
  • Loading branch information
Adamska1008 authored Sep 1, 2024
2 parents be5b81a + bd0a9e6 commit 5727b67
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ target_link_libraries(your_target PRIVATE myxml)

## Usage

Here’s a basic example:
Here’s a **basic example**:

```c++
#include <myxml/element.hpp>
Expand All @@ -111,7 +111,23 @@ int main()
}
```

Parse file:
**Create Element using custom string literal**:

```C++
#include <myxml/element.hpp>
// enable literals by using namespace
using namespace xml::literals;

int main()
{
auto elem = "<root></root>"_elem;
std::cout << elem->GetName() << std::endl;
}
```

Similar Approach for `myxml::Document`.

**Parse file**:

```C++
#include <myxml/document.hpp>
Expand Down
7 changes: 6 additions & 1 deletion include/myxml/document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace myxml
std::shared_ptr<Text> FirstText();

/** Load */
static Document Parse(std::string);
static Document Parse(std::string_view);
static Document ParseFile(std::string fileName);

/* Exportable */
Expand All @@ -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);
}
}
6 changes: 6 additions & 0 deletions include/myxml/element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Element> operator""_elem(const char *, std::size_t);
}
}
7 changes: 6 additions & 1 deletion src/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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));
}
}
5 changes: 5 additions & 0 deletions src/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,9 @@ namespace myxml
builder += indent + "</" + std::string(this->GetName()) + ">\n";
return builder;
}

std::shared_ptr<Element> literals::operator""_elem(const char *literal, std::size_t len)
{
return Element::Parse(std::string_view(literal, len));
}
}
7 changes: 7 additions & 0 deletions tests/document_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<root></root>"_doc;
REQUIRE(doc.GetRoot()->GetName() == "root");
}
7 changes: 7 additions & 0 deletions tests/element_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<root></root>"_elem;
REQUIRE(elem->GetName() == "root");
}

0 comments on commit 5727b67

Please sign in to comment.