1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
# UTF-8 decode
[](https://github.com/timothee-haudebourg/utf8-decode/actions)
[](https://crates.io/crates/utf8-decode)
[](https://github.com/timothee-haudebourg/utf8-decode#license)
[](https://docs.rs/utf8-decode)
<!-- cargo-rdme start -->
This crates provides incremental UTF-8 decoders implementing the
[`Iterator`] trait, wrapping around [`u8`] bytes iterators.
It also provide the `const`-compatible [`try_decode_char`] to decode UTF-8
byte streams, even in `const` contexts.
[`u8`]: std::primitive::u8
[`Iterator`]: std::iter::Iterator
[`try_decode_char`]: https://docs.rs/utf8-decode/latest/utf8_decode/fn.try_decode_char.html
### `Decoder`
The [`Decoder`] iterator can be used, for instance, to decode `u8` slices.
```rust
use utf8_decode::Decoder;
let bytes = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33];
let decoder = Decoder::new(bytes.iter().cloned());
let mut string = String::new();
for c in decoder {
string.push(c?);
}
println!("{}", string);
```
### `TryDecoder`
The [`TryDecoder`] iterator can be used, for instance, to decode UTF-8
encoded files.
```rust
use utf8_decode::TryDecoder;
let file = File::open("examples/file.txt")?;
let decoder = TryDecoder::new(file.bytes());
let mut string = String::new();
for c in decoder {
string.push(c?);
}
```
[`TryDecoder`]: https://docs.rs/utf8-decode/latest/utf8_decode/fallible/struct.TryDecoder.html
<!-- cargo-rdme end -->
## License
Licensed under either of
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
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.
|