pub struct CharReader<R: Read> { /* private fields */ }Expand description
A buffered reader able to read chars or lines without crashing when the stream doesn’t finish and/or doesn’t contain newlines.
It’s also able to avoid storying whole lines if you’re only interested in their beginning.
Bad UTF8 is reported as io::Error with kind InvalidData.
Implementations§
Source§impl<R: Read> CharReader<R>
impl<R: Read> CharReader<R>
pub fn new(src: R) -> Self
Sourcepub fn load_char(&mut self) -> Result<Option<(char, usize)>>
pub fn load_char(&mut self) -> Result<Option<(char, usize)>>
ensure there’s at least one char in the buffer, and returns it with its size in bytes (or None if the underlying stream is finished).
You probably don’t need this function but next_char.
Sourcepub fn next_char(&mut self) -> Result<Option<char>>
pub fn next_char(&mut self) -> Result<Option<char>>
read and return the next char, or NONE in case of EOF
Sourcepub fn peek_char(&mut self) -> Result<Option<char>>
pub fn peek_char(&mut self) -> Result<Option<char>>
return the next char, but doesn’t advance the cursor
Sourcepub fn read_line(
&mut self,
line: &mut String,
drop_after: usize,
fail_after: usize,
) -> Result<bool>
pub fn read_line( &mut self, line: &mut String, drop_after: usize, fail_after: usize, ) -> Result<bool>
append the next line, if any, but with some protection against wild stream content:
- don’t store chars after the drop_after threshold
- throw an error after the fail_after threshold
Thresholds are in chars, not bytes nor cols nor graphemes. Only difference with next_line is that you pass (and may reuse) the string to fill.
Return Ok(false) when there was no error but nothing to read (stream finished or paused).
This function may return Ok(true) and not have written anything: it means there was an empty line (i.e. next char will be a CR or LF)
Sourcepub fn next_line(
&mut self,
drop_after: usize,
fail_after: usize,
) -> Result<Option<String>>
pub fn next_line( &mut self, drop_after: usize, fail_after: usize, ) -> Result<Option<String>>
return the next line, if any, but with some protection against wild stream content:
- don’t store chars after the drop_after threshold
- throw an error after the fail_after threshold
Thresholds are in chars, not bytes nor cols nor graphemes.