more functional and better readme
This commit is contained in:
parent
49d6049a76
commit
79dbcb1d3d
4 changed files with 43 additions and 27 deletions
22
README.md
22
README.md
|
@ -1,3 +1,25 @@
|
||||||
# Cat2Text-rs
|
# Cat2Text-rs
|
||||||
|
|
||||||
This is a port of [Cat2Text](https://github.com/askiiart/Cat2Text) to Rust, with extra functionality, better documentation, and support for using it as a library as well.
|
This is a port of [Cat2Text](https://github.com/askiiart/Cat2Text) to Rust, with extra functionality, better documentation, and support for using it as a library as well.
|
||||||
|
|
||||||
|
## Base4 Format
|
||||||
|
|
||||||
|
Using the original base4 format, it works like this. First off, each word is equal to a value in base 4:
|
||||||
|
|
||||||
|
| Value | Cat sound |
|
||||||
|
| ----- | --------- |
|
||||||
|
| 0 | meow |
|
||||||
|
| 1 | mrrp |
|
||||||
|
| 2 | mreow |
|
||||||
|
| 3 | mrow |
|
||||||
|
|
||||||
|
Then, the text is converted into lowercase
|
||||||
|
|
||||||
|
## Limitations
|
||||||
|
|
||||||
|
This currently only supports lowercase text in the latin alphabet without numbers or anything - however, using `cat2text::core`, you can convert anything to meows, as long as you can put it in integers - which, you can.
|
||||||
|
|
||||||
|
## TODO
|
||||||
|
|
||||||
|
- Add functionality for converting `Vec<u8>` to cat.
|
||||||
|
- Add more bases - adaptable base option?
|
||||||
|
|
19
src/base4.rs
19
src/base4.rs
|
@ -1,3 +1,5 @@
|
||||||
|
use crate::core;
|
||||||
|
|
||||||
pub fn alphabet() -> Vec<String> {
|
pub fn alphabet() -> Vec<String> {
|
||||||
return vec!["meow", "mrrp", "mreow", "mrow"]
|
return vec!["meow", "mrrp", "mreow", "mrow"]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -10,8 +12,7 @@ pub fn char_length() -> u32 {
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add uppercase support, maybe possible?
|
pub fn encode(text: String) -> String {
|
||||||
pub fn encode(text: String) {
|
|
||||||
// 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()
|
||||||
|
@ -24,6 +25,16 @@ pub fn encode(text: String) {
|
||||||
words_as_bytes.push(word.as_bytes().into_iter().map(|item| item - 96).collect());
|
words_as_bytes.push(word.as_bytes().into_iter().map(|item| item - 96).collect());
|
||||||
}
|
}
|
||||||
|
|
||||||
let translation: String = "".to_string();
|
let mut results: Vec<Vec<String>> = Vec::new();
|
||||||
for word in words_as_bytes {}
|
for i in 0..words_as_bytes.len() {
|
||||||
|
results.push(Vec::new());
|
||||||
|
for j in 0..words_as_bytes[i].len() {
|
||||||
|
results[i].push(core::num_to_cat(words_as_bytes[i][j] as u32, alphabet(), char_length()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let results: Vec<String> = results.into_iter().map(|item| item.join(" ")).collect();
|
||||||
|
let results = results.join("; ");
|
||||||
|
return results;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
16
src/core.rs
16
src/core.rs
|
@ -1,9 +1,5 @@
|
||||||
//! Handles conversion one word at a time for any base
|
//! Handles conversion one word at a time for any base
|
||||||
// based off this SO answer: https://stackoverflow.com/a/1119769
|
// based off this SO answer: https://stackoverflow.com/a/1119769
|
||||||
use std::ops::Index;
|
|
||||||
|
|
||||||
use crate::base4::alphabet;
|
|
||||||
|
|
||||||
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();
|
||||||
let base: u32 = alphabet.len() as u32;
|
let base: u32 = alphabet.len() as u32;
|
||||||
|
@ -16,16 +12,12 @@ pub fn num_to_cat(num: u32, alphabet: Vec<String>, char_length: u32) -> String {
|
||||||
}
|
}
|
||||||
nums.reverse();
|
nums.reverse();
|
||||||
|
|
||||||
let mut result = "".to_string();
|
let mut result: Vec<String> = Vec::new();
|
||||||
for i in nums.clone() {
|
for item in nums {
|
||||||
result += alphabet[i as usize].as_str();
|
result.push(alphabet[item as usize].clone());
|
||||||
// put a space in between if it's not the last one
|
|
||||||
if i != nums.len() as u32 - 1 {
|
|
||||||
result += " "
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result.join(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts catspeak to a [`u32`]
|
/// Converts catspeak to a [`u32`]
|
||||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -29,17 +29,8 @@ fn main() {
|
||||||
stdin.read_line(&mut input).unwrap();
|
stdin.read_line(&mut input).unwrap();
|
||||||
println!(
|
println!(
|
||||||
"{}",
|
"{}",
|
||||||
core::cat_to_num(
|
base4::encode(input.trim().to_string())
|
||||||
input
|
|
||||||
.trim()
|
|
||||||
.to_string()
|
|
||||||
.split(" ")
|
|
||||||
.into_iter()
|
|
||||||
.map(|item| item.to_string())
|
|
||||||
.collect(),
|
|
||||||
base4::alphabet(),
|
|
||||||
base4::char_length()
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
println!("Invalid input, exiting...");
|
println!("Invalid input, exiting...");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue