pub struct LossyDecoder<F: FnMut(&str)> { /* private fields */ }Expand description
A push-based, lossy decoder for UTF-8. Errors are replaced with the U+FFFD replacement character.
Users “push” bytes into the decoder, which in turn “pushes” &str slices into a callback.
Note: Dropping the decoder signals the end of the input:
If the last input chunk ended with an incomplete byte sequence for a code point,
this is an error and a replacement character is emitted.
Use std::mem::forget to inhibit this behavior.
§Examples
Single-shot lossy decoding (like String::from_utf8_lossy but returning String):
use utf8_zero::LossyDecoder;
fn string_from_utf8_lossy(input: &[u8]) -> String {
let mut string = String::new();
LossyDecoder::new(|s| string.push_str(s)).feed(input);
string
}
assert_eq!(string_from_utf8_lossy(b"Hello\xC0World"), "Hello\u{FFFD}World");Streaming chunks – a multi-byte code point split across two feeds:
use utf8_zero::LossyDecoder;
let mut output = String::new();
{
let mut decoder = LossyDecoder::new(|s| output.push_str(s));
decoder.feed(b"Hello \xC3"); // first byte of U+00E9
decoder.feed(b"\xA9!"); // second byte + excl mark
}
assert_eq!(output, "Hello \u{00E9}!");Implementations§
Source§impl<F: FnMut(&str)> LossyDecoder<F>
impl<F: FnMut(&str)> LossyDecoder<F>
Sourcepub fn feed(&mut self, input: &[u8])
pub fn feed(&mut self, input: &[u8])
Feed one chunk of input into the decoder.
The input is decoded lossily
and the callback called once or more with &str string slices.
If the UTF-8 byte sequence for one code point was split into this bytes chunk and previous bytes chunks, it will be correctly pieced back together.