pub struct Utf8Decoder { /* private fields */ }Expand description
Decodes Event::Text bytes into str pieces, across chunk boundaries.
Event::Text is raw bytes, because the parser never copies them. A
consumer that wants characters needs two things this handles: a multi-byte
character split across two feed_with calls, and invalid bytes, which
become U+FFFD exactly as Parser::feed renders them.
Pieces are borrowed: an ASCII run is handed over as one slice of the input with nothing copied, and only a character straddling a boundary is assembled in the decoder’s own four bytes. It allocates nothing, ever.
let mut parser = Parser::new();
let mut decoder = Utf8Decoder::new();
let mut seen = String::new();
// The 'é' is split across the two chunks.
for chunk in [&b"caf\xC3"[..], &b"\xA9!"[..]] {
parser.feed_with(chunk, |event| match event {
Event::Text(bytes) => decoder.decode(bytes, |s| seen.push_str(s)),
_ => decoder.flush_incomplete(|s| seen.push_str(s)),
});
}
assert_eq!(seen, "café!");Implementations§
Source§impl Utf8Decoder
impl Utf8Decoder
Sourcepub fn is_idle(&self) -> bool
pub fn is_idle(&self) -> bool
True when no partial character is held over from a previous chunk.
Then a run of ASCII bytes decodes to itself, and a caller may use it
directly without going through Utf8Decoder::decode.
Sourcepub fn decode<F: FnMut(&str)>(&mut self, bytes: &[u8], f: F)
pub fn decode<F: FnMut(&str)>(&mut self, bytes: &[u8], f: F)
Decode bytes, calling f with each piece of text in order.
A character left incomplete at the end is held for the next call.
Sourcepub fn flush_incomplete<F: FnMut(&str)>(&mut self, f: F)
pub fn flush_incomplete<F: FnMut(&str)>(&mut self, f: F)
Give up on a character left incomplete, emitting U+FFFD for it.
Call this when something other than text arrives — a control byte or an escape sequence ends a text run, and the held bytes can never be completed. Does nothing when no character is pending.
Sourcepub fn is_pending(&self) -> bool
pub fn is_pending(&self) -> bool
True when a partial character is held for the next decode.