3 releases (stable)

1.0.1 Jul 21, 2019
1.0.0 Jul 19, 2019
0.1.0 Jul 19, 2019

#1920 in Parser implementations

Download history 10649/week @ 2024-06-19 8662/week @ 2024-06-26 10329/week @ 2024-07-03 10160/week @ 2024-07-10 10955/week @ 2024-07-17 10139/week @ 2024-07-24 11472/week @ 2024-07-31 13234/week @ 2024-08-07 11354/week @ 2024-08-14 12904/week @ 2024-08-21 11877/week @ 2024-08-28 13077/week @ 2024-09-04 13763/week @ 2024-09-11 12096/week @ 2024-09-18 12732/week @ 2024-09-25 10021/week @ 2024-10-02

50,994 downloads per month
Used in 134 crates (8 directly)

MIT/Apache

14KB
126 lines

UTF-8 decode

This crates provides incremental UTF-8 decoders implementing the Iterator trait. Thoses iterators are wrappers around u8 bytes iterators.

Decoder

The Decoder struct wraps Iterator<Item = u8> iterators. You can use it, for instance, to decode u8 slices.

extern crate utf8_decode;

use utf8_decode::Decoder;

fn main() -> std::io::Result<()> {
    let bytes = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 32, 240, 159, 140, 141];

    let decoder = Decoder::new(bytes.iter().cloned());

    let mut string = String::new();
    for c in decoder {
        string.push(c?);
    }

    println!("{}", string);

    Ok(())
}

UnsafeDecoder

The UnsafeDecoder wraps Iterator<Item = std::io::Result<u8>> iterators. You can use it, for instance, to decode UTF-8 encoded files.

extern crate utf8_decode;

use std::fs::File;
use std::io::Read;
use utf8_decode::UnsafeDecoder;

fn main() -> std::io::Result<()> {
    let file = File::open("examples/file.txt")?;

    let decoder = UnsafeDecoder::new(file.bytes());

    let mut string = String::new();
    for c in decoder {
        string.push(c?);
    }

    println!("{}", string);

    Ok(())
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps