Skip to main content

Utf8Decoder

Struct Utf8Decoder 

Source
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

Source

pub fn new(offset: usize) -> Self

create a decoder starting at byte offset offset

Source

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 sequence
Source

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

Source§

fn clone(&self) -> Utf8Decoder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Utf8Decoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Utf8Decoder

Source§

fn default() -> Utf8Decoder

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Utf8Decoder

Source§

fn eq(&self, other: &Utf8Decoder) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Utf8Decoder

Source§

impl StructuralPartialEq for Utf8Decoder

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.