git_anger_library/
core.rs1use once_cell::sync::OnceCell;
2use std::collections::HashSet;
3
4pub static CURSES_FILE: &str = include_str!("words.txt");
6
7fn curses() -> &'static HashSet<&'static str> {
8 static INSTANCE: OnceCell<HashSet<&'static str>> = OnceCell::new();
9 INSTANCE.get_or_init(|| CURSES_FILE.lines().map(|l| l.trim()).collect())
10}
11
12pub fn split_into_clean_words(input: &str) -> impl Iterator<Item = &str> {
18 input
19 .split(|c: char| !char::is_alphabetic(c))
20 .filter(|w| !w.is_empty())
21}
22
23pub fn naughty_word(word: &str) -> bool {
25 curses().contains(&word)
26}
27
28#[cfg(test)]
29mod test {
30 use super::*;
31
32 #[test]
33 fn test_naughty_words() {
34 assert!(naughty_word("fuck"));
35 assert!(naughty_word("cyberfuckers"));
36 assert!(naughty_word("shitty"));
37 assert!(!naughty_word("pretty"));
38 }
39
40 #[test]
41 fn test_clean_word() {
42 let words = split_into_clean_words("This! is a string: with, some. words in? it;");
43 assert_eq!(
44 vec!["This", "is", "a", "string", "with", "some", "words", "in", "it"],
45 words.collect::<Vec<_>>()
46 );
47 }
48}