update docs and reorganize a bit
This commit is contained in:
parent
5c5f42316a
commit
3e996720af
2 changed files with 16 additions and 9 deletions
|
@ -12,6 +12,13 @@ pub fn char_length() -> u32 {
|
|||
return 3;
|
||||
}
|
||||
|
||||
/// Encodes english text into base 4 catspeak
|
||||
///
|
||||
/// ```
|
||||
/// use cat2text::base4::encode;
|
||||
///
|
||||
/// 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: String) -> String {
|
||||
// makes it lowercase and split by spaces
|
||||
let words: Vec<String> = text
|
||||
|
@ -58,7 +65,7 @@ pub fn decode(text: String) -> String {
|
|||
let mut output: String = String::new();
|
||||
for engl_word in catspeak_words {
|
||||
let mut word = "".to_string();
|
||||
for engl_letter in core::split_every_3(engl_word) {
|
||||
for engl_letter in core::split_every_x(engl_word, 3) {
|
||||
let char_num = core::cat_to_num(
|
||||
engl_letter
|
||||
.split(" ")
|
||||
|
|
16
src/core.rs
16
src/core.rs
|
@ -58,24 +58,24 @@ pub fn cat_to_num(text: Vec<String>, alphabet: Vec<String>, char_length: u32) ->
|
|||
return num;
|
||||
}
|
||||
|
||||
/// Splits a cat word into every 3 segments
|
||||
/// Splits a cat word into every x segments
|
||||
///
|
||||
/// ```
|
||||
/// use cat2text::core::split_every_3;
|
||||
/// use cat2text::core::split_every_x;
|
||||
///
|
||||
/// assert_eq!(vec!["meow meow mrrp".to_string(), "meow mreow mrrp".to_string(), "mreow meow mrrp".to_string()], split_every_3("meow meow mrrp meow mreow mrrp mreow meow mrrp".to_string()));
|
||||
/// assert_eq!(vec!["meow meow mrrp".to_string(), "meow mreow mrrp".to_string(), "mreow meow mrrp".to_string()], split_every_x("meow meow mrrp meow mreow mrrp mreow meow mrrp".to_string(), 3));
|
||||
/// ```
|
||||
pub fn split_every_3(text: String) -> Vec<String> {
|
||||
pub fn split_every_x(text: String, x: usize) -> Vec<String> {
|
||||
let delim = " ";
|
||||
let tmp: Vec<String> = text.split(delim).map(|item| item.to_string()).collect();
|
||||
let mut output: Vec<String> = Vec::new();
|
||||
for i in 0..tmp.len() {
|
||||
if i % 3 == 0 {
|
||||
if i % x == 0 {
|
||||
output.push(String::new())
|
||||
}
|
||||
output[i / 3] += tmp[i].as_str();
|
||||
if i % 3 != 2 {
|
||||
output[i / 3] += " ";
|
||||
output[i / x] += tmp[i].as_str();
|
||||
if i % x != 2 {
|
||||
output[i / x] += " ";
|
||||
}
|
||||
}
|
||||
return output;
|
||||
|
|
Loading…
Add table
Reference in a new issue