1#[must_use]
20pub fn has_content(text: &str) -> bool {
21 text.chars().any(char::is_alphanumeric)
22}
23
24#[must_use]
36pub fn is_whitespace_only(text: &str) -> bool {
37 text.chars().all(char::is_whitespace)
38}
39
40#[must_use]
53pub fn normalize(text: &str) -> String {
54 text.split_whitespace().collect::<Vec<_>>().join(" ")
55}
56
57#[must_use]
70pub fn word_count(text: &str) -> usize {
71 text.split_whitespace().count()
72}
73
74#[must_use]
86pub fn sentence_count(text: &str) -> usize {
87 text.chars()
88 .filter(|c| matches!(c, '.' | '!' | '?'))
89 .count()
90}
91
92#[must_use]
106pub fn clean_for_comparison(text: &str) -> String {
107 text.chars()
108 .filter(|c| c.is_alphanumeric() || c.is_whitespace())
109 .collect::<String>()
110 .to_lowercase()
111 .split_whitespace()
112 .collect::<Vec<_>>()
113 .join(" ")
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119
120 #[test]
121 fn test_has_content() {
122 assert!(has_content("Hello"));
123 assert!(has_content("123"));
124 assert!(has_content(" a "));
125 assert!(!has_content(""));
126 assert!(!has_content(" "));
127 assert!(!has_content("..."));
128 assert!(!has_content("!@#$%"));
129 }
130
131 #[test]
132 fn test_is_whitespace_only() {
133 assert!(is_whitespace_only(""));
134 assert!(is_whitespace_only(" "));
135 assert!(is_whitespace_only("\t\n\r"));
136 assert!(!is_whitespace_only("a"));
137 assert!(!is_whitespace_only(" a "));
138 }
139
140 #[test]
141 fn test_normalize() {
142 assert_eq!(normalize("hello"), "hello");
143 assert_eq!(normalize(" hello "), "hello");
144 assert_eq!(normalize("hello world"), "hello world");
145 assert_eq!(normalize(" a b c "), "a b c");
146 assert_eq!(normalize(""), "");
147 assert_eq!(normalize("a\n\nb"), "a b");
149 assert_eq!(normalize("a\t\tb"), "a b");
150 assert_eq!(normalize("hello\n\n\nworld"), "hello world");
151 }
152
153 #[test]
154 fn test_word_count() {
155 assert_eq!(word_count("hello world"), 2);
156 assert_eq!(word_count("one"), 1);
157 assert_eq!(word_count(""), 0);
158 assert_eq!(word_count(" "), 0);
159 assert_eq!(word_count("a b c d e"), 5);
160 }
161
162 #[test]
163 fn test_sentence_count() {
164 assert_eq!(sentence_count("Hello."), 1);
165 assert_eq!(sentence_count("Hello. World!"), 2);
166 assert_eq!(sentence_count("What? Really! Yes."), 3);
167 assert_eq!(sentence_count("No punctuation"), 0);
168 }
169
170 #[test]
171 fn test_clean_for_comparison() {
172 assert_eq!(clean_for_comparison("Hello, World!"), "hello world");
173 assert_eq!(clean_for_comparison("Test123"), "test123");
174 assert_eq!(clean_for_comparison(" UPPER lower "), "upper lower");
175 }
176}