#parser #buffer #peekable

peek-buffer

Generic-typed buffer with ability to peek into the future! (And the past too.)

1 unstable release

0.1.2 Jun 20, 2024
0.1.1 Jun 20, 2024
0.1.0 Jun 20, 2024

#6 in #peekable

GPL-3.0 license

15KB
289 lines

peek-buffer Crates.io Version docs.rs


Essential for parsing.

let mut buffer = PeekBuffer::from("Hello, World!");

// peek without consuming
while let Some(&c) = buffer.peek() {
    println!("{}", c);

    // advance to the next item (char in this situation)
    buffer.advance_char();
    // generally you should use buffer.advance() to move to the next element
    // but if you're parsing a string, you should use buffer.advance_char()
    // since it keeps track of the line and the column the cursor is at.
}

// peek with consuming
buffer.rewind_to_beginning();
while let Some(&c) = buffer.eat() {
    println!("{}", c);
    buffer.advance_char();
}

println!("{}", buffer.len());

No runtime deps