Compare commits
1 commit
aa903203cf
...
4f9cb199b1
Author | SHA1 | Date | |
---|---|---|---|
|
4f9cb199b1 |
5 changed files with 13 additions and 70 deletions
|
@ -19,7 +19,7 @@ pub fn char_length(base: u32) -> u32 {
|
||||||
for i in 1..base + 1 {
|
for i in 1..base + 1 {
|
||||||
let num = base.pow(i);
|
let num = base.pow(i);
|
||||||
if num > 26 {
|
if num > 26 {
|
||||||
return i;
|
return num;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return u32::MAX;
|
return u32::MAX;
|
||||||
|
@ -70,41 +70,13 @@ pub fn encode(text: String, base: u32, char_length: u32) -> String {
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decodes catspeak into text using any base up to [`max_base()`]
|
|
||||||
///
|
|
||||||
/// `char_length` is set manually, but the minimum can be generated using [`char_length()`]
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use cat2text::anybase;
|
|
||||||
/// let text = "meow mewo; mrrp mreow mrrp nyaaaa~ mreow mreow meow nyaaaa~; meow mrow meow mrrp mreow meow mrrp mewo".to_string();
|
|
||||||
/// let base = 10;
|
|
||||||
/// let char_length = anybase::char_length(base);
|
|
||||||
|
|
||||||
/// assert_eq!("i love cats", anybase::decode(text, base, char_length));
|
#[test]
|
||||||
/// ```
|
fn test_anybase_encode() {
|
||||||
pub fn decode(text: String, base: u32, char_length: u32) -> String {
|
use crate::anybase;
|
||||||
let catspeak_words: Vec<String> = text.split("; ").map(|item| item.to_string()).collect();
|
let text = "i love cats".to_string();
|
||||||
let mut output: String = String::new();
|
let base = 10;
|
||||||
let mut shortened_alphabet = alphabet();
|
let char_length = anybase::char_length(base);
|
||||||
shortened_alphabet.truncate(base as usize);
|
|
||||||
for engl_word in catspeak_words {
|
assert_eq!("", anybase::encode(text, base, char_length));
|
||||||
let mut word = String::new();
|
|
||||||
for engl_letter in core::split_every_x(engl_word, char_length as usize) {
|
|
||||||
let char_num = core::cat_to_num(
|
|
||||||
engl_letter
|
|
||||||
.split(" ")
|
|
||||||
.map(|item| item.to_string())
|
|
||||||
.collect(),
|
|
||||||
shortened_alphabet.clone(),
|
|
||||||
char_length,
|
|
||||||
);
|
|
||||||
word += String::from_utf8(vec![(char_num + 96) as u8])
|
|
||||||
.unwrap()
|
|
||||||
.as_str();
|
|
||||||
}
|
|
||||||
word += " ";
|
|
||||||
output += word.as_str();
|
|
||||||
}
|
|
||||||
return output.trim().to_string();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -59,11 +59,12 @@ pub fn encode(text: String) -> String {
|
||||||
pub fn decode(text: String) -> String {
|
pub fn decode(text: String) -> String {
|
||||||
let catspeak_words: Vec<String> = text
|
let catspeak_words: Vec<String> = text
|
||||||
.split("; ")
|
.split("; ")
|
||||||
|
.into_iter()
|
||||||
.map(|item| item.to_string())
|
.map(|item| item.to_string())
|
||||||
.collect();
|
.collect();
|
||||||
let mut output: String = String::new();
|
let mut output: String = String::new();
|
||||||
for engl_word in catspeak_words {
|
for engl_word in catspeak_words {
|
||||||
let mut word = String::new();
|
let mut word = "".to_string();
|
||||||
for engl_letter in core::split_every_x(engl_word, 3) {
|
for engl_letter in core::split_every_x(engl_word, 3) {
|
||||||
let char_num = core::cat_to_num(
|
let char_num = core::cat_to_num(
|
||||||
engl_letter
|
engl_letter
|
||||||
|
|
|
@ -13,6 +13,7 @@ pub fn num_to_cat(num: u32, alphabet: Vec<String>, char_length: u32) -> String {
|
||||||
let base: u32 = alphabet.len() as u32;
|
let base: u32 = alphabet.len() as u32;
|
||||||
|
|
||||||
// base*n*-ifying logic
|
// base*n*-ifying logic
|
||||||
|
// FIXME: With base 10 this loops 100 times? see `anybase::test_anybase_encode()`
|
||||||
let mut nums: Vec<u32> = Vec::new();
|
let mut nums: Vec<u32> = Vec::new();
|
||||||
while (nums.len() as u32) < char_length {
|
while (nums.len() as u32) < char_length {
|
||||||
nums.push((num as u32) % base);
|
nums.push((num as u32) % base);
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
#![doc = include_str!("../README.md")]
|
#![doc = include_str!("../README.md")]
|
||||||
pub mod anybase;
|
//pub mod anybase;
|
||||||
pub mod base4;
|
pub mod base4;
|
||||||
pub mod core;
|
pub mod core;
|
||||||
pub mod bytes;
|
pub mod bytes;
|
||||||
mod tests;
|
|
||||||
|
|
30
src/tests.rs
30
src/tests.rs
|
@ -1,30 +0,0 @@
|
||||||
#[test]
|
|
||||||
fn test_anybase_base4() {
|
|
||||||
use crate::{anybase, base4};
|
|
||||||
let text = "i love cats".to_string();
|
|
||||||
|
|
||||||
let encoded = anybase::encode(text.clone(), 4, anybase::char_length(4));
|
|
||||||
assert_eq!(base4::encode(text), encoded);
|
|
||||||
|
|
||||||
let decoded = anybase::decode(encoded.clone(), 4, anybase::char_length(4));
|
|
||||||
assert_eq!(base4::decode(encoded), decoded);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_anybase_encode() {
|
|
||||||
use crate::anybase;
|
|
||||||
let text = "i love cats".to_string();
|
|
||||||
let base = 10;
|
|
||||||
let char_length = anybase::char_length(base);
|
|
||||||
|
|
||||||
assert_eq!("meow mewo; mrrp mreow mrrp nyaaaa~ mreow mreow meow nyaaaa~; meow mrow meow mrrp mreow meow mrrp mewo", anybase::encode(text, base, char_length));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_anybase_decode() {
|
|
||||||
use crate::anybase;
|
|
||||||
let text = "meow mewo; mrrp mreow mrrp nyaaaa~ mreow mreow meow nyaaaa~; meow mrow meow mrrp mreow meow mrrp mewo".to_string();
|
|
||||||
let base = 10;
|
|
||||||
let char_length = anybase::char_length(base);
|
|
||||||
assert_eq!("i love cats", anybase::decode(text, base, char_length));
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue