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
use std::collections::BTreeMap;
use crate::utils::{LETTER, DOT_LINE};
pub fn encode(input: &str) -> String {
encode_raw(input.to_lowercase().trim())
}
pub fn encode_raw(input: &str) -> String {
let mut result = String::with_capacity(10 * input.len());
let mut map = BTreeMap::new();
for (k, v) in LETTER.iter().zip(DOT_LINE.iter()) {
map.insert(*k, v);
}
for c in input.chars() {
let s = match map.get(&c) {
Some(o) => String::from(**o),
None => encode_missing(c),
};
result.push_str(&s);
result.push(' ');
}
result.pop();
return result;
}
fn encode_missing(c: char) -> String {
format!("{:b}", c as u32 + 64).replace('1', "_").replace('0', ".")
}