1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/// Counts the characters in target string with predicate function, returns the number of all matched characters.
///
///
/// # Arguments
///
/// * `s` - The string to count characters.
/// * `predicate` - The predicate function invoked on each character with a parameter.
///
/// # Returns
///
/// Returns the number of all matched characters.
///
/// # Example
///
/// ```rust
/// use rufl::string;
///
/// assert_eq!(5, string::count_by("hello!", string::is_alpha));
///
/// assert_eq!(3, string::count_by("1a2b3c", |s: &str| -> bool {s.chars().all(char::is_numeric)}));
///
/// ```
pub fn count_by(s: impl AsRef<str>, predicate: fn(&str) -> bool) -> usize {
match s.as_ref().len() {
0 => 0,
_ => {
let mut count = 0;
for c in crate::string::split_graphemes(s.as_ref()).iter() {
if predicate(c) {
count += 1;
}
}
count
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_count_by() {
assert_eq!(5, count_by("hello!", crate::string::is_alpha));
assert_eq!(3, count_by("1a2b3c", crate::string::is_digit));
assert_eq!(
1,
count_by("a b", |s: &str| -> bool {
s.chars().all(char::is_whitespace)
})
);
assert_eq!(
1,
count_by("hello\n", |s: &str| -> bool {
s.chars().all(char::is_control)
})
);
}
}