Skip to content

Commit e550a91

Browse files
authored
Merge pull request #37 from epage/polish
Polish
2 parents 034edfd + 6cd33ff commit e550a91

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/iter.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ where
8181
/// `in_hash`. The implementation-specific predicates will be
8282
/// deprecated when Rust supports trait specialization.
8383
///
84+
/// If you need to optimize this
85+
/// - Type is `Ord`, call `sort()` on this predicate.
86+
/// - Type is `Hash`, replace `in_iter` with `in_hash`.
87+
///
8488
/// # Examples
8589
///
8690
/// ```

src/str/regex.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,43 @@ pub struct RegexPredicate {
2121
re: regex::Regex,
2222
}
2323

24+
impl RegexPredicate {
25+
/// Require a specific count of matches.
26+
///
27+
/// # Examples
28+
///
29+
/// ```
30+
/// use predicates::prelude::*;
31+
///
32+
/// let predicate_fn = predicate::str::is_match("T[a-z]*").unwrap().count(3);
33+
/// assert_eq!(true, predicate_fn.eval("One Two Three Two One"));
34+
/// assert_eq!(false, predicate_fn.eval("One Two Three"));
35+
/// ```
36+
pub fn count(self, count: usize) -> RegexMatchesPredicate {
37+
RegexMatchesPredicate { re: self.re, count }
38+
}
39+
}
40+
2441
impl Predicate<str> for RegexPredicate {
2542
fn eval(&self, variable: &str) -> bool {
2643
self.re.is_match(variable)
2744
}
2845
}
2946

47+
/// Predicate that checks for repeated patterns.
48+
///
49+
/// This is created by `predicates::str::is_match(...).count`.
50+
#[derive(Clone, Debug)]
51+
pub struct RegexMatchesPredicate {
52+
re: regex::Regex,
53+
count: usize,
54+
}
55+
56+
impl Predicate<str> for RegexMatchesPredicate {
57+
fn eval(&self, variable: &str) -> bool {
58+
self.re.find_iter(variable).count() == self.count
59+
}
60+
}
3061
/// Creates a new `Predicate` that uses a regular expression to match the string.
3162
///
3263
/// # Examples

0 commit comments

Comments
 (0)