Compare commits

...

7 commits

Author SHA1 Message Date
askiiart
a9d81f015e
update tests 2025-01-15 12:22:50 -06:00
askiiart
9fac0f3667
fix bytes stuff - STILL NEED TO FIX TESTS 2025-01-15 10:35:42 -06:00
askiiart
7111192081
fix missing fields 2025-01-15 08:53:36 -06:00
askiiart
5e315db651
chore: format 2025-01-15 08:51:52 -06:00
askiiart
4fc0cbc078
bump version 2025-01-15 08:46:05 -06:00
askiiart
ec17c263d0
update cargo.toml 2025-01-15 08:42:03 -06:00
askiiart
189ceefbad
fix typo 2025-01-15 08:31:34 -06:00
5 changed files with 67 additions and 46 deletions

2
Cargo.lock generated
View file

@ -4,4 +4,4 @@ version = 4
[[package]] [[package]]
name = "cat2text" name = "cat2text"
version = "0.1.2" version = "0.1.3"

View file

@ -1,10 +1,14 @@
[package] [package]
name = "cat2text" name = "cat2text"
version = "0.1.2" version = "0.1.3"
edition = "2021" edition = "2021"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
description = "A port of Cat2Text to Rust, with extra functionality, better documentation, and support for using it as a library as well."
readme = "README.md"
repository = "https://git.askiiart.net/askiiart/cat2text-rs" repository = "https://git.askiiart.net/askiiart/cat2text-rs"
authors = ["askiiart <mail@askiiart.net>"] authors = ["askiiart <mail@askiiart.net>"]
categories = ["encoding"]
keywords = ["base4", "meow", "base16"]
[profile.release] [profile.release]
opt-level = 3 opt-level = 3

View file

@ -1,4 +1,4 @@
//! This module translates text //! This module translates text
use crate::core; use crate::core;
/// Encodes text into catspeak using any base up to [`max_base()`] /// Encodes text into catspeak using any base up to [`max_base()`]
@ -91,20 +91,21 @@ pub mod bytes {
/// Encodes from bytes into catspeak /// Encodes from bytes into catspeak
/// ///
/// ``` /// ```
/// use cat2text::{anybase::bytes::encode, core::char_length}; /// use cat2text::{anybase::bytes::encode, core::bytes::char_length};
/// ///
/// let bytes = &[9, 1]; /// let bytes = &[243, 10];
/// let base = 10; /// let base = 16;
/// let char_length = char_length(base); /// let char_length = char_length(base);
/// ///
/// assert_eq!("meow mewo meow mrrp", encode(bytes, base, char_length)); /// assert_eq!("mrow~ mrow meow purrrr", encode(bytes, base, char_length));
/// ``` /// ```
pub fn encode(bytes: impl AsRef<[u8]>, base: u32, char_length: u32) -> String { pub fn encode(bytes: impl AsRef<[u8]>, base: u32, char_length: u32) -> String {
let mut output = String::new(); let mut output = String::new();
let mut shortened_alphabet = core::alphabet(); let mut shortened_alphabet = core::alphabet();
shortened_alphabet.truncate(base as usize); shortened_alphabet.truncate(base as usize);
for byte in bytes.as_ref() { for byte in bytes.as_ref() {
output += core::num_to_cat(*byte as u32, shortened_alphabet.clone(), char_length).as_str(); output +=
core::num_to_cat(*byte as u32, shortened_alphabet.clone(), char_length).as_str();
output += " "; output += " ";
} }
return output.trim().to_string(); return output.trim().to_string();
@ -113,14 +114,14 @@ pub mod bytes {
/// Decodes catspeak into bytes /// Decodes catspeak into bytes
/// ///
/// ``` /// ```
/// use cat2text::{anybase::bytes::decode, core::char_length}; /// use cat2text::{anybase::bytes::decode, core::bytes::char_length};
/// ///
/// let text = "mreow mrrp meow mrrp".to_string(); /// let text = "mrow~ mrow meow purrrr".to_string();
/// let base = 10; /// let base = 16;
/// let char_length = char_length(base); /// let char_length = char_length(base);
/// ///
/// assert_eq!( /// assert_eq!(
/// vec![21, 1], /// vec![243, 10],
/// decode(text, base, char_length) /// decode(text, base, char_length)
/// ); /// );
/// ``` /// ```
@ -138,4 +139,3 @@ pub mod bytes {
return output.into(); return output.into();
} }
} }
// ...

View file

@ -1,6 +1,6 @@
//! This module handles base 4, like the original [Cat2Text](https://github.com/Evelyn3440/Cat2Text); it can translate either english text a-z, or byte arrays (see [`bytes`]) //! This module handles base 4, like the original [Cat2Text](https://github.com/Evelyn3440/Cat2Text); it can translate either english text a-z, or byte arrays (see [`bytes`])
use crate::core;
use crate::anybase; use crate::anybase;
use crate::core;
/// Returns the alphabet used by `cat2text::base4` /// Returns the alphabet used by `cat2text::base4`
pub fn alphabet() -> Vec<String> { pub fn alphabet() -> Vec<String> {
@ -9,12 +9,6 @@ pub fn alphabet() -> Vec<String> {
return tmp; return tmp;
} }
/// How many words long an english character is when translated to catspeak
pub fn char_length() -> u32 {
return 3;
}
/// Encodes english text into base 4 catspeak /// Encodes english text into base 4 catspeak
/// ///
/// ``` /// ```
@ -23,7 +17,7 @@ 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())) /// 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()); return anybase::encode(text.as_ref().to_string(), 4, core::char_length(4));
} }
/// Decodes base 4 catspeak to english text /// Decodes base 4 catspeak to english text
@ -34,22 +28,22 @@ 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()) return anybase::decode(text, 4, core::char_length(4));
} }
pub mod bytes { pub mod bytes {
//! This handles encoding and decoding bytes to/from catspeak //! This handles encoding and decoding bytes to/from catspeak
use crate::anybase; use crate::anybase;
use super::char_length; use crate::core::bytes::char_length;
/// Encodes from bytes into catspeak /// Encodes from bytes into catspeak
/// ///
/// ``` /// ```
/// use cat2text::base4::bytes::encode; /// use cat2text::base4::bytes::encode;
/// ///
/// assert_eq!("meow mreow mrrp meow meow mrrp", encode(&[9, 1])); /// assert_eq!("mrow mrow mrow mrrp meow meow meow mrrp", encode(&[253, 1]));
/// ``` /// ```
pub fn encode(bytes: impl AsRef<[u8]>) -> String { pub fn encode(bytes: impl AsRef<[u8]>) -> String {
anybase::bytes::encode(bytes, 4, char_length()) anybase::bytes::encode(bytes, 4, char_length(4))
} }
/// Decodes catspeak into bytes /// Decodes catspeak into bytes
@ -57,9 +51,9 @@ pub mod bytes {
/// ``` /// ```
/// use cat2text::base4::bytes::decode; /// use cat2text::base4::bytes::decode;
/// ///
/// assert_eq!(vec![9, 1], decode("meow mreow mrrp meow meow mrrp".to_string())); /// assert_eq!(vec![253, 1], decode("mrow mrow mrow mrrp meow meow meow mrrp".to_string()));
/// ``` /// ```
pub fn decode(text: String) -> Vec<u8> { pub fn decode(text: String) -> Vec<u8> {
anybase::bytes::decode(text, 4, char_length()) anybase::bytes::decode(text, 4, char_length(4))
} }
} }

View file

@ -3,10 +3,10 @@
/// Converts a [`u32`] to catspeak /// Converts a [`u32`] to catspeak
/// ///
/// ``` /// ```
/// use cat2text::core::num_to_cat; /// use cat2text::core::{num_to_cat, char_length};
/// use cat2text::base4::{alphabet, char_length}; /// use cat2text::base4::alphabet;
/// ///
/// assert_eq!("meow mreow mrrp".to_string(), num_to_cat(9, alphabet(), char_length())); /// assert_eq!("meow mreow mrrp".to_string(), num_to_cat(9, alphabet(), char_length(4)));
/// ``` /// ```
pub fn num_to_cat(num: u32, alphabet: Vec<String>, char_length: u32) -> String { pub fn num_to_cat(num: u32, alphabet: Vec<String>, char_length: u32) -> String {
let mut num: u32 = num.clone(); let mut num: u32 = num.clone();
@ -31,12 +31,12 @@ pub fn num_to_cat(num: u32, alphabet: Vec<String>, char_length: u32) -> String {
/// Converts catspeak to a [`u32`] /// Converts catspeak to a [`u32`]
/// ///
/// ``` /// ```
/// use cat2text::core::cat_to_num; /// use cat2text::core::{cat_to_num, bytes::char_length};
/// use cat2text::base4::{alphabet, char_length}; /// use cat2text::base4::alphabet;
/// ///
/// let letter = vec!["meow".to_string(), "mreow".to_string(), "mrrp".to_string()]; /// let text = vec!["meow".to_string(), "mrrp".to_string(), "mrow".to_string(), "meow".to_string()];
/// ///
/// assert_eq!(9, cat_to_num(letter, alphabet(), char_length())); /// assert_eq!(28, cat_to_num(text, alphabet(), char_length(4)));
/// ``` /// ```
pub fn cat_to_num(text: Vec<String>, alphabet: Vec<String>, char_length: u32) -> u32 { pub fn cat_to_num(text: Vec<String>, alphabet: Vec<String>, char_length: u32) -> u32 {
let mut nums: Vec<u32> = Vec::new(); let mut nums: Vec<u32> = Vec::new();
@ -59,7 +59,7 @@ pub fn cat_to_num(text: Vec<String>, alphabet: Vec<String>, char_length: u32) ->
} }
/// Splits a word encoded in catspeak every *x* segments /// Splits a word encoded in catspeak every *x* segments
/// ///
/// Used for decoding by splitting words apart into letters which can then be decoded individually /// Used for decoding by splitting words apart into letters which can then be decoded individually
/// ///
/// ```ignore /// ```ignore
@ -81,21 +81,24 @@ pub(crate) fn split_every_x(text: String, x: u32) -> Vec<String> {
} }
// trim everything before sending it back // trim everything before sending it back
output = output.into_iter().map(|item| item.trim().to_string()).collect(); output = output
.into_iter()
.map(|item| item.trim().to_string())
.collect();
return output; return output;
} }
/// Returns all cat sounds in the catspeak alphabet /// Returns all cat sounds in the catspeak alphabet
/// ///
/// ``` /// ```
/// use cat2text::core::alphabet; /// use cat2text::core::alphabet;
/// ///
/// println!("{:?}", alphabet()); /// println!("{:?}", alphabet());
/// ``` /// ```
pub fn alphabet() -> Vec<String> { pub fn alphabet() -> Vec<String> {
return vec![ return vec![
"meow", "mrrp", "mreow", "mrow", "nya~", "nyaaaa~", "mraow", "mew", "prrp", "mewo", "meow", "mrrp", "mreow", "mrow", "nya~", "nyaaaa~", "mraow", "mew", "prrp", "mewo",
"purrrr", "nya", "miao", "miau", "miauw", "mrow~" "purrrr", "nya", "miao", "miau", "miauw", "mrow~",
] ]
.into_iter() .into_iter()
.map(|a| a.to_string()) .map(|a| a.to_string())
@ -103,12 +106,12 @@ pub fn alphabet() -> Vec<String> {
} }
/// Returns the max base that can be used /// Returns the max base that can be used
/// ///
/// For example, if the available alphabet was `["meow", "mrrp", "mreow", "mrow"]`, the max base would be 4 /// For example, if the available alphabet was `["meow", "mrrp", "mreow", "mrow"]`, the max base would be 4
/// ///
/// ``` /// ```
/// use cat2text::core::max_base; /// use cat2text::core::max_base;
/// ///
/// println!("{}", max_base()); /// println!("{}", max_base());
/// ``` /// ```
pub fn max_base() -> u32 { pub fn max_base() -> u32 {
@ -116,10 +119,10 @@ pub fn max_base() -> u32 {
} }
/// Returns the minimum catspeak words per character needed for this base /// Returns the minimum catspeak words per character needed for this base
/// ///
/// ``` /// ```
/// use cat2text::core::char_length; /// use cat2text::core::char_length;
/// ///
/// let base = 10; /// let base = 10;
/// assert_eq!(char_length(base), 2) /// assert_eq!(char_length(base), 2)
/// ``` /// ```
@ -131,4 +134,24 @@ pub fn char_length(base: u32) -> u32 {
} }
} }
return u32::MAX; return u32::MAX;
} }
pub mod bytes {
/// Returns the minimum catspeak words per character needed for this base for bytes
///
/// ```
/// use cat2text::core::bytes::char_length;
///
/// let base = 16;
/// assert_eq!(char_length(base), 2)
/// ```
pub fn char_length(base: u32) -> u32 {
for i in 1..base + 1 {
let num = base.pow(i);
if num > 255 {
return i;
}
}
return u32::MAX;
}
}