Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added buffers::string_view_buffer #74

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading