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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#![cfg_attr(feature = "nightly", feature(external_doc))]
#![cfg_attr(feature = "nightly", doc(include = "../README.md"))]
#![no_std]
#[macro_use]
extern crate alloc;
use alloc::{string::String, vec::Vec};
pub const ENC_TABLE: &'static [char; 2048] = &include!("./enc_table.src");
pub const DEC_TABLE: &'static [u16; 4347] = &include!("./dec_table.src");
pub const TAIL: &'static [char; 8] = &['།', '༎', '༏', '༐', '༑', '༆', '༈', '༒'];
pub fn encode(bytes: &[u8]) -> String {
let mut ret = String::new();
let mut stage = 0x0000u16;
let mut remaining = 0;
for byte in bytes {
let byte = *byte as u16;
let need = 11 - remaining;
if need <= 8 {
remaining = 8 - need;
let index = (stage << need) | (byte >> remaining);
ret.push(ENC_TABLE[index as usize]);
stage = byte & ((1 << remaining) - 1);
} else {
stage = (stage << 8) | byte;
remaining += 8;
}
}
if remaining > 0 {
if remaining <= 3 {
ret.push(TAIL[stage as usize]);
} else {
ret.push(ENC_TABLE[stage as usize])
}
}
ret
}
pub fn decode(string: &str) -> Option<Vec<u8>> {
let mut ret = vec![];
let mut remaining = 0u8;
let mut stage = 0x00u32;
let mut chars = string.chars().peekable();
let mut residue = 0;
while let Some(c) = chars.next() {
residue = (residue + 11) % 8;
let (n_new_bits, new_bits) = match DEC_TABLE[c as usize] {
0xFFFF => {
if chars.peek().is_some() {
return None;
}
match TAIL.iter().enumerate().find(|(_, t)| *t == &c) {
Some((index, _)) => {
let need = 8 - remaining;
if index < (1 << need) {
(need, index as u16)
} else {
return None;
}
}
None => return None,
}
}
new_bits => {
if chars.peek().is_none() {
(11 - residue, new_bits)
} else {
(11, new_bits)
}
}
};
remaining += n_new_bits;
stage = (stage << n_new_bits) | new_bits as u32;
while remaining >= 8 {
remaining -= 8;
ret.push((stage >> remaining) as u8);
stage = stage & ((1 << remaining) - 1)
}
}
if remaining > 0 {
ret.push((stage >> (8 - remaining)) as u8)
}
Some(ret)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn encode_decode_roundtrip() {
for tv in &[
vec![],
vec![0],
vec![216, 110, 27, 35, 23, 49, 153, 7, 161, 234, 63],
vec![0b11111111, 0b11111111],
vec![
0b11111000, 0b00011111, 0b00000011, 0b11100000, 0b01111100, 0b00001111, 0b10000001,
0b11110000, 0b00111110, 0b00000111, 0b11000000,
],
vec![
0b11111000, 0b00011111, 0b00000011, 0b11100000, 0b01111100, 0b00001111, 0b10000001,
0b11110000, 0b00111110, 0b00000111, 0b11000000, 0b11111000,
],
vec![0b10101010; 3],
vec![0b10101010; 7],
vec![0b10101010; 10],
vec![0b00000000; 3],
vec![0b00000000; 7],
vec![0b00000000; 10],
vec![0b11111111; 3],
vec![0b11111111; 7],
vec![0b11111111; 10],
vec![0b01010101; 3],
vec![0b01010101; 7],
vec![0b01010101; 10],
vec![0b11110100; 3],
vec![0b11110100; 7],
vec![0b11110100; 10],
vec![0b11110110; 3],
vec![0b11110110; 7],
vec![0b11110110; 10],
vec![0b11110001; 3],
vec![0b11110001; 7],
vec![0b11110001; 10],
] {
let encoded = encode(&tv[..]);
let decoded = decode(&encoded).unwrap();
assert_eq!(tv[..], decoded[..]);
}
}
#[test]
fn wrong_tail_character() {
assert!(decode("ၐߥۻ༞ɵ༎").is_some());
assert!(decode("ၐߥۻ༞ɵ༑").is_none());
assert!(decode("ၐߥۻ༞ɵX").is_none());
assert!(decode("ၐߥۻ༞ɵ༎X").is_none());
}
}