Commit 1c0c52a9 authored by John Zacarias Jekel's avatar John Zacarias Jekel
Browse files

Minor improvements

parent caf6bdee
Loading
Loading
Loading
Loading
+15 −35
Original line number Diff line number Diff line
@@ -430,7 +430,7 @@ pub fn translate_with_style(english: &str, suffix_lower: &str, special_case_suff
    //Buffers for improved performance (avoid repeated heap allocations)
    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

    for character in english.chars().peekable() {
    for character in english.chars() {
        if in_word {
            if character.is_alphabetic() {
                //Save the character to translate once the word ends; we also keep apostrophes so that translate_word_with_style can handle contractions
@@ -630,14 +630,18 @@ pub fn translate_with_style_ascii(english: &str, suffix_lower: &str, special_cas
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_translate_with_style() {
        let suffix_special_case_suffix_pairs = [
    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"),
    ];

        for pair in suffix_special_case_suffix_pairs {
    #[test]
    fn test_translate_with_style() {
        for pair in SUFFIX_SPECIAL_CASE_SUFFIX_PAIRS {
            let suffix = pair.0;
            let special_case_suffix = pair.1;

@@ -654,11 +658,7 @@ mod tests {

    #[test]
    fn test_translate_with_style_edgecases() {
        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 {
        for pair in SUFFIX_SPECIAL_CASE_SUFFIX_PAIRS {
            let suffix = pair.0;
            let special_case_suffix = pair.1;

@@ -687,13 +687,7 @@ mod tests {

    #[test]
    fn test_translate_with_style_uppercase() {
        let suffix_special_case_suffix_lower_upper_tuples = [
            ("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"),
        ];

        for pair in suffix_special_case_suffix_lower_upper_tuples {
        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;
@@ -724,11 +718,7 @@ mod tests {

    #[test]
    fn test_translate_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 {
        for pair in SUFFIX_SPECIAL_CASE_SUFFIX_PAIRS {
            let suffix = pair.0;
            let special_case_suffix = pair.1;

@@ -745,11 +735,7 @@ mod tests {

    #[test]
    fn test_translate_with_style_ascii_edgecases() {
        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 {
        for pair in SUFFIX_SPECIAL_CASE_SUFFIX_PAIRS {
            let suffix = pair.0;
            let special_case_suffix = pair.1;

@@ -778,13 +764,7 @@ mod tests {

    #[test]
    fn test_translate_with_style_ascii_uppercase() {
        let suffix_special_case_suffix_lower_upper_tuples = [
            ("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"),
        ];

        for pair in suffix_special_case_suffix_lower_upper_tuples {
        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;
+8 −20
Original line number Diff line number Diff line
@@ -13,14 +13,10 @@ use crate::helpers::{is_vowel, is_y, word_is_uppercase, word_is_uppercase_ascii}
/* Functions */

pub(crate) fn translate_word_with_style_reuse_buffers (
    english_word: &str,
    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.is_empty() {
        return;
    }

    if english_word.len() == 1 {
        buffer_to_append_to.push_str(english_word);
        buffer_to_append_to.push_str(special_case_suffix_lower);
@@ -36,11 +32,9 @@ pub(crate) fn translate_word_with_style_reuse_buffers (
    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
    //However, if any word is only one letter long, this takes priority and the word is treated like a vowel
    let first_letter_was_vowel: bool = {
        is_vowel(first_letter)//Not including y
        || if let Some(character) = iterator.peek() { !character.is_alphabetic() } else { true }//Non-alphabetic character after the first letter, or the word ends after the first letter
    };
    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 {
@@ -100,14 +94,10 @@ pub(crate) fn translate_word_with_style_reuse_buffers (
}

pub(crate) fn translate_word_with_style_reuse_buffers_ascii (
    english_word: &str,
    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.is_empty() {
        return;
    }

    if english_word.len() == 1 {
        buffer_to_append_to.push_str(english_word);
        buffer_to_append_to.push_str(special_case_suffix_lower);
@@ -125,11 +115,9 @@ pub(crate) fn translate_word_with_style_reuse_buffers_ascii (
    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
    //However, if any word is only one letter long, this takes priority and the word is treated like a vowel
    let first_letter_was_vowel: bool = {
        is_vowel(first_letter)//Not including y
        || if let Some(character) = iterator.peek() { !character.is_alphabetic() } else { true }//Non-alphabetic character after the first letter, or the word ends after the first letter
    };
    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 {