Compare commits
No commits in common. "9bcfd8d2e5222d2f658fd95de1e283cb9b53fcee" and "4e59871c0d146d578a220c4cae31c651310793e3" have entirely different histories.
9bcfd8d2e5
...
4e59871c0d
3 changed files with 101 additions and 35 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
///
|
||||
|
|
125
src/base4.rs
125
src/base4.rs
|
@ -1,9 +1,10 @@
|
|||
use crate::anybase;
|
||||
use crate::core;
|
||||
|
||||
pub fn alphabet() -> Vec<String> {
|
||||
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,7 +12,6 @@ pub fn char_length() -> u32 {
|
|||
return 3;
|
||||
}
|
||||
|
||||
|
||||
/// Encodes english text into base 4 catspeak
|
||||
///
|
||||
/// ```
|
||||
|
@ -20,7 +20,34 @@ pub fn char_length() -> u32 {
|
|||
/// 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 {
|
||||
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<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;
|
||||
}
|
||||
|
||||
/// Decodes base 4 catspeak to english text
|
||||
|
@ -31,32 +58,70 @@ 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 {
|
||||
return anybase::decode(text, 4, char_length())
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
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<u8> {
|
||||
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();
|
||||
}
|
||||
|
||||
/// 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);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
}
|
||||
// ..............d......4..............a.q..a....y..d..w...a............
|
Loading…
Add table
Add a link
Reference in a new issue