use crate::byte_string::translate_with_style_lower_and_upper_suffixes as translate_byte_string_with_style_lower_and_upper_suffixes;
pub fn translate(english: &str) -> String {
return translate_way(english);
}
pub fn translate_way(english: &str) -> String {
return translate_with_style_lower_and_upper_suffixes(english, "ay", "way", "AY", "WAY");
}
pub fn translate_yay(english: &str) -> String {
return translate_with_style_lower_and_upper_suffixes(english, "ay", "yay", "AY", "YAY");
}
pub fn translate_hay(english: &str) -> String {
return translate_with_style_lower_and_upper_suffixes(english, "ay", "hay", "AY", "HAY");
}
pub fn translate_ferb(english: &str) -> String {
return translate_with_style_lower_and_upper_suffixes(english, "erb", "ferb", "ERB", "FERB");
}
pub fn translate_with_style(english: &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());
}
return translate_with_style_lower_and_upper_suffixes (
english,
suffix_lower, special_case_suffix_lower, &suffix_upper, &special_case_suffix_upper
);
}
fn translate_with_style_lower_and_upper_suffixes (
english: &str,
suffix_lower: &str, special_case_suffix_lower: &str, suffix_upper: &str, special_case_suffix_upper: &str
) -> String {
let mut pig_latin_string_bytes = Vec::<u8>::with_capacity(english.len() * 2); translate_byte_string_with_style_lower_and_upper_suffixes (
english.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_string_bytes
);
return unsafe { String::from_utf8_unchecked(pig_latin_string_bytes) };
}
#[cfg(test)]
mod tests {
use super::*;
const SUFFIX_SPECIAL_CASE_SUFFIX_PAIRS: [(&str, &str); 9] = [
("ay", "way"), ("ay", "yay"), ("ay", "hay"), ("erb", "ferb"), ("ancy", "fancy"), ("orange", "porange"), ("anana", "banana"), ("atin", "latin"), ("ust", "rust")
];
const SUFFIX_SPECIAL_CASE_SUFFIX_LOWER_UPPER_TUPLES: [(&str, &str, &str, &str); 9] = [
("ay", "way", "AY", "WAY"), ("ay", "yay", "AY", "YAY"), ("ay", "hay", "AY", "HAY"), ("erb", "ferb", "ERB", "FERB"),
("ancy", "fancy", "ANCY", "FANCY"), ("orange", "porange", "ORANGE", "PORANGE"),
("anana", "banana", "ANANA", "BANANA"), ("atin", "latin", "ATIN", "LATIN"), ("ust", "rust", "UST", "RUST"),
];
#[test]
fn test_translate_with_style() {
for pair in SUFFIX_SPECIAL_CASE_SUFFIX_PAIRS {
let suffix = pair.0;
let special_case_suffix = pair.1;
assert_eq!(translate_with_style("Hello world from the coolest Pig Latin translator!", suffix, special_case_suffix),
"Elloh".to_string() + suffix + " orldw" + suffix + " omfr" + suffix + " eth" + suffix + " oolestc" + suffix + " Igp" + suffix + " Atinl" + suffix + " anslatortr" + suffix + "!"
);
assert_eq!(translate_with_style("This library can translate any English text. It can even handle multiple sentences!", suffix, special_case_suffix),
"Isth".to_string() + suffix + " ibraryl" + suffix + " anc" + suffix + " anslatetr" + suffix + " any" + special_case_suffix + " English" + special_case_suffix + " extt" + suffix +
". It" + special_case_suffix + " anc" + suffix + " even" + special_case_suffix + " andleh" + suffix + " ultiplem" + suffix + " entencess" + suffix + "!"
);
}
}
#[test]
fn test_translate_with_style_edgecases() {
for pair in SUFFIX_SPECIAL_CASE_SUFFIX_PAIRS {
let suffix = pair.0;
let special_case_suffix = pair.1;
assert_eq!(translate_with_style("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!", suffix, special_case_suffix),
"Etl".to_string() + suffix + "'s ytr" + suffix + " omes" + suffix + " edge" + special_case_suffix + " asesc" + suffix + ". Atth" + suffix + " is" + special_case_suffix + " a" +
special_case_suffix + " ontractionc" + suffix + ", as" + special_case_suffix + " ellw" + suffix + " as" + special_case_suffix + " a" + special_case_suffix + " ordw" + suffix +
" erewh" + suffix + " eth" + suffix + " only" + special_case_suffix + " owelv" + suffix + " is" + special_case_suffix + " y" + special_case_suffix + ". Eatn" + suffix + ", all" +
special_case_suffix + " atth" + suffix + " orksw" + suffix + "!"
);
assert_eq!(translate_with_style("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxz", suffix, special_case_suffix),
"Atwh".to_string() + suffix + " if" + special_case_suffix + " a" + special_case_suffix + " ordw" + suffix + " ash" + suffix + " on" + suffix + " owelsv" + suffix + ", ikel" + suffix + " isth" + suffix + ": bcdfghjklmnpqrstvwxz" + suffix
);
assert_eq!(translate_with_style("Cool, so the heuristics make pretty good guesses with what they're fed!", suffix, special_case_suffix),
"Oolc".to_string() + suffix + ", os" + suffix + " eth" + suffix + " euristicsh" + suffix + " akem" + suffix + " ettypr" + suffix + " oodg" + suffix + " uessesg" + suffix + " ithw" + suffix + " atwh" + suffix + " eyth" + suffix + "'re edf" + suffix + "!"
);
assert_eq!(translate_with_style("Hello-world", suffix, special_case_suffix), "Elloh".to_string() + suffix + "-orldw" + suffix);
assert_eq!(translate_with_style("Hyphens-are-difficult-aren't-they?", suffix, special_case_suffix),
"Yphensh".to_string() + suffix + "-are" + special_case_suffix + "-ifficultd" + suffix + "-aren" + special_case_suffix + "'t-eyth" + suffix + "?"
);
}
}
#[test]
fn test_translate_with_style_uppercase() {
for pair in SUFFIX_SPECIAL_CASE_SUFFIX_LOWER_UPPER_TUPLES {
let suffix_lower = pair.0;
let special_case_suffix_lower = pair.1;
let suffix_upper = pair.2;
let special_case_suffix_upper = pair.3;
assert_eq!(translate_with_style("HELLO WORLD!", suffix_lower, special_case_suffix_lower),
"ELLOH".to_string() + suffix_upper + " ORLDW" + suffix_upper + "!"
);
assert_eq!(translate_with_style("ISN't THIS COOL?", suffix_lower, special_case_suffix_lower),
"ISN".to_string() + special_case_suffix_upper + "'t ISTH" + suffix_upper + " OOLC" + suffix_upper + "?"
);
assert_eq!(translate_with_style("What ABOUT a MIX?", suffix_lower, special_case_suffix_lower),
"Atwh".to_string() + suffix_lower + " ABOUT" + special_case_suffix_upper + " a" + special_case_suffix_lower + " IXM" + suffix_upper + "?"
);
assert_eq!(translate_with_style("Luke, I am your father!", suffix_lower, special_case_suffix_lower), "Ukel".to_string() + suffix_lower + ", I" + special_case_suffix_lower+ " am" + special_case_suffix_lower + " oury" + suffix_lower + " atherf" + suffix_lower + "!"
);
}
}
}
#[cfg_attr(feature = "nightly-features-benches", cfg(test))]
#[cfg(feature = "nightly-features-benches")]
mod benches {
extern crate test;
use test::Bencher;
use super::*;
const PROJECT_DESCRIPTION: &str = "A simple Rust library to translate from English to Pig Latin!";
const LOREM_IPSUM: &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
#[bench]
fn way_project_description(b: &mut Bencher) {
b.iter(|| -> String { return translate(PROJECT_DESCRIPTION); });
}
#[bench]
fn yay_project_description(b: &mut Bencher) {
b.iter(|| -> String { return translate_yay(PROJECT_DESCRIPTION); });
}
#[bench]
fn hay_project_description(b: &mut Bencher) {
b.iter(|| -> String { return translate_hay(PROJECT_DESCRIPTION); });
}
#[bench]
fn ferb_project_description(b: &mut Bencher) {
b.iter(|| -> String { return translate_ferb(PROJECT_DESCRIPTION); });
}
#[bench]
fn way_lorem_ipsum(b: &mut Bencher) {
b.iter(|| -> String { return translate(LOREM_IPSUM); });
}
#[bench]
fn yay_lorem_ipsum(b: &mut Bencher) {
b.iter(|| -> String { return translate_yay(LOREM_IPSUM); });
}
#[bench]
fn hay_lorem_ipsum(b: &mut Bencher) {
b.iter(|| -> String { return translate_hay(LOREM_IPSUM); });
}
#[bench]
fn ferb_lorem_ipsum(b: &mut Bencher) {
b.iter(|| -> String { return translate_ferb(LOREM_IPSUM); });
}
}