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
use crate::{DOT_LINE, LETTER};
use std::collections::BTreeMap;
pub fn decode(input: &str) -> String {
let mut result = String::new();
let mut map = BTreeMap::new();
for (k, v) in DOT_LINE.iter().zip(LETTER.iter()) {
map.insert(*k, v);
}
let normed = input.replace("*", ".").replace("-", "_");
for word in normed.trim().split("/") {
for c in word.trim().split(" ") {
let ch = match map.get(c) {
Some(o) => **o,
None => decode_missing(c),
};
result.push(ch);
}
result.push(' ');
}
result.pop();
result
}
fn decode_missing(s: &str) -> char {
let n = s.replace('_', "1").replace('.', "0");
if let Ok(u) = u32::from_str_radix(&n, 2) {
if let Some(c) = std::char::from_u32(u - 64) {
return c;
}
}
return ' ';
}