Compare commits
No commits in common. "f07d8186a8d7ac90fe7ddf6fc40876ecedde25d6" and "aa903203cf5520c0ea91552c3c0c8cc5c40dd876" have entirely different histories.
f07d8186a8
...
aa903203cf
4 changed files with 39 additions and 46 deletions
|
@ -4,7 +4,6 @@ version = "0.1.2"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "GPL-3.0-or-later"
|
license = "GPL-3.0-or-later"
|
||||||
repository = "https://git.askiiart.net/askiiart/cat2text-rs"
|
repository = "https://git.askiiart.net/askiiart/cat2text-rs"
|
||||||
authors = ["askiiart <mail@askiiart.net>"]
|
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
|
|
46
src/base4.rs
46
src/base4.rs
|
@ -1,5 +1,3 @@
|
||||||
use std::ops::Deref;
|
|
||||||
|
|
||||||
use crate::core;
|
use crate::core;
|
||||||
|
|
||||||
pub fn alphabet() -> Vec<String> {
|
pub fn alphabet() -> Vec<String> {
|
||||||
|
@ -21,8 +19,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: String) -> String {
|
||||||
let text = text.as_ref();
|
|
||||||
// makes it lowercase and split by spaces
|
// makes it lowercase and split by spaces
|
||||||
let words: Vec<String> = text
|
let words: Vec<String> = text
|
||||||
.to_ascii_lowercase()
|
.to_ascii_lowercase()
|
||||||
|
@ -86,44 +83,3 @@ pub fn decode(text: String) -> String {
|
||||||
|
|
||||||
return output.trim().to_string();
|
return output.trim().to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub mod bytes {
|
|
||||||
use crate::core;
|
|
||||||
use crate::base4::alphabet;
|
|
||||||
/// Encodes from bytes into catspeak
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use cat2text::base4::bytes::encode;
|
|
||||||
///
|
|
||||||
/// assert_eq!("meow meow mreow mrrp meow meow meow mrrp", encode(&[9, 1]));
|
|
||||||
/// ```
|
|
||||||
pub fn encode(bytes: impl AsRef<[u8]>) -> String {
|
|
||||||
let mut output = String::new();
|
|
||||||
for byte in bytes.as_ref() {
|
|
||||||
output += core::num_to_cat(*byte as u32, alphabet(), 4).as_str();
|
|
||||||
output += " ";
|
|
||||||
}
|
|
||||||
return output.trim().to_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Decodes catspeak into bytes
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use cat2text::base4::bytes::decode;
|
|
||||||
///
|
|
||||||
/// assert_eq!(vec![9, 1], decode("meow meow mreow mrrp meow meow meow mrrp".to_string()));
|
|
||||||
/// ```
|
|
||||||
pub fn decode(text: String) -> Vec<u8> {
|
|
||||||
let mut output: Vec<u8> = Vec::new();
|
|
||||||
for byte in core::split_every_x(text.clone(), 4) {
|
|
||||||
output.push(core::cat_to_num(
|
|
||||||
byte.split(" ").map(|item| item.to_string()).collect(),
|
|
||||||
alphabet(),
|
|
||||||
4,
|
|
||||||
) as u8);
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
37
src/bytes.rs
Normal file
37
src/bytes.rs
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
use crate::base4::alphabet;
|
||||||
|
use crate::core;
|
||||||
|
|
||||||
|
/// Encodes from bytes into catspeak
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use cat2text::bytes::from_bytes;
|
||||||
|
///
|
||||||
|
/// assert_eq!("meow meow mreow mrrp meow meow meow mrrp", from_bytes(vec![9, 1]));
|
||||||
|
/// ```
|
||||||
|
pub fn from_bytes(bytes: Vec<u8>) -> String {
|
||||||
|
let mut output = "".to_string();
|
||||||
|
for byte in bytes {
|
||||||
|
output += core::num_to_cat(byte as u32, alphabet(), 4).as_str();
|
||||||
|
output += " ";
|
||||||
|
}
|
||||||
|
return output.trim().to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Decodes catspeak into bytes
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use cat2text::bytes::to_bytes;
|
||||||
|
///
|
||||||
|
/// assert_eq!(vec![9, 1], to_bytes("meow meow mreow mrrp meow meow meow mrrp".to_string()));
|
||||||
|
/// ```
|
||||||
|
pub fn to_bytes(text: String) -> Vec<u8> {
|
||||||
|
let mut output: Vec<u8> = Vec::new();
|
||||||
|
for byte in core::split_every_x(text.clone(), 4) {
|
||||||
|
output.push(core::cat_to_num(
|
||||||
|
byte.split(" ").map(|item| item.to_string()).collect(),
|
||||||
|
alphabet(),
|
||||||
|
4,
|
||||||
|
) as u8);
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
|
@ -2,4 +2,5 @@
|
||||||
pub mod anybase;
|
pub mod anybase;
|
||||||
pub mod base4;
|
pub mod base4;
|
||||||
pub mod core;
|
pub mod core;
|
||||||
|
pub mod bytes;
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue