Skip to content

Commit 0709ef1

Browse files
committed
Merge pull request #81 from yorhel/master
Implement Eq for Regex
2 parents 45b6739 + 4c60549 commit 0709ef1

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

regex_macros/tests/tests.rs

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
use regex::{Regex, NoExpand};
1212

13+
#[test]
14+
fn eq() {
15+
assert_eq!(regex!(r"[a-z]+"), Regex::new("[a-z]+").unwrap());
16+
}
17+
1318
#[test]
1419
fn splitn() {
1520
let re = regex!(r"\d+");

src/re.rs

+11
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ impl fmt::Debug for Regex {
159159
}
160160
}
161161

162+
/// Equality comparison is based on the original string. It is possible that different regular
163+
/// expressions have the same matching behavior, but are still compared unequal. For example,
164+
/// `\d+` and `\d\d*` match the same set of strings, but are not considered equal.
165+
impl PartialEq for Regex {
166+
fn eq(&self, other: &Regex) -> bool {
167+
self.as_str() == other.as_str()
168+
}
169+
}
170+
171+
impl Eq for Regex {}
172+
162173
impl FromStr for Regex {
163174
type Err = parse::Error;
164175

0 commit comments

Comments
 (0)