get base4::decode working
This commit is contained in:
parent
75ad831e9e
commit
74512ffdb2
2 changed files with 35 additions and 7 deletions
34
src/base4.rs
34
src/base4.rs
|
@ -29,12 +29,42 @@ pub fn encode(text: String) -> String {
|
||||||
for i in 0..words_as_bytes.len() {
|
for i in 0..words_as_bytes.len() {
|
||||||
results.push(Vec::new());
|
results.push(Vec::new());
|
||||||
for j in 0..words_as_bytes[i].len() {
|
for j in 0..words_as_bytes[i].len() {
|
||||||
results[i].push(core::num_to_cat(words_as_bytes[i][j] as u32, alphabet(), char_length()));
|
results[i].push(core::num_to_cat(
|
||||||
|
words_as_bytes[i][j] as u32,
|
||||||
|
alphabet(),
|
||||||
|
char_length(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let results: Vec<String> = results.into_iter().map(|item| item.join(" ")).collect();
|
let results: Vec<String> = results.into_iter().map(|item| item.join(" ")).collect();
|
||||||
let results = results.join("; ");
|
let results = results.join("; ");
|
||||||
return results;
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Decodes base 4 catspeak to english text
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use cat2text::base4::decode;
|
||||||
|
///
|
||||||
|
/// assert_eq!("i love cats", decode("meow mreow mrrp; meow mrow meow meow mrow mrow mrrp mrrp mreow meow mrrp mrrp; meow meow mrow meow meow mrrp mrrp mrrp meow mrrp meow mrow".to_string()));
|
||||||
|
/// ```
|
||||||
|
pub fn decode(text: String) -> String {
|
||||||
|
let catspeak_words: Vec<String> = text
|
||||||
|
.split("; ")
|
||||||
|
.into_iter()
|
||||||
|
.map(|item| item.to_string())
|
||||||
|
.collect();
|
||||||
|
let mut output: String = String::new();
|
||||||
|
for engl_word in catspeak_words {
|
||||||
|
let mut word = "".to_string();
|
||||||
|
for engl_letter in core::split_every_3(engl_word) {
|
||||||
|
let char_num = core::cat_to_num(engl_letter.split(" ").map(|item| item.to_string()).collect(), alphabet(), char_length());
|
||||||
|
word += String::from_utf8(vec![(char_num + 96) as u8]).unwrap().as_str();
|
||||||
|
}
|
||||||
|
word += " ";
|
||||||
|
output += word.as_str();
|
||||||
|
}
|
||||||
|
|
||||||
|
return output.trim().to_string();
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,11 +18,9 @@ fn main() {
|
||||||
stdin.read_line(&mut input).unwrap();
|
stdin.read_line(&mut input).unwrap();
|
||||||
println!(
|
println!(
|
||||||
"{}",
|
"{}",
|
||||||
core::num_to_cat(
|
base4::decode(
|
||||||
input.trim().parse().unwrap(),
|
input.trim().to_string()
|
||||||
base4::alphabet(),
|
)
|
||||||
base4::char_length()
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
} else if trimmed == "2".to_string() {
|
} else if trimmed == "2".to_string() {
|
||||||
input = "".to_string();
|
input = "".to_string();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue