[−][src]Struct rut::Decoder
A bytewise UTF-8 decoder.
Methods
impl Decoder
[src]
pub fn new() -> Decoder
[src]
Creates a new decoder.
pub fn decode_byte(&mut self, byte: u8) -> DecoderResult
[src]
Decodes a single byte.
Correct Use
Due to the nature of bytewise UTF-8 decoding, blindly continuing through errors will lead to vastly incorrect results.
For example, the byte sequence C2, 41, 42
would return the results <Continue>, <Error>, B
.
However, Unicode expects this sequence to be treated as <Error>, A, B
.
This means that, when an error is encountered inside of a sequence,
the offending byte should be passsed to decode_byte
again.
This is because an ill-formed sequence ends at the last valid byte, not at the first invalid byte.
This is essentially what the decode_one
function does.
Example
Performing lossy decoding with Decoder
:
use std::fmt::Write; use rut::{Decoder, DecoderResult, Error::*}; let mut s = String::new(); let mut d = Decoder::new(); // An ill-formed UTF-8 sequence as described above. let bytes = [0xC2, 0x41, 0x42]; for &byte in &bytes { let mut result = d.decode_byte(byte); if let DecoderResult::Error(e) = result { match e { BrokenSequence | OverlongEncoding | InvalidCodePoint => { write!(&mut s, "\u{FFFD}").unwrap(); result = d.decode_byte(byte); }, _ => {} } } match result { DecoderResult::Continue => continue, DecoderResult::Char(c) => write!(&mut s, "{}", c).unwrap(), DecoderResult::Error(_) => write!(&mut s, "\u{FFFD}").unwrap() } } assert_eq!(&s, "�AB");
Trait Implementations
impl Clone for Decoder
[src]
fn clone(&self) -> Decoder
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl Copy for Decoder
[src]
impl Debug for Decoder
[src]
impl Default for Decoder
[src]
Auto Trait Implementations
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,