Sha256: f08575948d24b2c7c615836202e54ea68d4ae2a2a8731d60f326aa0b7f94050f
Contents?: true
Size: 891 Bytes
Versions: 180
Compression:
Stored size: 891 Bytes
Contents
#[macro_use] extern crate lazy_static; extern crate regex; use regex::Regex; // Regular expressions from Python version of exercism pub fn translate_word(word: &str) -> String { // Prevent creation and compilation at every call. // These are compiled exactly once lazy_static! { // Detects if it starts with a vowel static ref VOWEL: Regex = Regex::new(r"^([aeiou]|y[^aeiou]|xr)[a-z]*").unwrap(); // Detects splits for initial consonants static ref CONSONANTS: Regex = Regex::new(r"^([^aeiou]?qu|[^aeiou]+)([a-z]*)").unwrap(); } if VOWEL.is_match(word) { String::from(word) + "ay" } else { let caps = CONSONANTS.captures(word).unwrap(); String::from(&caps[2]) + &caps[1] + "ay" } } pub fn translate(text: &str) -> String { text.split(" ").map(|w| translate_word(w)).collect::<Vec<_>>().join(" ") }
Version data entries
180 entries across 180 versions & 1 rubygems