improve docs, bug fixes, add bytes functionality
This commit is contained in:
parent
021f9cbbfa
commit
e67a678120
5 changed files with 63 additions and 6 deletions
19
README.md
19
README.md
|
@ -37,13 +37,30 @@ Separating out the letters and words:
|
|||
> ]
|
||||
> ]
|
||||
|
||||
## Usage
|
||||
|
||||
To use the library, just import the relevant functions, and run it like this:
|
||||
|
||||
```rust
|
||||
use cat2text::base4::{encode, decode};
|
||||
|
||||
let encoded = encode("i love cats".to_string());
|
||||
assert_eq!(encoded, "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");
|
||||
let decoded = decode(encoded);
|
||||
assert_eq!(decoded, "i love cats");
|
||||
```
|
||||
|
||||
### Binary usage
|
||||
|
||||
TODO
|
||||
|
||||
## 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 catspeak.
|
||||
- Add more bases - any base option?
|
||||
- Make `core::{cat_to_num, num_to_cat}` usage consistent with each other
|
||||
- Improve CLI
|
||||
- Add error handling
|
||||
|
|
|
@ -17,7 +17,7 @@ pub fn char_length() -> u32 {
|
|||
/// ```
|
||||
/// 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().))
|
||||
/// 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
|
||||
|
|
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;
|
||||
}
|
|
@ -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;
|
||||
|
||||
// base*n*-ifying logic
|
||||
// FIXME: With base 10 this loops 100 times? see `anybase::test_anybase_encode()`
|
||||
let mut nums: Vec<u32> = Vec::new();
|
||||
while (nums.len() as u32) < char_length {
|
||||
nums.push((num as u32) % base);
|
||||
|
@ -74,9 +75,10 @@ pub fn split_every_x(text: String, x: usize) -> Vec<String> {
|
|||
output.push(String::new())
|
||||
}
|
||||
output[i / x] += tmp[i].as_str();
|
||||
if i % x != 2 {
|
||||
output[i / x] += " ";
|
||||
}
|
||||
output[i / x] += " ";
|
||||
}
|
||||
|
||||
// trim everything before sending it back
|
||||
output = output.into_iter().map(|item| item.trim().to_string()).collect();
|
||||
return output;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#![doc = include_str!("../README.md")]
|
||||
pub mod anybase;
|
||||
//pub mod anybase;
|
||||
pub mod base4;
|
||||
pub mod core;
|
||||
pub mod bytes;
|
||||
|
|
Loading…
Add table
Reference in a new issue