Move translate_word.rs to byte_string.rs

main
John Zacarias Jekel 1 year ago
parent 92d817489d
commit 47cbf3aba0
  1. 50
      src/byte_string.rs
  2. 910
      src/translate_words.rs

@ -1,9 +1,9 @@
/* NAME//TODO
/* byte_string.rs
* By: John Jekel
* Copyright (C) 2022 John Jekel
* See the LICENSE file at the root of the project for licensing info.
*
* TODO description
* Translation functions operating on &[u8] and Vec::<u8> (higher efficiency, but less user-friendly)
*
*/
@ -250,19 +250,19 @@ fn is_vowel(letter: u8) -> bool {
}
//Returns whether a letter is y or not.
pub(crate) fn is_y(letter: u8) -> bool {
fn is_y(letter: u8) -> bool {
return letter.to_ascii_lowercase() == b'y';
}
//Returns whether an entire word is upper case or not.
pub(crate) fn word_is_uppercase(english_word_bytes: &[u8]) -> bool {
fn word_is_uppercase(english_word_bytes: &[u8]) -> bool {
//Asume length is non-zero
//Heuristic: If the last letter of the word is uppercase, likely the whole word is uppercase
return (english_word_bytes[english_word_bytes.len() - 1] as char).is_ascii_uppercase();
}
//Clones each element of a slice and push()es it to a vector
pub(crate) fn push_slice_to_vector<T: Clone>(vec: &mut Vec<T>, slice: &[T]) {
fn push_slice_to_vector<T: Clone>(vec: &mut Vec<T>, slice: &[T]) {
for element in slice {
vec.push(element.clone());
}
@ -314,6 +314,26 @@ mod tests {
}
}
fn translate_word_with_style(english_word: &str, suffix_lower: &str, special_case_suffix_lower: &str) -> String {
let mut suffix_upper = String::new();
for letter in suffix_lower.chars() {
suffix_upper.push(letter.to_ascii_uppercase());
}
let mut special_case_suffix_upper = String::new();
for letter in special_case_suffix_lower.chars() {
special_case_suffix_upper.push(letter.to_ascii_uppercase());
}
let mut pig_latin_word = Vec::<u8>::new();
let mut starting_consonants_buffer = Vec::<u8>::new();
translate_word_with_style_reuse_buffers (
english_word.as_bytes(),
suffix_lower.as_bytes(), special_case_suffix_lower.as_bytes(), &suffix_upper.as_bytes(), &special_case_suffix_upper.as_bytes(),
&mut pig_latin_word, &mut starting_consonants_buffer
);
return std::str::from_utf8(pig_latin_word.as_slice()).unwrap().to_string();
}
#[test]
fn test_is_vowel() {
for letter in b"aeiouAEIOU".iter() {
@ -358,26 +378,6 @@ mod tests {
assert!(!word_is_uppercase(b"Sussus"));
assert!(!word_is_uppercase(b"Amogus"));
}
fn translate_word_with_style(english_word: &str, suffix_lower: &str, special_case_suffix_lower: &str) -> String {
let mut suffix_upper = String::with_capacity(suffix_lower.len());
for letter in suffix_lower.chars() {
suffix_upper.push(letter.to_ascii_uppercase());
}
let mut special_case_suffix_upper = String::with_capacity(special_case_suffix_lower.len());
for letter in special_case_suffix_lower.chars() {
special_case_suffix_upper.push(letter.to_ascii_uppercase());
}
let mut pig_latin_word = Vec::<u8>::new();
let mut starting_consonants_buffer = Vec::<u8>::new();
translate_word_with_style_reuse_buffers (
english_word.as_bytes(),
suffix_lower.as_bytes(), special_case_suffix_lower.as_bytes(), &suffix_upper.as_bytes(), &special_case_suffix_upper.as_bytes(),
&mut pig_latin_word, &mut starting_consonants_buffer
);
return std::str::from_utf8(pig_latin_word.as_slice()).unwrap().to_string();
}
}
/* Benches */

@ -1,910 +0,0 @@
/* translate_words.rs
* Copyright (C) 2022 John Jekel
* See the LICENSE file at the root of the project for licensing info.
*
* Contains functions to translate individual words (used by translate_strings.rs)
*
*/
/* Imports */
use crate::helpers::{is_vowel, is_vowel_ascii, is_y, word_is_uppercase, word_is_uppercase_ascii, push_slice_to_vector};
/* Functions */
pub(crate) fn translate_word_with_style_reuse_buffers (
english_word: &str,//Assumes this word is not empty
suffix_lower: &str, special_case_suffix_lower: &str, suffix_upper: &str, special_case_suffix_upper: &str,
buffer_to_append_to: &mut String, starting_consonants: &mut String
) {
if english_word.len() == 1 {
buffer_to_append_to.push_str(english_word);
buffer_to_append_to.push_str(special_case_suffix_lower);
return;
}
let mut iterator = english_word.chars();
//Check the first letter
let first_letter: char = iterator.next().unwrap();
//Check if the word is uppercase
let word_uppercase = word_is_uppercase(&english_word);
//As a herustic, we consider Y to be a vowel when it is not at the start of the word
let first_letter_was_vowel: bool = is_vowel(first_letter);//Not including y
//Clear the starting_consonants buffer we were given
starting_consonants.truncate(0);
if first_letter_was_vowel {
buffer_to_append_to.push(first_letter);
} else {
let first_char_was_upper = first_letter.is_ascii_uppercase();
starting_consonants.push(if word_uppercase { first_letter } else { first_letter.to_ascii_lowercase() });
//Grab all of the starting consonants, and push the first vowel we enounter to buffer_to_append_to
loop {
match iterator.next() {
None => { break; },//The word has no vowels, but it is a herustic to pass it on so that ex. the acroynm binary code decimal or bcd becomes bcdway, etc.
Some(character) => {
if is_vowel(character) || is_y(character) {//As a herustic, we consider Y to be a vowel when it is not at the start of the word
//The vowel is the first letter of the word; we want it match the capitalization of the first letter of the original word
if first_char_was_upper {
buffer_to_append_to.push(character.to_ascii_uppercase());
} else {
buffer_to_append_to.push(character.to_ascii_lowercase());
}
break;
} else {
starting_consonants.push(character);
}
}
}
}
}
//Copy all of the remaining letters up to the end of the word
loop {
match iterator.next() {
None => { break; },//End of the word
Some(character) => { buffer_to_append_to.push(character); }
}
}
//Copy starting consonants and add the suffix, or add the special_case_suffix depending on the circumstances
if first_letter_was_vowel {
if word_uppercase {
buffer_to_append_to.push_str(special_case_suffix_upper);
} else {
buffer_to_append_to.push_str(special_case_suffix_lower);
}
} else {
buffer_to_append_to.push_str(&starting_consonants);
if word_uppercase {
buffer_to_append_to.push_str(suffix_upper);
} else {
buffer_to_append_to.push_str(suffix_lower);
}
}
}
#[cfg(feature = "nightly-features-generics")]
pub(crate) fn translate_word_with_style_reuse_buffers_generic <
const SUFFIX_LOWER: &'static str, const SPECIAL_CASE_SUFFIX_LOWER: &'static str,
const SUFFIX_UPPER: &'static str, const SPECIAL_CASE_SUFFIX_UPPER: &'static str
> (
english_word: &str,//Assumes this word is not empty
buffer_to_append_to: &mut String, starting_consonants: &mut String
) {
if english_word.len() == 1 {
buffer_to_append_to.push_str(english_word);
buffer_to_append_to.push_str(SPECIAL_CASE_SUFFIX_LOWER);
return;
}
let mut iterator = english_word.chars();
//Check the first letter
let first_letter: char = iterator.next().unwrap();
//Check if the word is uppercase
let word_uppercase = word_is_uppercase(&english_word);
//As a herustic, we consider Y to be a vowel when it is not at the start of the word
let first_letter_was_vowel: bool = is_vowel(first_letter);//Not including y
//Clear the starting_consonants buffer we were given
starting_consonants.truncate(0);
if first_letter_was_vowel {
buffer_to_append_to.push(first_letter);
} else {
let first_char_was_upper = first_letter.is_ascii_uppercase();
starting_consonants.push(if word_uppercase { first_letter } else { first_letter.to_ascii_lowercase() });
//Grab all of the starting consonants, and push the first vowel we enounter to buffer_to_append_to
loop {
match iterator.next() {
None => { break; },//The word has no vowels, but it is a herustic to pass it on so that ex. the acroynm binary code decimal or bcd becomes bcdway, etc.
Some(character) => {
if is_vowel(character) || is_y(character) {//As a herustic, we consider Y to be a vowel when it is not at the start of the word
//The vowel is the first letter of the word; we want it match the capitalization of the first letter of the original word
if first_char_was_upper {
buffer_to_append_to.push(character.to_ascii_uppercase());
} else {
buffer_to_append_to.push(character.to_ascii_lowercase());
}
break;
} else {
starting_consonants.push(character);
}
}
}
}
}
//Copy all of the remaining letters up to the end of the word
loop {
match iterator.next() {
None => { break; },//End of the word
Some(character) => { buffer_to_append_to.push(character); }
}
}
//Copy starting consonants and add the suffix, or add the special_case_suffix depending on the circumstances
if first_letter_was_vowel {
if word_uppercase {
buffer_to_append_to.push_str(SPECIAL_CASE_SUFFIX_UPPER);
} else {
buffer_to_append_to.push_str(SPECIAL_CASE_SUFFIX_LOWER);
}
} else {
buffer_to_append_to.push_str(&starting_consonants);
if word_uppercase {
buffer_to_append_to.push_str(SUFFIX_UPPER);
} else {
buffer_to_append_to.push_str(SUFFIX_LOWER);
}
}
}
pub(crate) fn translate_word_with_style_reuse_buffers_ascii (
english_word: &[u8],//Assumes this word is not empty
suffix_lower: &[u8], special_case_suffix_lower: &[u8], suffix_upper: &[u8], special_case_suffix_upper: &[u8],
buffer_to_append_to: &mut Vec<u8>, starting_consonants: &mut Vec<u8>
) {
if english_word.len() == 1 {
push_slice_to_vector(buffer_to_append_to, english_word);
push_slice_to_vector(buffer_to_append_to, special_case_suffix_lower);
return;
}
//TODO more ascii optimizations
//Set the starting index (the first character is assumed to exist and is accessed directly in several spots)
let mut index = 1;
//Check if the word is uppercase
let word_uppercase = word_is_uppercase_ascii(english_word);
//As a herustic, we consider Y to be a vowel when it is not at the start of the word
let first_letter_was_vowel: bool = is_vowel_ascii(english_word[0]);//Not including y
//Clear the starting_consonants buffer we were given
starting_consonants.truncate(0);
if first_letter_was_vowel {
buffer_to_append_to.push(english_word[0]);
} else {
let first_char_was_upper = (english_word[0] as char).is_ascii_uppercase();
starting_consonants.push(if word_uppercase { english_word[0] } else { (english_word[0] as char).to_ascii_lowercase() as u8 });
//Grab all of the starting consonants, and push the first vowel we enounter to buffer_to_append_to
while index < english_word.len() {
let character: char = english_word[index] as char;
if is_vowel(character) || is_y(character) {//As a herustic, we consider Y to be a vowel when it is not at the start of the word
//The vowel is the first letter of the word; we want it match the capitalization of the first letter of the original word
if first_char_was_upper {
buffer_to_append_to.push(character.to_ascii_uppercase() as u8);
} else {
buffer_to_append_to.push(character.to_ascii_lowercase() as u8);
}
index += 1;
break;
} else {
starting_consonants.push(character as u8);
index += 1;
}
}
}
//Copy all of the remaining letters up to the end of the word
while index < english_word.len() {
buffer_to_append_to.push(english_word[index]);
index += 1;
}
//Copy starting consonants and add the suffix, or add the special_case_suffix depending on the circumstances
if first_letter_was_vowel {
if word_uppercase {
push_slice_to_vector(buffer_to_append_to, special_case_suffix_upper);
} else {
push_slice_to_vector(buffer_to_append_to, special_case_suffix_lower);
}
} else {
push_slice_to_vector(buffer_to_append_to, starting_consonants.as_slice());
if word_uppercase {
push_slice_to_vector(buffer_to_append_to, suffix_upper);
} else {
push_slice_to_vector(buffer_to_append_to, suffix_lower);
}
}
}
/*
//TODO this would be better (avoid static variables entirely) but we can't have some const generics depending on others
pub(crate) fn translate_word_with_style_reuse_buffers_ascii_generic <
const SUFFIX_LEN: usize, const SPECIAL_CASE_SUFFIX_LEN: usize,
const SUFFIX_LOWER: [u8; SUFFIX_LEN], const SPECIAL_CASE_SUFFIX_LOWER: [u8; SPECIAL_CASE_SUFFIX_LEN],
const SUFFIX_UPPER: [u8; SUFFIX_LEN], const SPECIAL_CASE_SUFFIX_UPPER: [u8; SPECIAL_CASE_SUFFIX_LEN]
> (
*/
#[cfg(feature = "nightly-features-generics")]
pub(crate) fn translate_word_with_style_reuse_buffers_ascii_generic <
const SUFFIX_LOWER: &'static [u8], const SPECIAL_CASE_SUFFIX_LOWER: &'static [u8],
const SUFFIX_UPPER: &'static [u8], const SPECIAL_CASE_SUFFIX_UPPER: &'static [u8]
> (
english_word: &[u8],//Assumes this word is not empty
buffer_to_append_to: &mut Vec<u8>, starting_consonants: &mut Vec<u8>
) {
if english_word.len() == 1 {
push_slice_to_vector(buffer_to_append_to, english_word);
push_slice_to_vector(buffer_to_append_to, SPECIAL_CASE_SUFFIX_LOWER);
return;
}
//TODO more ascii optimizations
//Set the starting index (the first character is assumed to exist and is accessed directly in several spots)
let mut index = 1;
//Check if the word is uppercase
let word_uppercase = word_is_uppercase_ascii(english_word);
//As a herustic, we consider Y to be a vowel when it is not at the start of the word
let first_letter_was_vowel: bool = is_vowel_ascii(english_word[0]);//Not including y
//Clear the starting_consonants buffer we were given
starting_consonants.truncate(0);
if first_letter_was_vowel {
buffer_to_append_to.push(english_word[0]);
} else {
let first_char_was_upper = (english_word[0] as char).is_ascii_uppercase();
starting_consonants.push(if word_uppercase { english_word[0] } else { (english_word[0] as char).to_ascii_lowercase() as u8 });
//Grab all of the starting consonants, and push the first vowel we enounter to buffer_to_append_to
while index < english_word.len() {
let character: char = english_word[index] as char;
if is_vowel(character) || is_y(character) {//As a herustic, we consider Y to be a vowel when it is not at the start of the word
//The vowel is the first letter of the word; we want it match the capitalization of the first letter of the original word
if first_char_was_upper {
buffer_to_append_to.push(character.to_ascii_uppercase() as u8);
} else {
buffer_to_append_to.push(character.to_ascii_lowercase() as u8);
}
index += 1;
break;
} else {
starting_consonants.push(character as u8);
index += 1;
}
}
}
//Copy all of the remaining letters up to the end of the word
while index < english_word.len() {
buffer_to_append_to.push(english_word[index]);
index += 1;
}
//Copy starting consonants and add the suffix, or add the special_case_suffix depending on the circumstances
if first_letter_was_vowel {
if word_uppercase {
push_slice_to_vector(buffer_to_append_to, SPECIAL_CASE_SUFFIX_UPPER);
} else {
push_slice_to_vector(buffer_to_append_to, SPECIAL_CASE_SUFFIX_LOWER);
}
} else {
push_slice_to_vector(buffer_to_append_to, starting_consonants.as_slice());
if word_uppercase {
push_slice_to_vector(buffer_to_append_to, SUFFIX_UPPER);
} else {
push_slice_to_vector(buffer_to_append_to, SUFFIX_LOWER);
}
}
}
/* Tests */
#[cfg(test)]
mod tests {
use super::*;
//TODO test uppercase words
#[test]
fn test_translate_word_with_style() {
let suffix_special_case_suffix_pairs = [
("ay", "way"), ("ay", "yay"), ("ay", "hay"), ("erb", "ferb"), ("ancy", "fancy"), ("orange", "porange"), ("anana", "banana"), ("atin", "latin"), ("ust", "rust")
];
for pair in suffix_special_case_suffix_pairs {
let suffix = pair.0;
let special_case_suffix = pair.1;
assert_eq!(translate_word_with_style("Hello", suffix, special_case_suffix), "Elloh".to_string() + suffix);
assert_eq!(translate_word_with_style("World", suffix, special_case_suffix), "Orldw".to_string() + suffix);
assert_eq!(translate_word_with_style("This", suffix, special_case_suffix), "Isth".to_string() + suffix);
assert_eq!(translate_word_with_style("is", suffix, special_case_suffix), "is".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style("a", suffix, special_case_suffix), "a".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style("test", suffix, special_case_suffix), "estt".to_string() + suffix);
assert_eq!(translate_word_with_style("of", suffix, special_case_suffix), "of".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style("the", suffix, special_case_suffix), "eth".to_string() + suffix);
assert_eq!(translate_word_with_style("function", suffix, special_case_suffix), "unctionf".to_string() + suffix);
assert_eq!(translate_word_with_style("translate", suffix, special_case_suffix), "anslatetr".to_string() + suffix);
assert_eq!(translate_word_with_style("word", suffix, special_case_suffix), "ordw".to_string() + suffix);
assert_eq!(translate_word_with_style("I", suffix, special_case_suffix), "I".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style("Love", suffix, special_case_suffix), "Ovel".to_string() + suffix);
assert_eq!(translate_word_with_style("Pig", suffix, special_case_suffix), "Igp".to_string() + suffix);
assert_eq!(translate_word_with_style("Latin", suffix, special_case_suffix), "Atinl".to_string() + suffix);
assert_eq!(translate_word_with_style("You", suffix, special_case_suffix), "Ouy".to_string() + suffix);//Y isn't a vowel here
assert_eq!(translate_word_with_style("should", suffix, special_case_suffix), "ouldsh".to_string() + suffix);
assert_eq!(translate_word_with_style("try", suffix, special_case_suffix), "ytr".to_string() + suffix);//Y is a vowel here
assert_eq!(translate_word_with_style("yougurt", suffix, special_case_suffix), "ougurty".to_string() + suffix);//Y isn't a vowel here
//assert_eq!(translate_word_with_style("it's", suffix, special_case_suffix), "it".to_string() + special_case_suffix + "'s");//Contraction
assert_eq!(translate_word_with_style("quite", suffix, special_case_suffix), "uiteq".to_string() + suffix);//Awful to pronounce, but correct
assert_eq!(translate_word_with_style("nice", suffix, special_case_suffix), "icen".to_string() + suffix);
}
}
#[test]
#[cfg(feature = "nightly-features-generics")]
fn test_translate_word_with_style_generic() {
//TODO test with other suffixes perhaps?
let suffix = "ay";
let special_case_suffix = "way";
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("Hello"), "Elloh".to_string() + suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("World"), "Orldw".to_string() + suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("This"), "Isth".to_string() + suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("is"), "is".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("a"), "a".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("test"), "estt".to_string() + suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("of"), "of".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("the"), "eth".to_string() + suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("function"), "unctionf".to_string() + suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("translate"), "anslatetr".to_string() + suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("word"), "ordw".to_string() + suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("I"), "I".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("Love"), "Ovel".to_string() + suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("Pig"), "Igp".to_string() + suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("Latin"), "Atinl".to_string() + suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("You"), "Ouy".to_string() + suffix);//Y isn't a vowel here
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("should"), "ouldsh".to_string() + suffix);
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("try"), "ytr".to_string() + suffix);//Y is a vowel here
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("yougurt"), "ougurty".to_string() + suffix);//Y isn't a vowel here
//assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("it's"), "it".to_string() + special_case_suffix + "'s");//Contraction
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("quite"), "uiteq".to_string() + suffix);//Awful to pronounce, but correct
assert_eq!(translate_word_with_style_generic::<"ay", "way", "AY", "WAY">("nice"), "icen".to_string() + suffix);
}
#[test]
fn test_translate_word_with_style_ascii() {
let suffix_special_case_suffix_pairs = [
("ay", "way"), ("ay", "yay"), ("ay", "hay"), ("erb", "ferb"), ("ancy", "fancy"), ("orange", "porange"), ("anana", "banana"), ("atin", "latin"), ("ust", "rust")
];
for pair in suffix_special_case_suffix_pairs {
let suffix = pair.0;
let special_case_suffix = pair.1;
assert_eq!(translate_word_with_style_ascii("Hello", suffix, special_case_suffix), "Elloh".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii("World", suffix, special_case_suffix), "Orldw".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii("This", suffix, special_case_suffix), "Isth".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii("is", suffix, special_case_suffix), "is".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style_ascii("a", suffix, special_case_suffix), "a".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style_ascii("test", suffix, special_case_suffix), "estt".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii("of", suffix, special_case_suffix), "of".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style_ascii("the", suffix, special_case_suffix), "eth".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii("function", suffix, special_case_suffix), "unctionf".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii("translate", suffix, special_case_suffix), "anslatetr".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii("word", suffix, special_case_suffix), "ordw".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii("I", suffix, special_case_suffix), "I".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style_ascii("Love", suffix, special_case_suffix), "Ovel".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii("Pig", suffix, special_case_suffix), "Igp".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii("Latin", suffix, special_case_suffix), "Atinl".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii("You", suffix, special_case_suffix), "Ouy".to_string() + suffix);//Y isn't a vowel here
assert_eq!(translate_word_with_style_ascii("should", suffix, special_case_suffix), "ouldsh".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii("try", suffix, special_case_suffix), "ytr".to_string() + suffix);//Y is a vowel here
assert_eq!(translate_word_with_style_ascii("yougurt", suffix, special_case_suffix), "ougurty".to_string() + suffix);//Y isn't a vowel here
//assert_eq!(translate_word_with_style_ascii("it's", suffix, special_case_suffix), "it".to_string() + special_case_suffix + "'s");//Contraction
assert_eq!(translate_word_with_style_ascii("quite", suffix, special_case_suffix), "uiteq".to_string() + suffix);//Awful to pronounce, but correct
assert_eq!(translate_word_with_style_ascii("nice", suffix, special_case_suffix), "icen".to_string() + suffix);
}
}
#[test]
#[cfg(feature = "nightly-features-generics")]
fn test_translate_word_with_style_ascii_generic() {
//TODO test with other suffixes perhaps?
let suffix = "ay";
let special_case_suffix = "way";
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("Hello"), "Elloh".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("World"), "Orldw".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("This"), "Isth".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("is"), "is".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("a"), "a".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("test"), "estt".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("of"), "of".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("the"), "eth".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("function"), "unctionf".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("translate"), "anslatetr".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("word"), "ordw".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("I"), "I".to_string() + special_case_suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("Love"), "Ovel".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("Pig"), "Igp".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("Latin"), "Atinl".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("You"), "Ouy".to_string() + suffix);//Y isn't a vowel here
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("should"), "ouldsh".to_string() + suffix);
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("try"), "ytr".to_string() + suffix);//Y is a vowel here
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("yougurt"), "ougurty".to_string() + suffix);//Y isn't a vowel here
//assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("it's"), "it".to_string() + special_case_suffix + "'s");//Contraction
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("quite"), "uiteq".to_string() + suffix);//Awful to pronounce, but correct
assert_eq!(translate_word_with_style_ascii_generic::<b"ay", b"way", b"AY", b"WAY">("nice"), "icen".to_string() + suffix);
}
fn translate_word_with_style(english_word: &str, suffix_lower: &str, special_case_suffix_lower: &str) -> String {
let mut suffix_upper = String::with_capacity(suffix_lower.len());
for letter in suffix_lower.chars() {
suffix_upper.push(letter.to_ascii_uppercase());
}
let mut special_case_suffix_upper = String::with_capacity(special_case_suffix_lower.len());
for letter in special_case_suffix_lower.chars() {
special_case_suffix_upper.push(letter.to_ascii_uppercase());
}
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
translate_word_with_style_reuse_buffers (
english_word,
suffix_lower, special_case_suffix_lower, &suffix_upper, &special_case_suffix_upper,
&mut pig_latin_word, &mut starting_consonants_buffer
);
return pig_latin_word;
}
#[cfg(feature = "nightly-features-generics")]
fn translate_word_with_style_generic <
const SUFFIX_LOWER: &'static str, const SPECIAL_CASE_SUFFIX_LOWER: &'static str,
const SUFFIX_UPPER: &'static str, const SPECIAL_CASE_SUFFIX_UPPER: &'static str
> (
english_word: &str
) -> String {
use crate::helpers::capitalize_ascii;
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
translate_word_with_style_reuse_buffers_generic::<
//Almost works, but it can't infer the length
//SUFFIX_LOWER, SPECIAL_CASE_SUFFIX_LOWER, { &capitalize_ascii(SUFFIX_LOWER.try_into().unwrap()) }, { &capitalize_ascii(SPECIAL_CASE_SUFFIX_LOWER.try_into().unwrap()) },
SUFFIX_LOWER, SPECIAL_CASE_SUFFIX_LOWER, SUFFIX_UPPER, SPECIAL_CASE_SUFFIX_UPPER,
> (
english_word,
&mut pig_latin_word, &mut starting_consonants_buffer
);
return pig_latin_word;
}
fn translate_word_with_style_ascii(english_word: &str, suffix_lower: &str, special_case_suffix_lower: &str) -> String {
let mut suffix_upper = String::with_capacity(suffix_lower.len());
for letter in suffix_lower.chars() {
suffix_upper.push(letter.to_ascii_uppercase());
}
let mut special_case_suffix_upper = String::with_capacity(special_case_suffix_lower.len());
for letter in special_case_suffix_lower.chars() {
special_case_suffix_upper.push(letter.to_ascii_uppercase());
}
let mut pig_latin_word = Vec::<u8>::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = Vec::<u8>::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
translate_word_with_style_reuse_buffers_ascii (
english_word.as_bytes(),
suffix_lower.as_bytes(), special_case_suffix_lower.as_bytes(), &suffix_upper.as_bytes(), &special_case_suffix_upper.as_bytes(),
&mut pig_latin_word, &mut starting_consonants_buffer
);
return std::str::from_utf8(pig_latin_word.as_slice()).unwrap().to_string();
}
#[cfg(feature = "nightly-features-generics")]
fn translate_word_with_style_ascii_generic <
const SUFFIX_LOWER: &'static [u8], const SPECIAL_CASE_SUFFIX_LOWER: &'static [u8],
const SUFFIX_UPPER: &'static [u8], const SPECIAL_CASE_SUFFIX_UPPER: &'static [u8]
> (
english_word: &str
) -> String {
use crate::helpers::capitalize_ascii;
let mut pig_latin_word = Vec::<u8>::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = Vec::<u8>::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
translate_word_with_style_reuse_buffers_ascii_generic::<
//Almost works, but it can't infer the length
//SUFFIX_LOWER, SPECIAL_CASE_SUFFIX_LOWER, { &capitalize_ascii(SUFFIX_LOWER.try_into().unwrap()) }, { &capitalize_ascii(SPECIAL_CASE_SUFFIX_LOWER.try_into().unwrap()) },
SUFFIX_LOWER, SPECIAL_CASE_SUFFIX_LOWER, SUFFIX_UPPER, SPECIAL_CASE_SUFFIX_UPPER,
> (
english_word.as_bytes(),
&mut pig_latin_word, &mut starting_consonants_buffer
);
return std::str::from_utf8(pig_latin_word.as_slice()).unwrap().to_string();
}
}
/* Benches */
#[cfg_attr(feature = "nightly-features-benches", cfg(test))]
#[cfg(feature = "nightly-features-benches")]
mod benches {
extern crate test;
use test::Bencher;
use super::*;
#[bench]
fn way_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers (
word,
"ay", "way", "AY", "WAY",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
#[bench]
fn yay_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers (
word,
"ay", "yay", "AY", "YAY",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
#[bench]
fn hay_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers (
word,
"ay", "hay", "AY", "HAY",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
#[bench]
fn ferb_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers (
word,
"erb", "ferb", "ERB", "FERB",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
#[bench]
#[cfg(feature = "nightly-features-generics")]
fn generic_way_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers_generic::<
"ay", "way", "AY", "WAY"
> (
word,
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
#[bench]
#[cfg(feature = "nightly-features-generics")]
fn generic_yay_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers_generic::<
"ay", "yay", "AY", "YAY"
> (
word,
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
#[bench]
#[cfg(feature = "nightly-features-generics")]
fn generic_hay_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers_generic::<
"ay", "hay", "AY", "HAY"
> (
word,
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
#[bench]
#[cfg(feature = "nightly-features-generics")]
fn generic_ferb_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers_generic::<
"erb", "ferb", "ERB", "FERB"
> (
word,
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
#[bench]
fn ascii_way_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = Vec::<u8>::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = Vec::<u8>::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box(b"translator");
translate_word_with_style_reuse_buffers_ascii (
word,
b"ay", b"way", b"AY", b"WAY",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", std::str::from_utf8(pig_latin_word.as_slice()).unwrap());//To avoid optimizing things out
}
#[bench]
fn ascii_yay_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = Vec::<u8>::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = Vec::<u8>::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box(b"translator");
translate_word_with_style_reuse_buffers_ascii (
word,
b"ay", b"yay", b"AY", b"YAY",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", std::str::from_utf8(pig_latin_word.as_slice()).unwrap());//To avoid optimizing things out
}
#[bench]
fn ascii_hay_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = Vec::<u8>::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = Vec::<u8>::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box(b"translator");
translate_word_with_style_reuse_buffers_ascii (
word,
b"ay", b"hay", b"AY", b"HAY",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", std::str::from_utf8(pig_latin_word.as_slice()).unwrap());//To avoid optimizing things out
}
#[bench]
fn ascii_ferb_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = Vec::<u8>::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = Vec::<u8>::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box(b"translator");
translate_word_with_style_reuse_buffers_ascii (
word,
b"erb", b"ferb", b"ERB", b"FERB",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", std::str::from_utf8(pig_latin_word.as_slice()).unwrap());//To avoid optimizing things out
}
#[bench]
#[cfg(feature = "nightly-features-generics")]
fn ascii_generic_way_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = Vec::<u8>::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = Vec::<u8>::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box(b"translator");
translate_word_with_style_reuse_buffers_ascii_generic::<
b"ay", b"way", b"AY", b"WAY"
> (
word,
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", std::str::from_utf8(pig_latin_word.as_slice()).unwrap());//To avoid optimizing things out
}
#[bench]
#[cfg(feature = "nightly-features-generics")]
fn ascii_generic_yay_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = Vec::<u8>::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = Vec::<u8>::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box(b"translator");
translate_word_with_style_reuse_buffers_ascii_generic::<
b"ay", b"yay", b"AY", b"YAY"
> (
word,
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", std::str::from_utf8(pig_latin_word.as_slice()).unwrap());//To avoid optimizing things out
}
#[bench]
#[cfg(feature = "nightly-features-generics")]
fn ascii_generic_hay_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = Vec::<u8>::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = Vec::<u8>::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box(b"translator");
translate_word_with_style_reuse_buffers_ascii_generic::<
b"ay", b"hay", b"AY", b"HAY"
> (
word,
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", std::str::from_utf8(pig_latin_word.as_slice()).unwrap());//To avoid optimizing things out
}
#[bench]
#[cfg(feature = "nightly-features-generics")]
fn ascii_generic_ferb_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = Vec::<u8>::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = Vec::<u8>::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box(b"translator");
translate_word_with_style_reuse_buffers_ascii_generic::<
b"erb", b"ferb", b"ERB", b"FERB"
> (
word,
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", std::str::from_utf8(pig_latin_word.as_slice()).unwrap());//To avoid optimizing things out
}
}
Loading…
Cancel
Save