pub enum TableType {
    Complete(&'static [char; 128]),
    Incomplete(&'static [Option<char>; 128]),
}
Expand description

Wrapper enumerate for decoding tables

It has 2 types: Complete, complete tables (it doesn’t have undefined codepoints) / Incomplete, incomplete tables (does have ones)

Variants§

§

Complete(&'static [char; 128])

complete table, which doen’t have any undefined codepoints

§

Incomplete(&'static [Option<char>; 128])

incomplete table, which has some undefined codepoints

Implementations§

source§

impl TableType

source

pub fn decode_string_checked<'a, T: Into<Cow<'a, [u8]>>>( &self, src: T ) -> Option<String>

Wrapper function for decoding bytes encoded in SBCSs

This function returns None if any bytes bumps into undefined codepoints

Arguments
  • src - bytes encoded in SBCS
Examples
use oem_cp::code_table::{DECODING_TABLE_CP437, DECODING_TABLE_CP874};
use oem_cp::code_table_type::TableType;
use TableType::{Complete,Incomplete};

assert_eq!(Complete(&DECODING_TABLE_CP437).decode_string_checked(vec![0xFB, 0xAC, 0x3D, 0xAB]), Some("√¼=½".to_string()));
// means shrimp in Thai (U+E49 => 0xE9)
assert_eq!(Incomplete(&DECODING_TABLE_CP874).decode_string_checked(vec![0xA1, 0xD8, 0xE9, 0xA7]), Some("กุ้ง".to_string()));
// 0xDB-0xDE,0xFC-0xFF is invalid in CP874 in Windows (strict mode)
assert_eq!(Incomplete(&DECODING_TABLE_CP874).decode_string_checked(vec![0x30, 0xDB]), None);
source

pub fn decode_string_lossy<'a, T: Into<Cow<'a, [u8]>>>(&self, src: T) -> String

Wrapper function for decoding bytes encoded in SBCSs

Undefined codepoints are replaced with U+FFFD.

Arguments
  • src - bytes encoded in SBCS
Examples
use oem_cp::code_table::{DECODING_TABLE_CP437, DECODING_TABLE_CP874};
use oem_cp::code_table_type::TableType;
use TableType::{Complete,Incomplete};

assert_eq!(Complete(&DECODING_TABLE_CP437).decode_string_lossy(vec![0xFB, 0xAC, 0x3D, 0xAB]), "√¼=½".to_string());
// means shrimp in Thai (U+E49 => 0xE9)
assert_eq!(Incomplete(&DECODING_TABLE_CP874).decode_string_lossy(vec![0xA1, 0xD8, 0xE9, 0xA7]), "กุ้ง".to_string());
// 0xDB-0xDE,0xFC-0xFF is invalid in CP874 in Windows (strict mode)
assert_eq!(Incomplete(&DECODING_TABLE_CP874).decode_string_lossy(vec![0x30, 0xDB]), "0\u{FFFD}".to_string());

Trait Implementations§

source§

impl Clone for TableType

source§

fn clone(&self) -> TableType

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for TableType

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.