Struct byte_reader::Reader
source · pub struct Reader<B: Bytes> { /* private fields */ }
Implementations§
source§impl<B: Bytes> Reader<B>
impl<B: Bytes> Reader<B>
sourcepub fn new(content: B) -> Self
pub fn new(content: B) -> Self
Generate new Reader
from &str | String | &[u8] | Vec<u8>
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
// Get a input from a File, standard input, or others
// Input can be `&str`, `String`, `&[u8]`, or `Vec<u8>`
let sample_input = "Hello, byte_reader!";
// Create mutable `r`
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_snake().unwrap(); // byte_reader
r.consume("!").unwrap();
println!("Greeted to `{name}`.");
}
source§impl<B: Bytes> Reader<B>
impl<B: Bytes> Reader<B>
sourcepub fn advance_by(&mut self, max: usize)
pub fn advance_by(&mut self, max: usize)
Advance by max
bytes (or, if remained bytes is shorter than max
, read all remained bytes)
sourcepub fn unwind_by(&mut self, max: usize)
pub fn unwind_by(&mut self, max: usize)
Unwind the parsing point by max
bytes (or, if already-read bytes is shorter than max
, rewind all)
When "location"
feature is activated, this may be less performant for some extensive input
sourcepub fn skip_while(&mut self, condition: impl Fn(&u8) -> bool)
pub fn skip_while(&mut self, condition: impl Fn(&u8) -> bool)
Skip next byte while condition
holds on it
sourcepub fn skip_whitespace(&mut self)
pub fn skip_whitespace(&mut self)
.skip_while(|b| b.is_ascii_whitespace())
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
// Get a input from a File, standard input, or others
// Input can be `&str`, `String`, `&[u8]`, or `Vec<u8>`
let sample_input = "Hello, byte_reader!";
// Create mutable `r`
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_snake().unwrap(); // byte_reader
r.consume("!").unwrap();
println!("Greeted to `{name}`.");
}
sourcepub fn read_while(&mut self, condition: impl Fn(&u8) -> bool) -> &[u8] ⓘ
pub fn read_while(&mut self, condition: impl Fn(&u8) -> bool) -> &[u8] ⓘ
Read next byte while the condition holds on it
sourcepub fn next(&mut self) -> Option<u8>
pub fn next(&mut self) -> Option<u8>
Read next one byte, or return None if the remained bytes is empty
sourcepub fn next_if(&mut self, condition: impl Fn(&u8) -> bool) -> Option<u8>
pub fn next_if(&mut self, condition: impl Fn(&u8) -> bool) -> Option<u8>
Read next one byte if the condition holds on it
sourcepub fn peek3(&self) -> Option<&u8>
pub fn peek3(&self) -> Option<&u8>
Peek next byte of next byte of next byte (without consuming)
sourcepub fn consume(&mut self, token: impl Bytes) -> Option<()>
pub fn consume(&mut self, token: impl Bytes) -> Option<()>
Read token
if the remained bytes starts with it, otherwise return Err
token : &str | String | &[u8] | Vec<u8>
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
// Get a input from a File, standard input, or others
// Input can be `&str`, `String`, `&[u8]`, or `Vec<u8>`
let sample_input = "Hello, byte_reader!";
// Create mutable `r`
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_snake().unwrap(); // byte_reader
r.consume("!").unwrap();
println!("Greeted to `{name}`.");
}
sourcepub fn consume_oneof<const N: usize>(
&mut self,
tokens: [impl Bytes; N]
) -> Option<usize>
pub fn consume_oneof<const N: usize>( &mut self, tokens: [impl Bytes; N] ) -> Option<usize>
Read first token in tokens
that the remained bytes starts with, and returns the index of the (matched) token, or Err
if none matched
token : &str | String | &[u8] | Vec<u8>
sourcepub fn read_camel(&mut self) -> Option<String>
pub fn read_camel(&mut self) -> Option<String>
Read a camelCase
word like helloWorld
, userID
, … as String
sourcepub fn read_snake(&mut self) -> Option<String>
pub fn read_snake(&mut self) -> Option<String>
Read a snake_case
word like hello_world
, user_id
, … as String
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
// Get a input from a File, standard input, or others
// Input can be `&str`, `String`, `&[u8]`, or `Vec<u8>`
let sample_input = "Hello, byte_reader!";
// Create mutable `r`
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_snake().unwrap(); // byte_reader
r.consume("!").unwrap();
println!("Greeted to `{name}`.");
}
sourcepub fn read_kebab(&mut self) -> Option<String>
pub fn read_kebab(&mut self) -> Option<String>
Read a kebeb-case
word like hello-world
, Content-Type
, … as String
sourcepub fn read_string(&mut self) -> Option<String>
pub fn read_string(&mut self) -> Option<String>
Read a double-quoted UTF-8 string literal like "Hello, world!"
, "application/json"
, … and return the quoted content as String
-
Returns
None
if- Expected
"
s were not found - The quoted content is not UTF-8
- Expected
-
This doesn’t handle escape sequences
sourcepub unsafe fn read_string_unchecked(&mut self) -> Option<String>
pub unsafe fn read_string_unchecked(&mut self) -> Option<String>
Read a double-quoted string literal like "Hello, world!"
, "application/json"
, … the and return the quoted content as String
without checking if the content bytes is valid UTF-8
This doesn’t handle escape sequences