Skip to main content

Utf8Decoder

Struct Utf8Decoder 

Source
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

Source

pub fn new() -> Self

A decoder with nothing held over.

Source

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.

Source

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.

Source

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.

Source

pub fn is_pending(&self) -> bool

True when a partial character is held for the next decode.

Trait Implementations§

Source§

impl Clone for Utf8Decoder

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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() -> Self

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

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.