1pub fn is_alphanumeric(s: &str) -> bool {
14 !s.is_empty() && s.chars().all(|c| c.is_alphanumeric())
15}
16
17pub fn is_alpha(s: &str) -> bool {
28 !s.is_empty() && s.chars().all(|c| c.is_alphabetic())
29}
30
31pub fn is_numeric(s: &str) -> bool {
42 !s.is_empty() && s.chars().all(|c| c.is_ascii_digit())
43}
44
45pub fn has_min_length(s: &str, min: usize) -> bool {
47 s.len() >= min
48}
49
50pub fn has_max_length(s: &str, max: usize) -> bool {
52 s.len() <= max
53}
54
55pub fn has_length_between(s: &str, min: usize, max: usize) -> bool {
57 let len = s.len();
58 len >= min && len <= max
59}
60
61pub fn contains(s: &str, pattern: &str) -> bool {
63 s.contains(pattern)
64}
65
66pub fn contains_case_insensitive(s: &str, pattern: &str) -> bool {
68 s.to_lowercase().contains(&pattern.to_lowercase())
69}
70
71pub fn is_uppercase(s: &str) -> bool {
73 !s.is_empty() && s.chars().filter(|c| c.is_alphabetic()).all(|c| c.is_uppercase())
74}
75
76pub fn is_lowercase(s: &str) -> bool {
78 !s.is_empty() && s.chars().filter(|c| c.is_alphabetic()).all(|c| c.is_lowercase())
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn test_alphanumeric() {
87 assert!(is_alphanumeric("abc123"));
88 assert!(is_alphanumeric("ABC"));
89 assert!(!is_alphanumeric("abc-123"));
90 assert!(!is_alphanumeric(""));
91 }
92
93 #[test]
94 fn test_alpha() {
95 assert!(is_alpha("abcXYZ"));
96 assert!(!is_alpha("abc123"));
97 assert!(!is_alpha(""));
98 }
99
100 #[test]
101 fn test_numeric() {
102 assert!(is_numeric("12345"));
103 assert!(!is_numeric("123.45"));
104 assert!(!is_numeric("abc"));
105 assert!(!is_numeric(""));
106 }
107
108 #[test]
109 fn test_length_validations() {
110 assert!(has_min_length("hello", 3));
111 assert!(!has_min_length("hi", 3));
112
113 assert!(has_max_length("hi", 5));
114 assert!(!has_max_length("hello world", 5));
115
116 assert!(has_length_between("hello", 3, 10));
117 assert!(!has_length_between("hi", 3, 10));
118 }
119
120 #[test]
121 fn test_contains() {
122 assert!(contains("hello world", "world"));
123 assert!(!contains("hello world", "foo"));
124
125 assert!(contains_case_insensitive("Hello World", "world"));
126 assert!(contains_case_insensitive("HELLO", "hello"));
127 }
128
129 #[test]
130 fn test_case() {
131 assert!(is_uppercase("HELLO"));
132 assert!(is_uppercase("HELLO123"));
133 assert!(!is_uppercase("Hello"));
134
135 assert!(is_lowercase("hello"));
136 assert!(is_lowercase("hello123"));
137 assert!(!is_lowercase("Hello"));
138 }
139}
140