use asref<str> instead
This commit is contained in:
parent
d65ae43d81
commit
a4bcaf6103
4 changed files with 10 additions and 11 deletions
|
@ -100,5 +100,4 @@ This currently only supports lowercase text in the latin alphabet, and byte arra
|
|||
## TODO
|
||||
|
||||
- Add error handling
|
||||
- Do `AsRef` stuff
|
||||
- Optimize code
|
||||
|
|
|
@ -14,12 +14,12 @@ use crate::core;
|
|||
///
|
||||
/// assert_eq!("meow mewo; mrrp mreow mrrp nyaaaa~ mreow mreow meow nyaaaa~; meow mrow meow mrrp mreow meow mrrp mewo", encode(text, base, char_length));
|
||||
/// ```
|
||||
pub fn encode(text: String, base: u32, char_length: u32) -> String {
|
||||
pub fn encode(text: impl AsRef<str>, base: u32, char_length: u32) -> String {
|
||||
let mut shortened_alphabet = core::alphabet();
|
||||
shortened_alphabet.truncate(base as usize);
|
||||
|
||||
// makes it lowercase and split by spaces
|
||||
let words: Vec<String> = text
|
||||
let words: Vec<String> = text.as_ref()
|
||||
.to_ascii_lowercase()
|
||||
.split(" ")
|
||||
.map(|item| return item.to_string())
|
||||
|
@ -60,8 +60,8 @@ pub fn encode(text: String, base: u32, char_length: u32) -> String {
|
|||
|
||||
/// assert_eq!("i love cats", decode(text, base, char_length));
|
||||
/// ```
|
||||
pub fn decode(text: String, base: u32, char_length: u32) -> String {
|
||||
let catspeak_words: Vec<String> = text.split("; ").map(|item| item.to_string()).collect();
|
||||
pub fn decode(text: impl AsRef<str>, base: u32, char_length: u32) -> String {
|
||||
let catspeak_words: Vec<String> = text.as_ref().split("; ").map(|item| item.to_string()).collect();
|
||||
let mut output: String = String::new();
|
||||
let mut shortened_alphabet = core::alphabet();
|
||||
shortened_alphabet.truncate(base as usize);
|
||||
|
@ -125,11 +125,11 @@ pub mod bytes {
|
|||
/// decode(text, base, char_length)
|
||||
/// );
|
||||
/// ```
|
||||
pub fn decode(text: String, base: u32, char_length: u32) -> Vec<u8> {
|
||||
pub fn decode(text: impl AsRef<str>, base: u32, char_length: u32) -> Vec<u8> {
|
||||
let mut output: Vec<u8> = Vec::new();
|
||||
let mut shortened_alphabet = core::alphabet();
|
||||
shortened_alphabet.truncate(base as usize);
|
||||
for byte in core::split_every_x(text.clone(), char_length) {
|
||||
for byte in core::split_every_x(text.as_ref(), char_length) {
|
||||
output.push(core::cat_to_num(
|
||||
byte.split(" ").map(|item| item.to_string()).collect(),
|
||||
shortened_alphabet.clone(),
|
||||
|
|
|
@ -27,7 +27,7 @@ pub fn encode(text: impl AsRef<str>) -> String {
|
|||
///
|
||||
/// assert_eq!("i love cats", decode("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".to_string()));
|
||||
/// ```
|
||||
pub fn decode(text: String) -> String {
|
||||
pub fn decode(text: impl AsRef<str>) -> String {
|
||||
return anybase::decode(text, 4, core::char_length(4));
|
||||
}
|
||||
|
||||
|
@ -53,7 +53,7 @@ pub mod bytes {
|
|||
///
|
||||
/// assert_eq!(vec![253, 1], decode("mrow mrow mrow mrrp meow meow meow mrrp".to_string()));
|
||||
/// ```
|
||||
pub fn decode(text: String) -> Vec<u8> {
|
||||
pub fn decode(text: impl AsRef<str>) -> Vec<u8> {
|
||||
anybase::bytes::decode(text, 4, char_length(4))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,10 +67,10 @@ pub fn cat_to_num(text: Vec<String>, alphabet: Vec<String>, char_length: u32) ->
|
|||
///
|
||||
/// assert_eq!(vec!["meow meow mrrp".to_string(), "meow mreow mrrp".to_string()], split_every_x("meow meow mrrp meow mreow mrrp".to_string(), 3));
|
||||
/// ```
|
||||
pub(crate) fn split_every_x(text: String, x: u32) -> Vec<String> {
|
||||
pub(crate) fn split_every_x(text: impl AsRef<str>, x: u32) -> Vec<String> {
|
||||
let x = x as usize;
|
||||
let delim = " ";
|
||||
let tmp: Vec<String> = text.split(delim).map(|item| item.to_string()).collect();
|
||||
let tmp: Vec<String> = text.as_ref().split(delim).map(|item| item.to_string()).collect();
|
||||
let mut output: Vec<String> = Vec::new();
|
||||
for i in 0..tmp.len() {
|
||||
if i % x == 0 {
|
||||
|
|
Loading…
Add table
Reference in a new issue