Skip to main content

english_core/
utils.rs

1use crate::EnglishCore;
2impl EnglishCore {
3    pub fn pair_match(word: &str, listik: &[(&str, &str)]) -> Option<String> {
4        listik
5            .iter()
6            .find(|(sing, _)| *sing == word)
7            .map(|(_, plur)| plur.to_string())
8    }
9
10    pub fn replace_last_occurence(input: &str, pattern: &str, replacement: &str) -> String {
11        if let Some(last_index) = input.rfind(pattern) {
12            let (before_last, _after_last) = input.split_at(last_index);
13            format!("{}{}", before_last, replacement)
14        } else {
15            input.into()
16        }
17    }
18    pub fn iter_replace_last(word: &str, pairs: &[(&str, &str)]) -> Option<String> {
19        for (sing, plur) in pairs {
20            if word.ends_with(sing) {
21                return Some(EnglishCore::replace_last_occurence(word, sing, plur));
22            }
23        }
24        None
25    }
26
27    pub fn starts_with_uppercase(word: &str) -> bool {
28        word.chars()
29            .next()
30            .map(|c| c.is_uppercase())
31            .unwrap_or(false)
32    }
33}