27 releases (12 stable)

3.1.1 Jul 8, 2024
3.0.0 Mar 25, 2024
2.1.0 Mar 24, 2024
1.2.2 Dec 14, 2023
0.5.2 Sep 25, 2023

#522 in Parser implementations

Download history 3/week @ 2024-08-12 46/week @ 2024-08-19 45/week @ 2024-08-26 24/week @ 2024-09-02 13/week @ 2024-09-09 6/week @ 2024-09-16 27/week @ 2024-09-23 13/week @ 2024-09-30 1/week @ 2024-10-07 19/week @ 2024-10-14 20/week @ 2024-10-21 1/week @ 2024-10-28 16/week @ 2024-11-04 10/week @ 2024-11-11 32/week @ 2024-11-18 53/week @ 2024-11-25

111 downloads per month
Used in 2 crates

MIT license

14KB
172 lines

byte_reader

A minimal byte-by-byte reader for parsing input.

test status of byte_reader crates.io

Use case

Following situation:

I want to read and parse some input, but it's not so large-scale parsing task, so I'd like to avoid adding a heavyweight crate like nom or nom8 to my dependencies ...

Of course, byte_reader supports no std environment.


Usage

use byte_reader::Reader;

fn main() {
    // Get an input `&[u8]` from a File, standard input, or others
    let sample_input = "Hello,    byte_reader!".as_bytes();

    // Create mutable `r` for the input
    let mut r = Reader::new(sample_input);

    // Use some simple operations
    // to parse the input
    r.consume("Hello").unwrap();
    r.consume(",").unwrap();
    r.skip_whitespace();
    let name = r.read_while(|b| b != &b'!'); // b"byte_reader"
    let name = String::from_utf8_lossy(name).to_string();
    r.consume("!").unwrap();

    println!("Greeted to `{name}`.");
}

Operations

  • remaining
  • read_while, read_until
  • next, next_if
  • peek, peek2, peek3
  • advance_by, unwind_by
  • consume, consume_oneof
  • skip_while, skip_whitespace

Features

"location"

Enable tracking reader's location, line and column (1-origin), in the input bytes.

"text"

Some utility methods for text-parsing are available:

  • read_quoted_by
  • read_uint, read_int
  • read_camel, read_snake, read_kebab

License

byte_reader is licensed under the MIT License (LICENSE or https://opensource.org/licenses/MIT).

No runtime deps

Features