MCPcopy Create free account
hub / github.com/douchuan/algorithm / try_from

Method try_from

src/strings/alphabet.rs:57–78  ·  view source on GitHub ↗
(s: &str)

Source from the content-addressed store, hash-verified

55 type Error = String;
56
57 fn try_from(s: &str) -> Result<Self, Self::Error> {
58 let alphabet: Vec<char> = s.chars().collect();
59 let mut unicode = vec![false; MAX_VALUE];
60 for &c in &alphabet {
61 if unicode[c as usize] {
62 return Err(format!("Illegal alphabet: repeated character = {:?}", c));
63 }
64 unicode[c as usize] = true; //set code point
65 }
66
67 let radix = alphabet.len();
68 let mut inverse = vec![-1; MAX_VALUE];
69 for i in 0..radix {
70 inverse[alphabet[i] as usize] = i as i32;
71 }
72
73 Ok(Self {
74 alphabet,
75 inverse,
76 radix,
77 })
78 }
79}
80
81impl TryFrom<u16> for Alphabet {

Callers

nothing calls this directly

Calls 2

ErrEnum · 0.85
lenMethod · 0.45

Tested by

no test coverage detected