Compare commits

..

No commits in common. "a9d81f015e9c56968a2f5fd6a055bee9957c12c2" and "4ceb59d052ca8150bd26df5b91f8532ac7b840a2" have entirely different histories.

5 changed files with 46 additions and 67 deletions

2
Cargo.lock generated
View file

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

View file

@ -1,14 +1,10 @@
[package]
name = "cat2text"
version = "0.1.3"
version = "0.1.2"
edition = "2021"
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"
authors = ["askiiart <mail@askiiart.net>"]
categories = ["encoding"]
keywords = ["base4", "meow", "base16"]
[profile.release]
opt-level = 3

View file

@ -1,4 +1,4 @@
//! This module translates text
//! This module translates text
use crate::core;
/// Encodes text into catspeak using any base up to [`max_base()`]
@ -91,21 +91,20 @@ pub mod bytes {
/// Encodes from bytes into catspeak
///
/// ```
/// use cat2text::{anybase::bytes::encode, core::bytes::char_length};
/// use cat2text::{anybase::bytes::encode, core::char_length};
///
/// let bytes = &[243, 10];
/// let base = 16;
/// let bytes = &[9, 1];
/// let base = 10;
/// let char_length = char_length(base);
///
/// assert_eq!("mrow~ mrow meow purrrr", encode(bytes, base, char_length));
/// 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 = core::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 += core::num_to_cat(*byte as u32, shortened_alphabet.clone(), char_length).as_str();
output += " ";
}
return output.trim().to_string();
@ -114,14 +113,14 @@ pub mod bytes {
/// Decodes catspeak into bytes
///
/// ```
/// use cat2text::{anybase::bytes::decode, core::bytes::char_length};
/// use cat2text::{anybase::bytes::decode, core::char_length};
///
/// let text = "mrow~ mrow meow purrrr".to_string();
/// let base = 16;
/// let text = "mreow mrrp meow mrrp".to_string();
/// let base = 10;
/// let char_length = char_length(base);
///
/// assert_eq!(
/// vec![243, 10],
/// vec![21, 1],
/// decode(text, base, char_length)
/// );
/// ```
@ -139,3 +138,4 @@ pub mod bytes {
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`])
use crate::anybase;
use crate::core;
use crate::anybase;
/// Returns the alphabet used by `cat2text::base4`
pub fn alphabet() -> Vec<String> {
@ -9,6 +9,12 @@ pub fn alphabet() -> Vec<String> {
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
///
/// ```
@ -17,7 +23,7 @@ pub fn alphabet() -> Vec<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 {
return anybase::encode(text.as_ref().to_string(), 4, core::char_length(4));
return anybase::encode(text.as_ref().to_string(), 4, char_length());
}
/// Decodes base 4 catspeak to english text
@ -28,22 +34,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()));
/// ```
pub fn decode(text: String) -> String {
return anybase::decode(text, 4, core::char_length(4));
return anybase::decode(text, 4, char_length())
}
pub mod bytes {
//! This handles encoding and decoding bytes to/from catspeak
use crate::anybase;
use crate::core::bytes::char_length;
use super::char_length;
/// Encodes from bytes into catspeak
///
/// ```
/// use cat2text::base4::bytes::encode;
///
/// assert_eq!("mrow mrow mrow mrrp meow meow meow mrrp", encode(&[253, 1]));
/// assert_eq!("meow mreow mrrp meow meow mrrp", encode(&[9, 1]));
/// ```
pub fn encode(bytes: impl AsRef<[u8]>) -> String {
anybase::bytes::encode(bytes, 4, char_length(4))
anybase::bytes::encode(bytes, 4, char_length())
}
/// Decodes catspeak into bytes
@ -51,9 +57,9 @@ pub mod bytes {
/// ```
/// use cat2text::base4::bytes::decode;
///
/// assert_eq!(vec![253, 1], decode("mrow mrow mrow mrrp meow meow meow mrrp".to_string()));
/// assert_eq!(vec![9, 1], decode("meow mreow mrrp meow meow mrrp".to_string()));
/// ```
pub fn decode(text: String) -> Vec<u8> {
anybase::bytes::decode(text, 4, char_length(4))
anybase::bytes::decode(text, 4, char_length())
}
}
}

View file

@ -3,10 +3,10 @@
/// Converts a [`u32`] to catspeak
///
/// ```
/// use cat2text::core::{num_to_cat, char_length};
/// use cat2text::base4::alphabet;
/// use cat2text::core::num_to_cat;
/// use cat2text::base4::{alphabet, char_length};
///
/// assert_eq!("meow mreow mrrp".to_string(), num_to_cat(9, alphabet(), char_length(4)));
/// assert_eq!("meow mreow mrrp".to_string(), num_to_cat(9, alphabet(), char_length()));
/// ```
pub fn num_to_cat(num: u32, alphabet: Vec<String>, char_length: u32) -> String {
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`]
///
/// ```
/// use cat2text::core::{cat_to_num, bytes::char_length};
/// use cat2text::base4::alphabet;
/// use cat2text::core::cat_to_num;
/// use cat2text::base4::{alphabet, char_length};
///
/// let text = vec!["meow".to_string(), "mrrp".to_string(), "mrow".to_string(), "meow".to_string()];
/// let letter = vec!["meow".to_string(), "mreow".to_string(), "mrrp".to_string()];
///
/// assert_eq!(28, cat_to_num(text, alphabet(), char_length(4)));
/// assert_eq!(9, cat_to_num(letter, alphabet(), char_length()));
/// ```
pub fn cat_to_num(text: Vec<String>, alphabet: Vec<String>, char_length: u32) -> u32 {
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
///
///
/// Used for decoding by splitting words apart into letters which can then be decoded individually
///
/// ```ignore
@ -81,24 +81,21 @@ pub(crate) fn split_every_x(text: String, x: u32) -> Vec<String> {
}
// 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;
}
/// Returns all cat sounds in the catspeak alphabet
///
///
/// ```
/// use cat2text::core::alphabet;
///
///
/// println!("{:?}", alphabet());
/// ```
pub fn alphabet() -> Vec<String> {
return vec![
"meow", "mrrp", "mreow", "mrow", "nya~", "nyaaaa~", "mraow", "mew", "prrp", "mewo",
"purrrr", "nya", "miao", "miau", "miauw", "mrow~",
"purrrr", "nya", "miao", "miau", "miauw", "mrow~"
]
.into_iter()
.map(|a| a.to_string())
@ -106,12 +103,12 @@ pub fn alphabet() -> Vec<String> {
}
/// 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
///
///
/// ```
/// use cat2text::core::max_base;
///
///
/// println!("{}", max_base());
/// ```
pub fn max_base() -> u32 {
@ -119,10 +116,10 @@ pub fn max_base() -> u32 {
}
/// Returns the minimum catspeak words per character needed for this base
///
///
/// ```
/// use cat2text::core::char_length;
///
///
/// let base = 10;
/// assert_eq!(char_length(base), 2)
/// ```
@ -134,24 +131,4 @@ pub fn char_length(base: u32) -> u32 {
}
}
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;
}
}
}