I18nReaderEncodingDetector

Struct I18nReaderEncodingDetector 

Source
pub struct I18nReaderEncodingDetector { /* private fields */ }
Expand description

Encoding detector for I18nReader.

Implementations§

Source§

impl I18nReaderEncodingDetector

Source

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);
Source

pub fn buffer_size(self, size: usize) -> Self

Sets buffer size.

Source

pub fn bytes_to_guess(self, size: usize) -> Self

Sets bytes_to_guess.

Source

pub fn non_text_threshold(self, percent: u8) -> Self

Sets non_text_threshold.

Source

pub fn non_ascii_to_guess(self, num: usize) -> Self

Sets non_ascii_to_guess.

Source

pub fn add_bom_utf16(self, add_bom: bool) -> Self

Sets add_bom_utf16.

Source

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.

Source

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§

Source§

impl Debug for I18nReaderEncodingDetector

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.