• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H03-May-2022-12,94112,408

tests/H03-May-2022-76,21268,143

.cargo-checksum.jsonH A D03-May-202289 11

.gitignoreH A D03-Sep-2017307 118

.travis.ymlH A D03-Sep-2017158 1211

Cargo.tomlH A D15-Sep-2017245 129

LICENSEH A D03-Sep-20177.5 KiB166128

README.mdH A D03-Sep-20171.1 KiB5843

README.md

1Rust-Chardet
2=========================
3[![Rust-Charset on Travis CI][travis-image]][travis]
4
5[travis-image]: https://travis-ci.org/thuleqaid/rust-chardet.png
6[travis]: https://travis-ci.org/thuleqaid/rust-chardet
7
8Rust version of chardet.
9
10## Usage
11
12Put this in your `Cargo.toml`:
13
14```toml
15[dependencies]
16chardet = "0.2"
17```
18
19Then put this in your crate root:
20
21```rust
22extern crate chardet;
23```
24
25Using with encoding:
26
27```rust
28extern crate chardet;
29extern crate encoding;
30use chardet;
31use std::fs::OpenOptions;
32use std::io::prelude::*;
33use encoding::DecoderTrap;
34use encoding::label::encoding_from_whatwg_label;
35
36// open text file
37let mut fh = OpenOptions::new().read(true).open(filepath).expect(
38    "Could not open file",
39);
40let mut reader: Vec<u8> = Vec::new();
41
42// read file
43fh.read_to_end(&mut reader).expect("Could not read file");
44
45// detect charset of the file
46let result = detect(&reader);
47// result.0 Encode
48// result.1 Confidence
49// result.2 Language
50
51// decode file into utf-8
52let coder = encoding_from_whatwg_label(charset2encoding(&result.0));
53if coder.is_some() {
54    let utf8reader = coder.unwrap().decode(&reader, DecoderTrap::Ignore).expect("Error");
55}
56```
57
58