Skip to content
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
2 changes: 1 addition & 1 deletion include/boost/capy/read_until.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ struct read_until_awaitable
await_suspend(std::coroutine_handle<> h, io_env const* env)
{
inner_.emplace(read_until_match_impl(
*stream_, buffers(), match_, initial_amount_));
*stream_, buffers(), std::move(match_), initial_amount_));
return inner_->await_suspend(h, env);
}

Expand Down
49 changes: 49 additions & 0 deletions test/unit/read_until.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
#include "test_suite.hpp"

#include <cstring>
#include <memory>
#include <span>
#include <string>
#include <string_view>
#include <type_traits>

namespace boost {
namespace capy {
Expand Down Expand Up @@ -451,6 +453,52 @@ struct read_until_test
}));
}

void
testMoveOnlyMatcher()
{
struct move_only_matcher
{
std::unique_ptr<std::string> delim;

explicit move_only_matcher(std::string d)
: delim(std::make_unique<std::string>(std::move(d)))
{
}

std::size_t
operator()(
std::string_view data,
std::size_t* hint) const
{
auto pos = data.find(*delim);
if(pos != std::string_view::npos)
return pos + delim->size();
if(hint)
*hint = delim->size() > 1 ? delim->size() - 1 : 0;
return std::string_view::npos;
}
};

static_assert(MatchCondition<move_only_matcher>);
static_assert(! std::is_copy_constructible_v<move_only_matcher>);

// Chunked reads route the matcher through await_suspend, which
// moves rather than copies it.
BOOST_TEST(test::fuse().armed([](test::fuse& f) -> task<void>
{
test::read_stream rs(f, 4);
rs.provide("hello\r\nworld");

std::string data;
auto [ec, n] = co_await read_until(
rs, dynamic_buffer(data), move_only_matcher("\r\n"));
if(ec)
co_return;

BOOST_TEST_EQ(n, 7u); // "hello\r\n"
}));
}

//----------------------------------------------------------

void
Expand Down Expand Up @@ -486,6 +534,7 @@ struct read_until_test
testErrorConditions();
testPrefilledBuffer();
testMatchCondition();
testMoveOnlyMatcher();
testMatchHelpers();
}
};
Expand Down
Loading