rust_string_utils/utils/
number.rs

1/// Checks if the input string contains only alphabetic characters.
2///
3/// # Arguments
4///
5/// * `input` - A string slice that holds the string to be checked.
6///
7/// # Returns
8///
9/// * `true` if the input string contains only alphabetic characters and is not empty.
10/// * `false` otherwise.
11///
12/// # Examples
13///
14/// ```
15/// use rust_string_utils::utils::number::is_alpha;
16/// assert_eq!(is_alpha("abc"), true);
17/// assert_eq!(is_alpha("ab2c"), false);
18/// assert_eq!(is_alpha(""), false);
19/// ```
20pub fn is_alpha(input: &str) -> bool {
21    !input.is_empty() && input.chars().all(|c| c.is_alphabetic())
22}
23
24/// Checks if the input string contains only alphanumeric characters.
25///
26/// # Arguments
27///
28/// * `input` - A string slice that holds the string to be checked.
29///
30/// # Returns
31///
32/// * `true` if the input string contains only alphanumeric characters and is not empty.
33/// * `false` otherwise.
34///
35/// # Examples
36///
37/// ```
38/// use rust_string_utils::utils::number::is_alphanumeric;
39/// assert_eq!(is_alphanumeric("abc123"), true);
40/// assert_eq!(is_alphanumeric("abc 123"), false);
41/// assert_eq!(is_alphanumeric(""), false);
42/// ```
43pub fn is_alphanumeric(input: &str) -> bool {
44    !input.is_empty() && input.chars().all(|c| c.is_alphanumeric())
45}
46
47/// Checks if the input string contains only alphanumeric characters or spaces.
48///
49/// # Arguments
50///
51/// * `input` - A string slice that holds the string to be checked.
52///
53/// # Returns
54///
55/// * `true` if the input string contains only alphanumeric characters or spaces.
56/// * `false` otherwise.
57///
58/// # Examples
59///
60/// ```
61/// use rust_string_utils::utils::number::is_alphanumeric_space;
62/// assert_eq!(is_alphanumeric_space("abc 123"), true);
63/// assert_eq!(is_alphanumeric_space("abc-123"), false);
64/// assert_eq!(is_alphanumeric_space(""), true);
65/// ```
66pub fn is_alphanumeric_space(input: &str) -> bool {
67    input
68        .chars()
69        .all(|c| c.is_alphanumeric() || c.is_whitespace())
70}
71
72/// Checks if the input string contains only alphabetic characters or spaces.
73///
74/// # Arguments
75///
76/// * `input` - A string slice that holds the string to be checked.
77///
78/// # Returns
79///
80/// * `true` if the input string contains only alphabetic characters or spaces.
81/// * `false` otherwise.
82///
83/// # Examples
84///
85/// ```
86/// use rust_string_utils::utils::number::is_alpha_space;
87/// assert_eq!(is_alpha_space("abc"), true);
88/// assert_eq!(is_alpha_space("ab c"), true);
89/// assert_eq!(is_alpha_space("ab2c"), false);
90/// assert_eq!(is_alpha_space("ab-c"), false);
91/// ```
92pub fn is_alpha_space(input: &str) -> bool {
93    input
94        .chars()
95        .all(|c| c.is_alphabetic() || c.is_whitespace())
96}
97
98/// Checks if the input string contains only ASCII printable characters.
99///
100/// # Arguments
101///
102/// * `input` - A string slice that holds the string to be checked.
103///
104/// # Returns
105///
106/// * `true` if the input string contains only ASCII printable characters or is empty.
107/// * `false` otherwise.
108///
109/// # Examples
110///
111/// ```
112/// use rust_string_utils::utils::number::is_ascii_printable;
113/// assert_eq!(is_ascii_printable(""), true);
114/// assert_eq!(is_ascii_printable(" "), true);
115/// assert_eq!(is_ascii_printable("Ceki"), true);
116/// assert_eq!(is_ascii_printable("ab2c"), true);
117/// assert_eq!(is_ascii_printable("!ab-c~"), true);
118/// assert_eq!(is_ascii_printable("\x7F"), false);
119/// assert_eq!(is_ascii_printable("Ceki Gülcü"), false);
120/// ```
121pub fn is_ascii_printable(input: &str) -> bool {
122    input.is_empty()
123        || input
124            .chars()
125            .all(|c| c.is_ascii() && (c.is_ascii_graphic() || c.is_ascii_whitespace()))
126}
127
128#[cfg(test)]
129mod tests {
130    use super::*;
131
132    #[test]
133    fn test_is_alpha() {
134        assert_eq!(is_alpha(""), false);
135        assert_eq!(is_alpha("  "), false);
136        assert_eq!(is_alpha("abc"), true);
137        assert_eq!(is_alpha("ab2c"), false);
138        assert_eq!(is_alpha("ab-c"), false);
139    }
140
141    #[test]
142    fn test_is_alphanumeric() {
143        assert_eq!(is_alphanumeric(""), false);
144        assert_eq!(is_alphanumeric("  "), false);
145        assert_eq!(is_alphanumeric("abc"), true);
146        assert_eq!(is_alphanumeric("ab c"), false);
147        assert_eq!(is_alphanumeric("ab2c"), true);
148        assert_eq!(is_alphanumeric("ab-c"), false);
149    }
150
151    #[test]
152    fn test_is_alphanumeric_space() {
153        assert_eq!(is_alphanumeric_space(""), true);
154        assert_eq!(is_alphanumeric_space("  "), true);
155        assert_eq!(is_alphanumeric_space("abc"), true);
156        assert_eq!(is_alphanumeric_space("ab c"), true);
157        assert_eq!(is_alphanumeric_space("ab2c"), true);
158        assert_eq!(is_alphanumeric_space("ab-c"), false);
159    }
160
161    #[test]
162    fn test_is_alpha_space() {
163        assert_eq!(is_alpha_space(""), true);
164        assert_eq!(is_alpha_space("  "), true);
165        assert_eq!(is_alpha_space("abc"), true);
166        assert_eq!(is_alpha_space("ab c"), true);
167        assert_eq!(is_alpha_space("ab2c"), false);
168        assert_eq!(is_alpha_space("ab-c"), false);
169    }
170
171    #[test]
172    fn test_is_ascii_printable() {
173        assert_eq!(is_ascii_printable(""), true);
174        assert_eq!(is_ascii_printable(" "), true);
175        assert_eq!(is_ascii_printable("Ceki"), true);
176        assert_eq!(is_ascii_printable("ab2c"), true);
177        assert_eq!(is_ascii_printable("!ab-c~"), true);
178        assert_eq!(is_ascii_printable(" "), true);
179        assert_eq!(is_ascii_printable("!"), true);
180        assert_eq!(is_ascii_printable("~"), true);
181        assert_eq!(is_ascii_printable("\x7F"), false);
182        assert_eq!(is_ascii_printable("Ceki Gülcü"), false);
183    }
184}