Skip to content

Commit

Permalink
feat(text.hpp): trimmed and trim
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamska1008 committed Sep 6, 2024
1 parent 17020e2 commit 667b1b6
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 18 deletions.
8 changes: 5 additions & 3 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ REQUIRE(root.first_elem().name() == "child");
element next("next");
root.push_front(next);
REQUIRE(root.first_elem().name() == "next");
root.push_back(text("Hello"));
REQUIRE(root.first_text().str() == "Hello");
```
### Text
Expand All @@ -84,10 +86,10 @@ text txt = "Hello";
text txt = root.first_text();
// print raw
fmt::println(txt);
// print trimmed text, it returns std::string and will not modify it
fmt::println(txt.trimmed());
// yet it will modify. return a new text
// print trimmed string, it returns std::string and will not modify it
fmt::println(txt.trim());
// return a new trimmed text
fmt::println(txt.trimmed());
```

### CData
Expand Down
35 changes: 21 additions & 14 deletions include/myxml/text.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
#pragma once
#include <string>
#include "myxml/node.hpp"
#include "myxml/interface.hpp"

namespace myxml
{

class text : public printable
struct text_impl : public node
{
std::string inner;

text_impl() = default;
explicit text_impl(std::string_view str);

virtual ~text_impl() = default;

/* Implment Exportable*/
virtual void print(std::ostream &) const override;
};

class text : public printable, public interface
{
friend class element;

Expand All @@ -15,26 +29,19 @@ namespace myxml
text(std::shared_ptr<text_impl>);

public:
text(std::string_view);
text(const std::string &);
text(std::string &&);

std::string trimmed();
text trim();

/* Implement printable */
virtual void print(std::ostream &) const override;
virtual void entity_encoding(bool) override;
virtual void platform_specific_newline(bool) override;
};

struct text_impl : public node
{
std::string inner;

text_impl() = default;
explicit text_impl(std::string_view str);

virtual ~text_impl() = default;

/* Implment Exportable*/
virtual void print(std::ostream &) const override;
/* Implement interface */
virtual std::shared_ptr<node> impl() { return _impl; }
};

namespace util
Expand Down
18 changes: 17 additions & 1 deletion src/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,28 @@ namespace myxml
_impl->inner = str;
}

text::text(std::string_view str)
text::text(const std::string &str)
{
_impl = std::make_shared<text_impl>();
_impl->inner = str;
}

std::string text::trimmed()
{
std::string trimmed;
auto is_space = [](char ch)
{ return std::isspace(static_cast<char>(ch)); };
auto &ref = _impl->inner;
auto start = std::find_if_not(ref.begin(), ref.end(), is_space);
auto end = std::find_if_not(ref.rbegin(), ref.rend(), is_space).base();
return (start < end) ? std::string(start, end) : std::string();
}

text text::trim()
{
return this->trimmed();
}

void text::print(std::ostream &os) const
{
this->_impl->print(os);
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ add_custom_test(parser_test)
add_custom_test(printable_test)
add_custom_test(document_test)
add_custom_test(buffer_test)
add_custom_test(text_test)
2 changes: 2 additions & 0 deletions tests/element_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ TEST_CASE("Element Interface", "[element]")
element next("next");
root.push_front(next);
REQUIRE(root.first_elem().name() == "next");
root.push_back(text("Hello"));
REQUIRE(root.first_text().str() == "Hello");
}

SECTION("query by name")
Expand Down
14 changes: 14 additions & 0 deletions tests/text_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <catch2/catch_test_macros.hpp>
#include "myxml/text.hpp"

TEST_CASE("text interface", "[text]")
{
using namespace myxml;

SECTION("trim")
{
text text(" \n\n Hello \n \t ");
REQUIRE(text.trimmed() == "Hello");
REQUIRE(text.trim().str() == "Hello");
}
}

0 comments on commit 667b1b6

Please sign in to comment.