xhtmlchardet

Crates.ioxhtmlchardet
lib.rsxhtmlchardet
version2.2.0
sourcesrc
created_at2015-08-31 08:29:15.902594
updated_at2022-01-26 00:08:33.967717
descriptionCharacter set detection for XML and HTML
homepagehttps://github.com/wezm/xhtmlchardet
repositoryhttps://github.com/wezm/xhtmlchardet
max_upload_size
id2970
size15,659
Wesley Moore (wezm)

documentation

https://docs.rs/xhtmlchardet/

README

xhtmlchardet

Basic character set detection for XML and HTML in Rust.

Build Status Documentation Latest Version

Minimum Supported Rust Version: 1.24.0

Example

use std::io::Cursor;
extern crate xhtmlchardet;

let text = b"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><channel><title>Example</title></channel>";
let mut text_cursor = Cursor::new(text.to_vec());
let detected_charsets: Vec<String> = xhtmlchardet::detect(&mut text_cursor, None).unwrap();
assert_eq!(detected_charsets, vec!["iso-8859-1".to_string()]);

Rationale

I wrote a feed crawler that needed to determine the character set of fetched content so that it could be normalised to UTF-8. Initially I used the uchardet crate but I encountered some situations where it misdetected the charset. I collected all these edge cases together and built a test suite. Then I implemented this crate, which passes all of those tests. It uses a fairly naïve approach derived from section F of the XML specification.

Commit count: 42

cargo fmt