pub struct I18nReaderEncodingDetector { /* private fields */ }Expand description
Encoding detector for I18nReader.
Implementations§
Source§impl I18nReaderEncodingDetector
impl I18nReaderEncodingDetector
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new I18nReaderEncodingDetector.
Options can be modified using method chaining.
§Options
- buffer size
Default is 8K bytes. - bytes_to_guess
How many bytes are used to guess_utf8.
Default is 1K bytes. - non_ascii_to_guess
The number of non-ASCII characters to be used to guess_utf8 the encoding.
Non-ASCII here includes non-textual characters.
Default is 100 characters. - non_text_threshold
The threshold to determine the guess_utfguess_utf8failed.
The value should be specified in percentage.
Default is 0%. - add_bom_utf16
If it’s true and a BOM is not found at the head of the input, a BOM is added.
§Example
let detector = transcoding_rs::I18nReaderEncodingDetector::new()
.buffer_size(1024) // options can be changed by method chaining.
.bytes_to_guess(512)
.non_ascii_to_guess(10)
.non_text_threshold(5)
.add_bom_utf16(true);Sourcepub fn buffer_size(self, size: usize) -> Self
pub fn buffer_size(self, size: usize) -> Self
Sets buffer size.
Sourcepub fn bytes_to_guess(self, size: usize) -> Self
pub fn bytes_to_guess(self, size: usize) -> Self
Sets bytes_to_guess.
Sourcepub fn non_text_threshold(self, percent: u8) -> Self
pub fn non_text_threshold(self, percent: u8) -> Self
Sets non_text_threshold.
Sourcepub fn non_ascii_to_guess(self, num: usize) -> Self
pub fn non_ascii_to_guess(self, num: usize) -> Self
Sets non_ascii_to_guess.
Sourcepub fn add_bom_utf16(self, add_bom: bool) -> Self
pub fn add_bom_utf16(self, add_bom: bool) -> Self
Sets add_bom_utf16.
Sourcepub fn guess_utf8<R>(self, reader: R) -> Result<GuessResult<R>>where
R: Read,
pub fn guess_utf8<R>(self, reader: R) -> Result<GuessResult<R>>where
R: Read,
Guesses the source encoding and return GuessResult,
setting the destination encoding to UTF-8.
Once this method is called, the instance is no longer available, since this method moves the ownership to the return value.
Sourcepub fn guess<R>(
self,
reader: R,
dst_encoding: &'static Encoding,
) -> Result<GuessResult<R>>where
R: Read,
pub fn guess<R>(
self,
reader: R,
dst_encoding: &'static Encoding,
) -> Result<GuessResult<R>>where
R: Read,
Guesses the source encoding and return GuessResult,
setting the destination encoding to the specified one.
Once this method is called, the instance is no longer available, since this method moves the ownership to the return value.
§Example
use std::io::Read;
use encoding_rs;
let dst_encoding = encoding_rs::EUC_JP;
let src = b"\x83\x6E\x83\x8D\x81\x5B"; // ハロー in SHIFT_JIS
let detector = transcoding_rs::I18nReaderEncodingDetector::new();
let guess_result = detector.guess(
src.as_ref(), // `slice` can be used, since it implments the `std::io::Read` trait.
dst_encoding).unwrap();
match guess_result {
transcoding_rs::GuessResult::Success(mut reader,enc) => {
assert_eq!(encoding_rs::SHIFT_JIS, enc);
let buf = &mut vec![0u8;128];
let n = reader.read(buf).unwrap();
let expected = b"\xA5\xCF\xA5\xED\xA1\xBC"; // ハロー in EUC_JP
assert_eq!(expected, &buf[..n]);
},
_ => panic!()
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for I18nReaderEncodingDetector
impl RefUnwindSafe for I18nReaderEncodingDetector
impl Send for I18nReaderEncodingDetector
impl Sync for I18nReaderEncodingDetector
impl Unpin for I18nReaderEncodingDetector
impl UnwindSafe for I18nReaderEncodingDetector
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more