diff --git a/README.md b/README.md index d9028a6..16943cf 100644 --- a/README.md +++ b/README.md @@ -58,11 +58,12 @@ TODO ## Limitations -This currently only supports lowercase text in the latin alphabet, and byte arrays. +This currently only supports lowercase text in the latin alphabet without numbers or anything - however, using `cat2text::core`, you can convert anything to meows, as long as you can put it in integers - which, you can. ## TODO +- Add more bases - any base option? + - Once this is done, set base 4 as aliases of this - Make `core::{cat_to_num, num_to_cat}` usage consistent with each other - Improve CLI - Add error handling -- Do `AsRef` stuff diff --git a/src/anybase.rs b/src/anybase.rs index 61957ad..8448623 100644 --- a/src/anybase.rs +++ b/src/anybase.rs @@ -118,7 +118,7 @@ pub fn decode(text: String, base: u32, char_length: u32) -> String { } pub mod bytes { - use super::alphabet; + use crate::anybase::alphabet; use crate::core; /// Encodes from bytes into catspeak /// diff --git a/src/base4.rs b/src/base4.rs index 5f9625d..cddd51b 100644 --- a/src/base4.rs +++ b/src/base4.rs @@ -1,9 +1,10 @@ -use crate::anybase; +use crate::core; pub fn alphabet() -> Vec { - let mut tmp = anybase::alphabet(); - tmp.truncate(4); - return tmp; + return vec!["meow", "mrrp", "mreow", "mrow"] + .into_iter() + .map(|a| a.to_string()) + .collect(); } /// How many words long a character is when translated to catspeak @@ -11,16 +12,42 @@ 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) -> String { - return anybase::encode(text.as_ref().to_string(), 4, char_length()); + let text = text.as_ref(); + // makes it lowercase and split by spaces + let words: Vec = text + .to_ascii_lowercase() + .split(" ") + .map(|item| return item.to_string()) + .collect(); + let mut words_as_bytes: Vec> = 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::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 = results.into_iter().map(|item| item.join(" ")).collect(); + let results = results.join("; "); + return results; } /// Decodes base 4 catspeak to english text @@ -31,32 +58,70 @@ pub fn encode(text: impl AsRef) -> 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 { - return anybase::decode(text, 4, char_length()) + let catspeak_words: Vec = 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(); } + pub mod bytes { - 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, char_length()) - } - - /// 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 { - anybase::bytes::decode(text, 4, char_length()) + 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 += " "; } + return output.trim().to_string(); } -// ..............d......4..............a.q..a....y..d..w...a............ \ No newline at end of file + +/// 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 { + let mut output: Vec = 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); + } + return output; +} + +} \ No newline at end of file