byte_stream_splitter

Crates.iobyte_stream_splitter
lib.rsbyte_stream_splitter
version0.2.0
sourcesrc
created_at2015-12-10 11:44:35.593496
updated_at2023-08-20 22:48:13.589162
descriptionSplitting a stream of bytes by separator sequence
homepagehttps://github.com/forgemo/byte_stream_splitter
repositoryhttps://github.com/forgemo/byte_stream_splitter
max_upload_size
id3605
size13,249
Publish tracing (github:tokio-rs:publish-tracing)

documentation

https://github.com/forgemo/byte_stream_splitter/blob/master/README.md

README

Build Status

byte_stream_splitter

Rust library for splitting byte streams.

    // Prepare your separator sequence.
    let separator = [0x00, 0x00];        

    // Prepare your data byte stream.
    // This can be anything implementing the BufRead trait.
    let mut data = io::Cursor::new(vec![
        0xAA, 0xAB,                     // Prefix
        0x00, 0x00, 0x01, 0x02, 0x03,   // FullMatch
        0x00, 0x00, 0x04, 0x05, 0x06,   // FullMatch
        0x00, 0x00, 0x07, 0x08          // Suffix
        ]);

    // The splitter implements the Iterator trait and can be used as such.
    // You can iterate through the matches via next() or next_to_buf().
    // Use next() if you don't care about holding the whole match in memory while searching for the next separator.
    // Use next_to_buf() if you want to directly handle the matched bytes while scanning for the next separator.  

    // The first match contains all bytes until the first separator sequence is detected (Prefix).
    // The last match contains all bytes starting from the last detected separator sequence. (Suffix)
    // All other matches between the prefix and suffix contain all the bytes from a separator sequence until the next one starts.

    // Note: If the stream immediately starts with the separator, the prefix will still be returned empty.

    let mut splitter = ByteStreamSplitter::new(&mut data, &separator);
    let prefix = splitter.next().unwrap().unwrap();
    let match1 = splitter.next().unwrap().unwrap();
    let match2 = splitter.next().unwrap().unwrap();
    let suffix = splitter.next().unwrap().unwrap();

    assert_eq!(prefix, vec![0xAA, 0xAB]);
    assert_eq!(match1, vec![0x01, 0x02, 0x03]);
    assert_eq!(match2, vec![0x04, 0x05, 0x06]);
    assert_eq!(suffix, vec![0x07, 0x08]);
Commit count: 19

cargo fmt