Compare commits
No commits in common. "a9d81f015e9c56968a2f5fd6a055bee9957c12c2" and "4ceb59d052ca8150bd26df5b91f8532ac7b840a2" have entirely different histories.
a9d81f015e
...
4ceb59d052
5 changed files with 46 additions and 67 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -4,4 +4,4 @@ version = 4
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cat2text"
|
name = "cat2text"
|
||||||
version = "0.1.3"
|
version = "0.1.2"
|
||||||
|
|
|
@ -1,14 +1,10 @@
|
||||||
[package]
|
[package]
|
||||||
name = "cat2text"
|
name = "cat2text"
|
||||||
version = "0.1.3"
|
version = "0.1.2"
|
||||||
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
|
||||||
|
|
|
@ -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,21 +91,20 @@ pub mod bytes {
|
||||||
/// Encodes from bytes into catspeak
|
/// 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 bytes = &[9, 1];
|
||||||
/// let base = 16;
|
/// let base = 10;
|
||||||
/// let char_length = char_length(base);
|
/// 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 {
|
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 +=
|
output += core::num_to_cat(*byte as u32, shortened_alphabet.clone(), char_length).as_str();
|
||||||
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();
|
||||||
|
@ -114,14 +113,14 @@ pub mod bytes {
|
||||||
/// Decodes catspeak into 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 text = "mreow mrrp meow mrrp".to_string();
|
||||||
/// let base = 16;
|
/// let base = 10;
|
||||||
/// let char_length = char_length(base);
|
/// let char_length = char_length(base);
|
||||||
///
|
///
|
||||||
/// assert_eq!(
|
/// assert_eq!(
|
||||||
/// vec![243, 10],
|
/// vec![21, 1],
|
||||||
/// decode(text, base, char_length)
|
/// decode(text, base, char_length)
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -139,3 +138,4 @@ pub mod bytes {
|
||||||
return output.into();
|
return output.into();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// ...
|
24
src/base4.rs
24
src/base4.rs
|
@ -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::anybase;
|
|
||||||
use crate::core;
|
use crate::core;
|
||||||
|
use crate::anybase;
|
||||||
|
|
||||||
/// 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,6 +9,12 @@ 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
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -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()))
|
/// 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, core::char_length(4));
|
return anybase::encode(text.as_ref().to_string(), 4, char_length());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decodes base 4 catspeak to english text
|
/// 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()));
|
/// 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, core::char_length(4));
|
return anybase::decode(text, 4, char_length())
|
||||||
}
|
}
|
||||||
|
|
||||||
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 crate::core::bytes::char_length;
|
use super::char_length;
|
||||||
/// Encodes from bytes into catspeak
|
/// Encodes from bytes into catspeak
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use cat2text::base4::bytes::encode;
|
/// 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 {
|
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
|
/// Decodes catspeak into bytes
|
||||||
|
@ -51,9 +57,9 @@ pub mod bytes {
|
||||||
/// ```
|
/// ```
|
||||||
/// use cat2text::base4::bytes::decode;
|
/// 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> {
|
pub fn decode(text: String) -> Vec<u8> {
|
||||||
anybase::bytes::decode(text, 4, char_length(4))
|
anybase::bytes::decode(text, 4, char_length())
|
||||||
}
|
}
|
||||||
}
|
}
|
59
src/core.rs
59
src/core.rs
|
@ -3,10 +3,10 @@
|
||||||
/// Converts a [`u32`] to catspeak
|
/// Converts a [`u32`] to catspeak
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use cat2text::core::{num_to_cat, char_length};
|
/// use cat2text::core::num_to_cat;
|
||||||
/// use cat2text::base4::alphabet;
|
/// 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 {
|
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, bytes::char_length};
|
/// use cat2text::core::cat_to_num;
|
||||||
/// use cat2text::base4::alphabet;
|
/// 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 {
|
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,24 +81,21 @@ 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
|
output = output.into_iter().map(|item| item.trim().to_string()).collect();
|
||||||
.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())
|
||||||
|
@ -106,12 +103,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 {
|
||||||
|
@ -119,10 +116,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)
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -134,24 +131,4 @@ 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;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue