Skip to content

Commit

Permalink
Added buffers::string_view_buffer (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonotsugipaa authored Feb 6, 2024
1 parent 18c5669 commit 616c788
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
21 changes: 21 additions & 0 deletions include/ctpg/ctpg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,27 @@ namespace buffers
std::string str;
};

class string_view_buffer
{
public:
string_view_buffer(const std::string_view& str):
str(str)
{}

auto begin() const { return str.cbegin(); }
auto end() const { return str.cend(); }

using iterator = std::string_view::const_iterator;

std::string_view get_view(iterator start, iterator end) const
{
return std::string_view(str.data() + (start - str.begin()), end - start);
}

private:
std::string_view str;
};

template<typename Buffer>
using iterator_t = typename Buffer::iterator;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/buffers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,20 @@ TEST_CASE("string buffer", "[buffers]")
REQUIRE(result.has_value());
REQUIRE(result.value() == 42);
}

TEST_CASE("string_view buffer, lvalue", "[buffers]")
{
std::string_view txt(" 0 1 2 ");
string_view_buffer buf(txt);
auto result = test::p1.parse(buf);
REQUIRE(result.has_value());
REQUIRE(result.value() == 42);
}

TEST_CASE("string_view buffer, rvalue", "[buffers]")
{
string_view_buffer buf(std::string_view(" 0 1 2 "));
auto result = test::p1.parse(buf);
REQUIRE(result.has_value());
REQUIRE(result.value() == 42);
}

0 comments on commit 616c788

Please sign in to comment.