encoding8 0.3.2

various 8-bit encodings
Documentation
mod string;
#[cfg(test)]
mod tests;
pub use self::string::ASCII;

const DEL: u8 = 127;
const CASE_OFFSET: u8 = b'a' - b'A';

/// `to_ebdic` makes a copy of a byte encoded as ASCII as it's EBDIC equivalent.
/// use `make_ebcdic` if you want to convert in-place
#[inline]
pub fn to_ebcdic(b: u8) -> u8 {
    match b {
        0x0 => 0x0,
        0x1 => 0x1,
        0x2 => 0x2,
        0x3 => 0x3,
        0x4 => 0x37,
        0x5 => 0x2d,
        0x6 => 0x2e,
        0x7 => 0x2f,
        0x8 => 0x16,
        0x9 => 0x5,
        0xa => 0x15,
        0xb => 0xb,
        0xc => 0xc,
        0xd => 0xd,
        0xe => 0xe,
        0xf => 0xf,
        0x10 => 0x10,
        0x11 => 0x11,
        0x12 => 0x12,
        0x13 => 0x13,
        0x14 => 0x3c,
        0x15 => 0x3d,
        0x16 => 0x32,
        0x17 => 0x26,
        0x18 => 0x18,
        0x19 => 0x19,
        0x1a => 0x3f,
        0x1b => 0x27,
        0x1c => 0x1c,
        0x1d => 0x1d,
        0x1e => 0x1e,
        0x1f => 0x1f,
        0x20 => 0x40,
        0x21 => 0x5a,
        0x22 => 0x7f,
        0x23 => 0x7b,
        0x24 => 0x5b,
        0x25 => 0x6c,
        0x26 => 0x50,
        0x27 => 0x7d,
        0x28 => 0x4d,
        0x29 => 0x5d,
        0x2a => 0x5c,
        0x2b => 0x4e,
        0x2c => 0x6b,
        0x2d => 0x60,
        0x2e => 0x4b,
        0x2f => 0x61,
        0x30 => 0xf0,
        0x31 => 0xf1,
        0x32 => 0xf2,
        0x33 => 0xf3,
        0x34 => 0xf4,
        0x35 => 0xf5,
        0x36 => 0xf6,
        0x37 => 0xf7,
        0x38 => 0xf8,
        0x39 => 0xf9,
        0x3a => 0x7a,
        0x3b => 0x5e,
        0x3c => 0x4c,
        0x3d => 0x7e,
        0x3e => 0x6e,
        0x3f => 0x6f,
        0x40 => 0x7c,
        0x41 => 0xc1,
        0x42 => 0xc2,
        0x43 => 0xc3,
        0x44 => 0xc4,
        0x45 => 0xc5,
        0x46 => 0xc6,
        0x47 => 0xc7,
        0x48 => 0xc8,
        0x49 => 0xc9,
        0x4a => 0xd1,
        0x4b => 0xd2,
        0x4c => 0xd3,
        0x4d => 0xd4,
        0x4e => 0xd5,
        0x4f => 0xd6,
        0x50 => 0xd7,
        0x51 => 0xd8,
        0x52 => 0xd9,
        0x53 => 0xe2,
        0x54 => 0xe3,
        0x55 => 0xe4,
        0x56 => 0xe5,
        0x57 => 0xe6,
        0x58 => 0xe7,
        0x59 => 0xe8,
        0x5a => 0xe9,
        0x5b => 0xad,
        0x5c => 0xe0, 
        0x5d => 0xbd,
        0x5e => 0x5f,
        0x5f => 0x6d,
        0x60 => 0x79,
        0x61 => 0x81,
        0x62 => 0x82,
        0x63 => 0x83,
        0x64 => 0x84,
        0x65 => 0x85,
        0x66 => 0x86,
        0x67 => 0x87,
        0x68 => 0x88,
        0x69 => 0x89,
        0x6a => 0x91,
        0x6b => 0x92,
        0x6c => 0x93,
        0x6d => 0x94,
        0x6e => 0x95,
        0x6f => 0x96,
        0x70 => 0x97,
        0x71 => 0x98,
        0x72 => 0x99,
        0x73 => 0xa2,
        0x74 => 0xa3,
        0x75 => 0xa4,
        0x76 => 0xa5,
        0x77 => 0xa6,
        0x78 => 0xa7,
        0x79 => 0xa8,
        0x7a => 0xa9,
        0x7b => 0xc0,
        0x7c => 0x4f,
        0x7d => 0xd0,
        0x7e => 0xa1,
        0x7f => 0x7,
        0x80 => 0x20,
        0x81 => 0x21,
        0x82 => 0x22,
        0x83 => 0x23,
        0x84 => 0x24,
        0x85 => 0x4,
        0x86 => 0x6,
        0x87 => 0x8,
        0x88 => 0x28,
        0x89 => 0x29,
        0x8a => 0x2a,
        0x8b => 0x2b,
        0x8c => 0x2c,
        0x8d => 0x9,
        0x8e => 0xa,
        0x8f => 0x14,
        0x90 => 0x30,
        0x91 => 0x31,
        0x92 => 0x25,
        0x93 => 0x33,
        0x94 => 0x34,
        0x95 => 0x35,
        0x96 => 0x36,
        0x97 => 0x17,
        0x98 => 0x38,
        0x99 => 0x39,
        0x9a => 0x3a,
        0x9b => 0x3b,
        0x9c => 0x1a,
        0x9d => 0x1b,
        0x9e => 0x3e,
        0x9f => 0xff,
        0xa0 => 0x41,
        0xa1 => 0xaa,
        0xa2 => 0x4a,
        0xa3 => 0xb1,
        0xa4 => 0x9f,
        0xa5 => 0xb2,
        0xa6 => 0x6a,
        0xa7 => 0xb5,
        0xa8 => 0xbb,
        0xa9 => 0xb4,
        0xaa => 0x9a,
        0xab => 0x8a,
        0xac => 0xb0,
        0xad => 0xca,
        0xae => 0xaf,
        0xaf => 0xbc,
        0xb0 => 0x90,
        0xb1 => 0x8f,
        0xb2 => 0xea,
        0xb3 => 0xfa,
        0xb4 => 0xbe,
        0xb5 => 0xa0,
        0xb6 => 0xb6,
        0xb7 => 0xb3,
        0xb8 => 0x9d,
        0xb9 => 0xda,
        0xba => 0x9b,
        0xbb => 0x8b,
        0xbc => 0xb7,
        0xbd => 0xb8,
        0xbe => 0xb9,
        0xbf => 0xab,
        0xc0 => 0x64,
        0xc1 => 0x65,
        0xc2 => 0x62,
        0xc3 => 0x66,
        0xc4 => 0x63,
        0xc5 => 0x67,
        0xc6 => 0x9e,
        0xc7 => 0x68,
        0xc8 => 0x74,
        0xc9 => 0x71,
        0xca => 0x72,
        0xcb => 0x73,
        0xcc => 0x78,
        0xcd => 0x75,
        0xce => 0x76,
        0xcf => 0x77,
        0xd0 => 0xac,
        0xd1 => 0x69,
        0xd2 => 0xed,
        0xd3 => 0xee,
        0xd4 => 0xeb,
        0xd5 => 0xef,
        0xd6 => 0xec,
        0xd7 => 0xbf,
        0xd8 => 0x80,
        0xd9 => 0xfd,
        0xda => 0xfe,
        0xdb => 0xfb,
        0xdc => 0xfc,
        0xdd => 0xba,
        0xde => 0xae,
        0xdf => 0x59,
        0xe0 => 0x44,
        0xe1 => 0x45,
        0xe2 => 0x42,
        0xe3 => 0x46,
        0xe4 => 0x43,
        0xe5 => 0x47,
        0xe6 => 0x9c,
        0xe7 => 0x48,
        0xe8 => 0x54,
        0xe9 => 0x51,
        0xea => 0x52,
        0xeb => 0x53,
        0xec => 0x58,
        0xed => 0x55,
        0xee => 0x56,
        0xef => 0x57,
        0xf0 => 0x8c,
        0xf1 => 0x49,
        0xf2 => 0xcd,
        0xf3 => 0xce,
        0xf4 => 0xcb,
        0xf5 => 0xcf,
        0xf6 => 0xcc,
        0xf7 => 0xe1,
        0xf8 => 0x70,
        0xf9 => 0xdd,
        0xfa => 0xde,
        0xfb => 0xdb,
        0xfc => 0xdc,
        0xfd => 0x8d,
        0xfe => 0x8e,
        0xff => 0xdf,
        _ => unreachable!(),
    }
}
#[inline]
/// `make_ebcdic` converts a byte encoded as ASCII to it's EBDIC equivalent, in-place.
/// use `to_ebcdic` if you want to make a copy
pub fn make_ebcdic(bp: &mut u8) {
    *bp = to_ebcdic(*bp); 
} 

/// makes an uppercase ASCII character into a lowercase one
/// ```
/// # use encoding8::ascii::to_uppercase;
/// for (low, high) in (b'a'..=b'z').zip(b'A'..=b'Z') {
///     assert_eq!(to_uppercase(low), high)
/// }
/// ```
pub fn to_uppercase(b: u8) -> u8 {
    match b {
        b'a'..=b'z' => b - CASE_OFFSET,
        _ => b,
    }
}

pub fn to_lowercase(b: u8) -> u8 {
    match b {
        b'A'..=b'Z' => b + CASE_OFFSET,
        _ => b,
    }
}
/// returns true if b is a lowercase ASCII character
/// ```
/// # use encoding8::ascii::is_lowercase;
/// for b in 0..=255 {
///     match b {
///         b'a'..=b'z' => assert!(is_lowercase(b), "{}", b as char),
///         b => assert!(!is_lowercase(b)),
///     }
/// }
/// ```

pub fn is_lowercase(b: u8) -> bool {
    b'a' <= b && b <= b'z'
}

/// returns true if b is an uppercase ASCII character
/// ```
/// # use encoding8::ascii::is_uppercase;
/// for b in 0..=255 {
///     match b {
///         b'A'..=b'Z' => assert!(is_uppercase(b), "{}", b as char),
///         b => assert!(!is_uppercase(b)),
///     }
/// }
/// ```

pub fn is_uppercase(b: u8) -> bool {
    b'A' <= b && b <= b'Z'
}

/// returns true if b is a digit (i.e, 0..=9)
/// ```
/// # use encoding8::ascii::is_numeric;
/// for b in 0..=255 {
///     match b {
///         b'0'..=b'9' => assert!(is_numeric(b), "{}", b as char),
///         b => assert!(!is_numeric(b)),
///     }
/// }
/// ```
pub fn is_numeric(b: u8) -> bool {
    b'0' <= b && b <= b'9'
}

/// is_control returns true if b is a control (non-printable) ASCII character
/// ```
/// # use encoding8::ascii::is_control;
/// for b in 0..=255 {
///     match b {
///         0..=31 | 127 => assert!(is_control(b), "{}", b as char),
///         b => assert!(!is_control(b)),
///     }
/// }
/// ```
pub fn is_control(b: u8) -> bool {
    b < 32 || b == DEL
}

/// is_printable returns true if b is a printable (non-control) character
/// ```
/// # use encoding8::ascii::is_printable;
/// for b in 0..=255 {
///     match b {
///         0..=31| 127 => assert!(!is_printable(b), "{}", b as char),
///         b => assert!(is_printable(b)),
///     }
/// }
/// ```
pub fn is_printable(b: u8) -> bool {
    return !is_control(b);
}

/// is_alphabetical returns true if b is an alphabetical character
///  ```
/// # use encoding8::ascii::is_alphabetical;
/// for b in 0..=255 {
///     match b {
///         b'a'..=b'z' | b'A'..=b'Z' => assert!(is_alphabetical(b), "{}", b as char),
///         _ => assert!(!is_alphabetical(b)),
///     };
/// };
/// ```
pub fn is_alphabetical(b: u8) -> bool {
    is_lowercase(b) || is_uppercase(b)
}

/// is_alphanumeric returns true if b is a digit or alphabetical character
/// ```
/// # use encoding8::ascii::is_alphanumeric;
/// for b in 0..=255 {
///     match b {
///         b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => assert!(is_alphanumeric(b), "{}", b as char),
///         b => assert!(!is_alphanumeric(b)),
///     }   
/// }
/// ```
pub fn is_alphanumeric(b: u8) -> bool {
    is_alphabetical(b) || is_numeric(b)
}