From 44d63edc3dc9012220359c232b5f1b17e4c83431 Mon Sep 17 00:00:00 2001 From: Yorhel Date: Thu, 23 Apr 2015 10:17:25 +0200 Subject: [PATCH 1/3] Implement Eq for Regex Note that this is just a comparison of the original strings, it's still possible that two different strings result in the same matching behaviour. --- regex_macros/tests/tests.rs | 5 +++++ src/re.rs | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/regex_macros/tests/tests.rs b/regex_macros/tests/tests.rs index ecd965ff8f..217155d221 100644 --- a/regex_macros/tests/tests.rs +++ b/regex_macros/tests/tests.rs @@ -10,6 +10,11 @@ use regex::{Regex, NoExpand}; +#[test] +fn eq() { + assert_eq!(regex!(r"[a-z]+"), Regex::new("[a-z]+").unwrap()); +} + #[test] fn splitn() { let re = regex!(r"\d+"); diff --git a/src/re.rs b/src/re.rs index 845f53a010..e4f268f961 100644 --- a/src/re.rs +++ b/src/re.rs @@ -159,6 +159,14 @@ impl fmt::Debug for Regex { } } +impl PartialEq for Regex { + fn eq(&self, other: &Regex) -> bool { + self.as_str() == other.as_str() + } +} + +impl Eq for Regex {} + impl FromStr for Regex { type Err = parse::Error; From 296cc506ffb7ef33ec1874f4631a2d3589f06e81 Mon Sep 17 00:00:00 2001 From: Yorhel Date: Thu, 23 Apr 2015 19:04:54 +0200 Subject: [PATCH 2/3] Document (Partial)Eq behaviour for Regex --- src/re.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/re.rs b/src/re.rs index e4f268f961..6def50617d 100644 --- a/src/re.rs +++ b/src/re.rs @@ -159,6 +159,9 @@ impl fmt::Debug for Regex { } } +/// Equality comparison is based on the original string. It is possible that different regular +/// expressions have the same matching behaviour, but are still compared inequal. For example, +/// `\d+` and `\d\d*` match the same set of strings, but are not considered equal. impl PartialEq for Regex { fn eq(&self, other: &Regex) -> bool { self.as_str() == other.as_str() From 4c60549c7871870c042c9444541edb161e71d1fa Mon Sep 17 00:00:00 2001 From: Yorhel Date: Thu, 23 Apr 2015 19:10:12 +0200 Subject: [PATCH 3/3] Spelling fix (Sorry for the noise, didn't even realize "behaviour" was British spelling) --- src/re.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/re.rs b/src/re.rs index 6def50617d..2ef58a56eb 100644 --- a/src/re.rs +++ b/src/re.rs @@ -160,7 +160,7 @@ impl fmt::Debug for Regex { } /// Equality comparison is based on the original string. It is possible that different regular -/// expressions have the same matching behaviour, but are still compared inequal. For example, +/// expressions have the same matching behavior, but are still compared unequal. For example, /// `\d+` and `\d\d*` match the same set of strings, but are not considered equal. impl PartialEq for Regex { fn eq(&self, other: &Regex) -> bool {