boost::capy::test::bufgrind

Iterates split points of a buffer sequence into two adjacent halves.

Synopsis

template<ConstBufferSequence BS>
class bufgrind;

Description

This class iterates through all possible ways to split a buffer sequence into two parts (b1, b2) where concatenating them yields the original sequence. It uses an async‐generator‐like pattern that allows co_await between iterations.

The split type automatically preserves mutability: passing a MutableBufferSequence yields halves that model MutableBufferSequence, while passing a ConstBufferSequence yields halves that model ConstBufferSequence. Each half is the buffer‐sequence view exposed by a buffer_slice over the corresponding byte range, and can be passed directly to read_some, write_some, buffer_size, etc.

Thread Safety

Not thread‐safe.

Example

void
bufgrind_walk_splits_demo()
{
    // Test all split points of a buffer
    std::string data = "hello world";
    auto cb = make_buffer( data );

    fuse f;
    auto r = f.inert( [&]( fuse& ) -> task<> {
        bufgrind bg( cb );
        while( bg ) {
            auto [b1, b2] = co_await bg.next();
            // b1 contains first N bytes (as a buffer sequence)
            // b2 contains remaining bytes (as a buffer sequence)
            // concatenating b1 + b2 equals original
            BOOST_TEST( buffer_to_string( b1, b2 ) == data );
        }
    } );
}

Mutable Buffer Example

// Mutable buffers preserve mutability
char data[100];
mutable_buffer buf( data, sizeof( data ) );

task<>
bufgrind_walk_mutable()
{
    bufgrind bg( buf );
    while( bg ) {
        auto [b1, b2] = co_await bg.next();
        // b1, b2 yield mutable_buffer when iterated
        static_assert( MutableBufferSequence<decltype(b1)> );
        static_assert( MutableBufferSequence<decltype(b2)> );
    }
}

Step Size Example

// Skip by 10 bytes for faster iteration
const_buffer bufgrind_step_data( "0123456789ABCDE", 15 );

task<>
bufgrind_walk_by_step()
{
    bufgrind bg( bufgrind_step_data, 10 );
    while( bg ) {
        auto [b1, b2] = co_await bg.next();
        // Visits positions 0, 10, 20, ..., and always size
    }
}

Type Aliases

Name

Description

slice_type

Names the buffer‐sequence type buffer_slice yields for each half.

split_type

Pairs the two slice_type halves that next yields together.

Member Functions

Name

Description

bufgrind [constructor]

Construct a buffer grinder.

next

Return the next split point.

operator bool

Check if more split points remain.

See Also

buffer_slice

Created with MrDocs