pub struct Utf8Decoder { /* private fields */ }Expand description
push based UTF-8 decoder that tracks byte positions
§Examples
decode a valid character
let mut decoder = Utf8Decoder::new(0);
assert_eq!(decoder.push(0xF0), None); // accumulating
assert_eq!(decoder.push(0x9F), None);
assert_eq!(decoder.push(0xA6), None);
assert_eq!(decoder.push(0x80), Some(Ok((0, '🦀')))); // complete
decoder.finish()?; // check for truncated sequence
keeps going after an error. offending bytes are thrown in the garbage can
let mut decoder = Utf8Decoder::new(0);
assert_eq!(decoder.push(b'a'), Some(Ok((0, 'a'))));
assert_eq!(decoder.push(0xC3), None);
assert_eq!(
decoder.push(0xC3),
Some(Err(Error {
range: 1..3,
kind: ErrorKind::ExpectedContinuation(0xC3),
}))
);
assert_eq!(decoder.push(b'b'), Some(Ok((3, 'b'))));Implementations§
Source§impl Utf8Decoder
impl Utf8Decoder
Sourcepub fn push(&mut self, b: u8) -> Option<Result<(usize, char)>>
pub fn push(&mut self, b: u8) -> Option<Result<(usize, char)>>
process a single byte
§Examples
let mut decoder = Utf8Decoder::default();
assert_eq!(decoder.push(0xC3), None); // accumulating
assert_eq!(decoder.push(0xA9), Some(Ok((0, 'é')))); // complete
// error
let expected = Some(Err(Error {
range: 2..3,
kind: ErrorKind::InvalidLead(0x80),
}));
assert_eq!(decoder.push(0x80), expected);
// after error, decoder is reset to idle and continues
assert_eq!(decoder.push(b'b'), Some(Ok((3, 'b'))));
assert_eq!(decoder.finish(), Ok(4)); // no truncated sequenceSourcepub fn finish(self) -> Result<usize>
pub fn finish(self) -> Result<usize>
flush the decoder when there are no more bytes left
on success, returns the total number of bytes consumed
§Errors
Returns an error of kind ErrorKind::UnfinishedSequence when current byte sequence is truncated
§Examples
// idle decoder is all good
assert_eq!(Utf8Decoder::new(0).finish(), Ok(0));
// incomplete sequence returns `UnfinishedSequence`
let mut decoder = Utf8Decoder::new(0);
assert_eq!(decoder.push(0xC3), None);
assert_eq!(
decoder.finish().unwrap_err().kind,
ErrorKind::UnfinishedSequence
);Trait Implementations§
Source§impl Clone for Utf8Decoder
impl Clone for Utf8Decoder
Source§fn clone(&self) -> Utf8Decoder
fn clone(&self) -> Utf8Decoder
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for Utf8Decoder
impl Debug for Utf8Decoder
Source§impl Default for Utf8Decoder
impl Default for Utf8Decoder
Source§fn default() -> Utf8Decoder
fn default() -> Utf8Decoder
Returns the “default value” for a type. Read more
Source§impl PartialEq for Utf8Decoder
impl PartialEq for Utf8Decoder
impl Eq for Utf8Decoder
impl StructuralPartialEq for Utf8Decoder
Auto Trait Implementations§
impl Freeze for Utf8Decoder
impl RefUnwindSafe for Utf8Decoder
impl Send for Utf8Decoder
impl Sync for Utf8Decoder
impl Unpin for Utf8Decoder
impl UnsafeUnpin for Utf8Decoder
impl UnwindSafe for Utf8Decoder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more