Skip to content

Implement Eq for Regex #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from Apr 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions regex_macros/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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+");
Expand Down
11 changes: 11 additions & 0 deletions src/re.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ impl fmt::Debug for Regex {
}
}

/// Equality comparison is based on the original string. It is possible that different regular
/// 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yorhel Could you please put a doc comment on this impl explaining the definition of equality used? (Something like what you said in your initial comment is just fine.)

fn eq(&self, other: &Regex) -> bool {
self.as_str() == other.as_str()
}
}

impl Eq for Regex {}

impl FromStr for Regex {
type Err = parse::Error;

Expand Down