Compare commits

..

No commits in common. "b6455d8b6703ba3ae48d070e5519797189f19c04" and "500888f5e74cbbc21cc9d6f692e1285714474ffe" have entirely different histories.

3 changed files with 3 additions and 67 deletions

View file

@ -61,7 +61,6 @@ This currently only supports lowercase text in the latin alphabet without number
## 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

View file

@ -15,13 +15,6 @@ pub fn max_base() -> u32 {
}
/// Returns the minimum catspeak words per character needed for this base
///
/// ```
/// use cat2text::anybase::char_length;
///
/// let base = 10;
/// assert_eq!(char_length(base), 2)
/// ```
pub fn char_length(base: u32) -> u32 {
for i in 1..base + 1 {
let num = base.pow(i);
@ -38,12 +31,11 @@ pub fn char_length(base: u32) -> u32 {
///
/// ```
/// use cat2text::anybase;
///
/// let text = "i love cats".to_string();
/// let base = 10;
/// let char_length = anybase::char_length(base);
///
/// assert_eq!("meow mewo; mrrp mreow mrrp nyaaaa~ mreow mreow meow nyaaaa~; meow mrow meow mrrp mreow meow mrrp mewo", anybase::encode(text, base, char_length));
/// assert_eq!("", anybase::encode(text, base, char_length));
/// ```
pub fn encode(text: String, base: u32, char_length: u32) -> String {
let mut shortened_alphabet = alphabet();
@ -84,7 +76,6 @@ pub fn encode(text: String, base: u32, char_length: u32) -> String {
///
/// ```
/// use cat2text::anybase;
///
/// let text = "meow mewo; mrrp mreow mrrp nyaaaa~ mreow mreow meow nyaaaa~; meow mrow meow mrrp mreow meow mrrp mewo".to_string();
/// let base = 10;
/// let char_length = anybase::char_length(base);
@ -98,7 +89,7 @@ pub fn decode(text: String, base: u32, char_length: u32) -> String {
shortened_alphabet.truncate(base as usize);
for engl_word in catspeak_words {
let mut word = String::new();
for engl_letter in core::split_every_x(engl_word, char_length) {
for engl_letter in core::split_every_x(engl_word, char_length as usize) {
let char_num = core::cat_to_num(
engl_letter
.split(" ")
@ -117,56 +108,3 @@ pub fn decode(text: String, base: u32, char_length: u32) -> String {
return output.trim().to_string();
}
pub mod bytes {
use crate::anybase::alphabet;
use crate::core;
/// Encodes from bytes into catspeak
///
/// ```
/// use cat2text::anybase::{bytes::encode, char_length};
///
/// let bytes = &[9, 1];
/// let base = 10;
/// let char_length = char_length(base);
///
/// assert_eq!("meow mewo meow mrrp", encode(bytes, base, char_length));
/// ```
pub fn encode(bytes: impl AsRef<[u8]>, base: u32, char_length: u32) -> String {
let mut output = String::new();
let mut shortened_alphabet = alphabet();
shortened_alphabet.truncate(base as usize);
for byte in bytes.as_ref() {
output += core::num_to_cat(*byte as u32, shortened_alphabet.clone(), char_length).as_str();
output += " ";
}
return output.trim().to_string();
}
/// Decodes catspeak into bytes
///
/// ```
/// use cat2text::anybase::{bytes::decode, char_length};
///
/// let text = "mreow mrrp meow mrrp".to_string();
/// let base = 10;
/// let char_length = char_length(base);
///
/// assert_eq!(
/// vec![21, 1],
/// decode(text, base, char_length)
/// );
/// ```
pub fn decode(text: String, base: u32, char_length: u32) -> Vec<u8> {
let mut output: Vec<u8> = Vec::new();
let mut shortened_alphabet = alphabet();
shortened_alphabet.truncate(base as usize);
for byte in core::split_every_x(text.clone(), char_length) {
output.push(core::cat_to_num(
byte.split(" ").map(|item| item.to_string()).collect(),
shortened_alphabet.clone(),
char_length,
) as u8);
}
return output.into();
}
}

View file

@ -65,8 +65,7 @@ pub fn cat_to_num(text: Vec<String>, alphabet: Vec<String>, char_length: u32) ->
///
/// assert_eq!(vec!["meow meow mrrp".to_string(), "meow mreow mrrp".to_string(), "mreow meow mrrp".to_string()], split_every_x("meow meow mrrp meow mreow mrrp mreow meow mrrp".to_string(), 3));
/// ```
pub fn split_every_x(text: String, x: u32) -> Vec<String> {
let x = x as usize;
pub fn split_every_x(text: String, x: usize) -> Vec<String> {
let delim = " ";
let tmp: Vec<String> = text.split(delim).map(|item| item.to_string()).collect();
let mut output: Vec<String> = Vec::new();