have base4 just be aliases for anybase
This commit is contained in:
parent
4e59871c0d
commit
02212e3bef
1 changed files with 30 additions and 95 deletions
125
src/base4.rs
125
src/base4.rs
|
@ -1,10 +1,9 @@
|
|||
use crate::core;
|
||||
use crate::anybase;
|
||||
|
||||
pub fn alphabet() -> Vec<String> {
|
||||
return vec!["meow", "mrrp", "mreow", "mrow"]
|
||||
.into_iter()
|
||||
.map(|a| a.to_string())
|
||||
.collect();
|
||||
let mut tmp = anybase::alphabet();
|
||||
tmp.truncate(4);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// How many words long a character is when translated to catspeak
|
||||
|
@ -12,42 +11,16 @@ pub fn char_length() -> u32 {
|
|||
return 3;
|
||||
}
|
||||
|
||||
|
||||
/// Encodes english text into base 4 catspeak
|
||||
///
|
||||
///
|
||||
/// ```
|
||||
/// use cat2text::base4::encode;
|
||||
///
|
||||
///
|
||||
/// assert_eq!("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", encode("i love cats".to_string()))
|
||||
/// ```
|
||||
pub fn encode(text: impl AsRef<str>) -> String {
|
||||
let text = text.as_ref();
|
||||
// makes it lowercase and split by spaces
|
||||
let words: Vec<String> = text
|
||||
.to_ascii_lowercase()
|
||||
.split(" ")
|
||||
.map(|item| return item.to_string())
|
||||
.collect();
|
||||
let mut words_as_bytes: Vec<Vec<u8>> = Vec::new();
|
||||
for word in words {
|
||||
// convert to bytes then subtract by 96
|
||||
words_as_bytes.push(word.as_bytes().into_iter().map(|item| item - 96).collect());
|
||||
}
|
||||
|
||||
let mut results: Vec<Vec<String>> = Vec::new();
|
||||
for i in 0..words_as_bytes.len() {
|
||||
results.push(Vec::new());
|
||||
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(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let results: Vec<String> = results.into_iter().map(|item| item.join(" ")).collect();
|
||||
let results = results.join("; ");
|
||||
return results;
|
||||
return anybase::encode(text.as_ref().to_string(), 4, char_length());
|
||||
}
|
||||
|
||||
/// Decodes base 4 catspeak to english text
|
||||
|
@ -58,70 +31,32 @@ pub fn encode(text: impl AsRef<str>) -> String {
|
|||
/// 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("; ")
|
||||
.map(|item| item.to_string())
|
||||
.collect();
|
||||
let mut output: String = String::new();
|
||||
for engl_word in catspeak_words {
|
||||
let mut word = String::new();
|
||||
for engl_letter in core::split_every_x(engl_word, 3) {
|
||||
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();
|
||||
return anybase::decode(text, 4, char_length())
|
||||
}
|
||||
|
||||
|
||||
pub mod bytes {
|
||||
use crate::core;
|
||||
use crate::base4::alphabet;
|
||||
/// Encodes from bytes into catspeak
|
||||
///
|
||||
/// ```
|
||||
/// use cat2text::base4::bytes::encode;
|
||||
///
|
||||
/// assert_eq!("meow meow mreow mrrp meow meow meow mrrp", encode(&[9, 1]));
|
||||
/// ```
|
||||
pub fn encode(bytes: impl AsRef<[u8]>) -> String {
|
||||
let mut output = String::new();
|
||||
for byte in bytes.as_ref() {
|
||||
output += core::num_to_cat(*byte as u32, alphabet(), 4).as_str();
|
||||
output += " ";
|
||||
use crate::anybase;
|
||||
use super::char_length;
|
||||
/// Encodes from bytes into catspeak
|
||||
///
|
||||
/// ```
|
||||
/// use cat2text::base4::bytes::encode;
|
||||
///
|
||||
/// assert_eq!("meow meow mreow mrrp meow meow meow mrrp", encode(&[9, 1]));
|
||||
/// ```
|
||||
pub fn encode(bytes: impl AsRef<[u8]>) -> String {
|
||||
anybase::bytes::encode(bytes, 4, super::char_length())
|
||||
}
|
||||
return output.trim().to_string();
|
||||
}
|
||||
|
||||
/// Decodes catspeak into bytes
|
||||
///
|
||||
/// ```
|
||||
/// use cat2text::base4::bytes::decode;
|
||||
///
|
||||
/// assert_eq!(vec![9, 1], decode("meow meow mreow mrrp meow meow meow mrrp".to_string()));
|
||||
/// ```
|
||||
pub fn decode(text: String) -> Vec<u8> {
|
||||
let mut output: Vec<u8> = Vec::new();
|
||||
for byte in core::split_every_x(text.clone(), 4) {
|
||||
output.push(core::cat_to_num(
|
||||
byte.split(" ").map(|item| item.to_string()).collect(),
|
||||
alphabet(),
|
||||
4,
|
||||
) as u8);
|
||||
/// Decodes catspeak into bytes
|
||||
///
|
||||
/// ```
|
||||
/// use cat2text::base4::bytes::decode;
|
||||
///
|
||||
/// assert_eq!(vec![9, 1], decode("meow meow mreow mrrp meow meow meow mrrp".to_string()));
|
||||
/// ```
|
||||
pub fn decode(text: String) -> Vec<u8> {
|
||||
anybase::bytes::decode(text, 4, super::char_length())
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
}
|
||||
// ..............d......4..............a.q..a....y..d..w...a............
|
Loading…
Add table
Reference in a new issue