boost::capy::read_until

Asynchronously read until a delimiter string is found.

Synopsis

template<
    ReadStream Stream,
    class B>
requires DynamicBufferParam<B&&>
auto
read_until(
    Stream& stream,
    B&& buffers,
    std::string_view delim,
    std::size_t initial_amount = 2048);

Description

Reads data from the stream until the delimiter is found. This is a convenience overload equivalent to calling read_until with match_delim{delim}. If the delimiter already exists in the buffer, returns immediately without I/O.

  • The operation completes when:

  • The delimiter string is found

  • End‐of‐stream is reached (`cond::eof`)

  • The buffer's `max_size()` is reached (`cond::not_found`)

  • An error occurs

  • The operation is cancelled

Cancellation

Supports cancellation via stop_token propagated through the IoAwaitable protocol. When cancelled, returns with cond::canceled.

  • `cond::eof` ‐ EOF before delimiter; `n` is buffer size

  • `cond::not_found` ‐ `max_size()` reached before delimiter

  • `cond::canceled` ‐ Operation was cancelled

Example

task<std::string> read_line( ReadStream auto& stream )
{
    std::string line;
    auto [ec, n] = co_await read_until(
        stream, string_dynamic_buffer( &line ), "\r\n" );
    if( ec == cond::eof )
        co_return line;  // partial line at EOF
    if( ec )
        detail::throw_system_error( ec );
    line.resize( n - 2 );  // remove "\r\n"
    co_return line;
}

Return Value

An awaitable yielding (error_code, std::size_t). On success, n is bytes up to and including the delimiter. Compare error codes to conditions:

Parameters

Name Description

stream

The stream to read from. The caller retains ownership.

buffers

The dynamic buffer to append data to. Must remain valid until the operation completes.

delim

The delimiter string to search for.

initial_amount

Initial bytes to read per iteration (default 2048). Grows by 1.5x when filled.

See Also

read_until, match_delim, DynamicBufferParam

Created with MrDocs