bytes2chars
lazily decodes utf-8 chars from bytes
provides lazy, fallible analogs to str::Chars (Utf8Chars) and str::CharIndices (Utf8CharIndices), as well as a lower-level push-based Utf8Decoder
design goals
- rich errors—what went wrong and where
- lazy
no-std- performance
quick start
prefer iterators like Utf8CharIndices or Utf8Chars if you have access to a byte iterator. Utf8Chars still tracks bytes for error context, so it's purely a convenience wrapper
if you receive bytes in chunks, use the push-based Utf8Decoder
examples
iterator api
let input = b"\xF0\x9F\xA6\x80 rust".iter.copied;
// decode into an iterator of chars and their positions
let indexed = from.?;
let expected = vec!;
assert_eq!;
// convenience wrapper to decode into an iterator of chars
let chars = from.?;
assert_eq!;
error handling
let err = from
.
.unwrap_err;
assert_eq!;
assert_eq!;
push based decoder
let mut decoder = new;
assert_eq!; // accumulating
assert_eq!;
assert_eq!;
assert_eq!; // complete
decoder.finish?; // check for truncated sequence
alternatives
std::str::from_utf8
eager and error context provides a range but not a particular cause
utf8-decode
also lazy. error provides a range but not a particular cause. does not provide a push based decoder