Compare commits
7 commits
4ceb59d052
...
a9d81f015e
Author | SHA1 | Date | |
---|---|---|---|
|
a9d81f015e | ||
|
9fac0f3667 | ||
|
7111192081 | ||
|
5e315db651 | ||
|
4fc0cbc078 | ||
|
ec17c263d0 | ||
|
189ceefbad |
5 changed files with 67 additions and 46 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -4,4 +4,4 @@ version = 4
|
|||
|
||||
[[package]]
|
||||
name = "cat2text"
|
||||
version = "0.1.2"
|
||||
version = "0.1.3"
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
[package]
|
||||
name = "cat2text"
|
||||
version = "0.1.2"
|
||||
version = "0.1.3"
|
||||
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
|
||||
|
|
|
@ -91,20 +91,21 @@ pub mod bytes {
|
|||
/// 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 base = 10;
|
||||
/// let bytes = &[243, 10];
|
||||
/// let base = 16;
|
||||
/// 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 {
|
||||
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();
|
||||
|
@ -113,14 +114,14 @@ pub mod 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 base = 10;
|
||||
/// let text = "mrow~ mrow meow purrrr".to_string();
|
||||
/// let base = 16;
|
||||
/// let char_length = char_length(base);
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// vec![21, 1],
|
||||
/// vec![243, 10],
|
||||
/// decode(text, base, char_length)
|
||||
/// );
|
||||
/// ```
|
||||
|
@ -138,4 +139,3 @@ pub mod bytes {
|
|||
return output.into();
|
||||
}
|
||||
}
|
||||
// ...
|
22
src/base4.rs
22
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`])
|
||||
use crate::core;
|
||||
use crate::anybase;
|
||||
use crate::core;
|
||||
|
||||
/// Returns the alphabet used by `cat2text::base4`
|
||||
pub fn alphabet() -> Vec<String> {
|
||||
|
@ -9,12 +9,6 @@ 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
|
||||
///
|
||||
/// ```
|
||||
|
@ -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()))
|
||||
/// ```
|
||||
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
|
||||
|
@ -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()));
|
||||
/// ```
|
||||
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 {
|
||||
//! This handles encoding and decoding bytes to/from catspeak
|
||||
use crate::anybase;
|
||||
use super::char_length;
|
||||
use crate::core::bytes::char_length;
|
||||
/// Encodes from bytes into catspeak
|
||||
///
|
||||
/// ```
|
||||
/// 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 {
|
||||
anybase::bytes::encode(bytes, 4, char_length())
|
||||
anybase::bytes::encode(bytes, 4, char_length(4))
|
||||
}
|
||||
|
||||
/// Decodes catspeak into bytes
|
||||
|
@ -57,9 +51,9 @@ pub mod bytes {
|
|||
/// ```
|
||||
/// 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> {
|
||||
anybase::bytes::decode(text, 4, char_length())
|
||||
anybase::bytes::decode(text, 4, char_length(4))
|
||||
}
|
||||
}
|
41
src/core.rs
41
src/core.rs
|
@ -3,10 +3,10 @@
|
|||
/// Converts a [`u32`] to catspeak
|
||||
///
|
||||
/// ```
|
||||
/// use cat2text::core::num_to_cat;
|
||||
/// use cat2text::base4::{alphabet, char_length};
|
||||
/// use cat2text::core::{num_to_cat, 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 {
|
||||
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;
|
||||
/// use cat2text::base4::{alphabet, char_length};
|
||||
/// use cat2text::core::{cat_to_num, bytes::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 {
|
||||
let mut nums: Vec<u32> = Vec::new();
|
||||
|
@ -81,7 +81,10 @@ 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;
|
||||
}
|
||||
|
||||
|
@ -95,7 +98,7 @@ pub(crate) fn split_every_x(text: String, x: u32) -> Vec<String> {
|
|||
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())
|
||||
|
@ -132,3 +135,23 @@ 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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue