#[must_use]
pub fn has_content(text: &str) -> bool {
text.chars().any(char::is_alphanumeric)
}
#[must_use]
pub fn is_whitespace_only(text: &str) -> bool {
text.chars().all(char::is_whitespace)
}
#[must_use]
pub fn normalize(text: &str) -> String {
text.split_whitespace().collect::<Vec<_>>().join(" ")
}
#[must_use]
pub fn word_count(text: &str) -> usize {
text.split_whitespace().count()
}
#[must_use]
pub fn sentence_count(text: &str) -> usize {
text.chars()
.filter(|c| matches!(c, '.' | '!' | '?'))
.count()
}
#[must_use]
pub fn clean_for_comparison(text: &str) -> String {
text.chars()
.filter(|c| c.is_alphanumeric() || c.is_whitespace())
.collect::<String>()
.to_lowercase()
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_has_content() {
assert!(has_content("Hello"));
assert!(has_content("123"));
assert!(has_content(" a "));
assert!(!has_content(""));
assert!(!has_content(" "));
assert!(!has_content("..."));
assert!(!has_content("!@#$%"));
}
#[test]
fn test_is_whitespace_only() {
assert!(is_whitespace_only(""));
assert!(is_whitespace_only(" "));
assert!(is_whitespace_only("\t\n\r"));
assert!(!is_whitespace_only("a"));
assert!(!is_whitespace_only(" a "));
}
#[test]
fn test_normalize() {
assert_eq!(normalize("hello"), "hello");
assert_eq!(normalize(" hello "), "hello");
assert_eq!(normalize("hello world"), "hello world");
assert_eq!(normalize(" a b c "), "a b c");
assert_eq!(normalize(""), "");
assert_eq!(normalize("a\n\nb"), "a b");
assert_eq!(normalize("a\t\tb"), "a b");
assert_eq!(normalize("hello\n\n\nworld"), "hello world");
}
#[test]
fn test_word_count() {
assert_eq!(word_count("hello world"), 2);
assert_eq!(word_count("one"), 1);
assert_eq!(word_count(""), 0);
assert_eq!(word_count(" "), 0);
assert_eq!(word_count("a b c d e"), 5);
}
#[test]
fn test_sentence_count() {
assert_eq!(sentence_count("Hello."), 1);
assert_eq!(sentence_count("Hello. World!"), 2);
assert_eq!(sentence_count("What? Really! Yes."), 3);
assert_eq!(sentence_count("No punctuation"), 0);
}
#[test]
fn test_clean_for_comparison() {
assert_eq!(clean_for_comparison("Hello, World!"), "hello world");
assert_eq!(clean_for_comparison("Test123"), "test123");
assert_eq!(clean_for_comparison(" UPPER lower "), "upper lower");
}
}