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
|
## 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
|
## 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
|
- Make `core::{cat_to_num, num_to_cat}` usage consistent with each other
|
||||||
- Improve CLI
|
- Improve CLI
|
||||||
- Add error handling
|
- Add error handling
|
||||||
- Do `AsRef` stuff
|
|
||||||
|
|
|
@ -118,7 +118,7 @@ pub fn decode(text: String, base: u32, char_length: u32) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod bytes {
|
pub mod bytes {
|
||||||
use super::alphabet;
|
use crate::anybase::alphabet;
|
||||||
use crate::core;
|
use crate::core;
|
||||||
/// Encodes from bytes into catspeak
|
/// Encodes from bytes into catspeak
|
||||||
///
|
///
|
||||||
|
|
129
src/base4.rs
129
src/base4.rs
|
@ -1,9 +1,10 @@
|
||||||
use crate::anybase;
|
use crate::core;
|
||||||
|
|
||||||
pub fn alphabet() -> Vec<String> {
|
pub fn alphabet() -> Vec<String> {
|
||||||
let mut tmp = anybase::alphabet();
|
return vec!["meow", "mrrp", "mreow", "mrow"]
|
||||||
tmp.truncate(4);
|
.into_iter()
|
||||||
return tmp;
|
.map(|a| a.to_string())
|
||||||
|
.collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// How many words long a character is when translated to catspeak
|
/// How many words long a character is when translated to catspeak
|
||||||
|
@ -11,16 +12,42 @@ pub fn char_length() -> u32 {
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Encodes english text into base 4 catspeak
|
/// Encodes english text into base 4 catspeak
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use cat2text::base4::encode;
|
/// 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()))
|
/// 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 {
|
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
|
/// 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()));
|
/// 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 {
|
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 {
|
pub mod bytes {
|
||||||
use crate::anybase;
|
use crate::core;
|
||||||
use super::char_length;
|
use crate::base4::alphabet;
|
||||||
/// Encodes from bytes into catspeak
|
/// Encodes from bytes into catspeak
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use cat2text::base4::bytes::encode;
|
/// use cat2text::base4::bytes::encode;
|
||||||
///
|
///
|
||||||
/// assert_eq!("meow meow mreow mrrp meow meow meow mrrp", encode(&[9, 1]));
|
/// assert_eq!("meow meow mreow mrrp meow meow meow mrrp", encode(&[9, 1]));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn encode(bytes: impl AsRef<[u8]>) -> String {
|
pub fn encode(bytes: impl AsRef<[u8]>) -> String {
|
||||||
anybase::bytes::encode(bytes, 4, char_length())
|
let mut output = String::new();
|
||||||
}
|
for byte in bytes.as_ref() {
|
||||||
|
output += core::num_to_cat(*byte as u32, alphabet(), 4).as_str();
|
||||||
/// Decodes catspeak into bytes
|
output += " ";
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// 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())
|
|
||||||
}
|
}
|
||||||
|
return output.trim().to_string();
|
||||||
}
|
}
|
||||||
// ..............d......4..............a.q..a....y..d..w...a............
|
|
||||||
|
/// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue