File: alphabet.rs

package info (click to toggle)
rust-base-x 0.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 128 kB
  • sloc: makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,352 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use encoder::{AsciiEncoder, Utf8Encoder};
use decoder::{AsciiDecoder, Utf8Decoder};
use DecodeError;

use std::ascii::AsciiExt;

const INVALID_INDEX: u8 = 0xFF;

pub trait Alphabet {
    fn encode(&self, input: &[u8]) -> String;

    fn decode(&self, input: &str) -> Result<Vec<u8>, DecodeError>;
}

impl<'a> Alphabet for &'a [u8] {
    #[inline(always)]
    fn encode(&self, input: &[u8]) -> String {
        AsciiEncoder::encode(*self, input)
    }

    #[inline(always)]
    fn decode(&self, input: &str) -> Result<Vec<u8>, DecodeError> {
        let mut lookup = [INVALID_INDEX; 256];

        for (i, byte) in self.iter().enumerate() {
            lookup[*byte as usize] = i as u8;
        }

        AsciiDecoder::decode(*self, lookup, input)
    }
}

impl<'a> Alphabet for &'a str {
    #[inline(always)]
    fn encode(&self, input: &[u8]) -> String {
        if self.is_ascii() {
            return self.as_bytes().encode(input);
        }

        let alphabet: Vec<char> = self.chars().collect();
        Utf8Encoder::encode(&alphabet, input)
    }

    #[inline(always)]
    fn decode(&self, input: &str) -> Result<Vec<u8>, DecodeError> {
        if self.is_ascii() {
            return self.as_bytes().decode(input);
        }

        let alphabet: Vec<char> = self.chars().collect();
        Utf8Decoder::decode(&alphabet, input)
    }
}