Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion regex-automata/src/util/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn parse_many_with<P: AsRef<str>>(
/// These options are defined as a group since they apply to every regex engine
/// in this crate. Instead of re-defining them on every engine's builder, they
/// are instead provided here as one cohesive unit.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Config {
case_insensitive: bool,
multi_line: bool,
Expand Down
6 changes: 5 additions & 1 deletion src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ impl Builder {
.configure(metac)
.syntax(syntaxc)
.build(&pattern)
.map(|meta| crate::Regex { meta, pattern })
.map(|meta| crate::Regex {
meta,
pattern,
syntaxc: Arc::new(self.syntaxc),
})
.map_err(Error::from_meta_build_error)
}

Expand Down
26 changes: 26 additions & 0 deletions src/regex/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ use crate::{error::Error, RegexBuilder};
pub struct Regex {
pub(crate) meta: meta::Regex,
pub(crate) pattern: Arc<str>,
pub(crate) syntaxc: Arc<regex_automata::util::syntax::Config>,
Comment thread
dolezvo1 marked this conversation as resolved.
Outdated
}

impl core::fmt::Display for Regex {
Expand Down Expand Up @@ -1276,6 +1277,31 @@ impl Regex {
&self.pattern
}

/// Compares the source strings and compile options of two regexes,
Comment thread
dolezvo1 marked this conversation as resolved.
Outdated
/// returning true iff both are equal.
/// Such result implies equivalence of the regexes, but the contrary
/// provides no information about their equivalence.
///
/// # Example
///
/// ```
/// use regex::{Regex, RegexBuilder};
///
/// let r1 = Regex::new(r"a+").unwrap();
/// let r2 = Regex::new(r"aa*").unwrap();
/// assert!(!r1.source_and_options_equals(&r2));
///
/// let r3 = RegexBuilder::new(r"a+")
/// .case_insensitive(true)
/// .build()
/// .unwrap();
/// assert!(!r1.source_and_options_equals(&r3));
/// ```
#[inline]
pub fn source_and_options_equals(&self, other: &Self) -> bool {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This naming will need work. I don't like how it is right now. But I don't have the spoons to come up with a better one at the moment.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I think you're right that the name could be improved, but I'm not quite sure how. The _equals part I believe should stay. source_and_options could perhaps be replaced by compile_inputs or regex_info, but I'm not sure whether that is better. Replacing source by str or pattern could also make sense.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@BurntSushi Hello, did you happen to have any ideas about a better name?

self.pattern == other.pattern && self.syntaxc == other.syntaxc
}

/// Returns an iterator over the capture names in this regex.
///
/// The iterator returned yields elements of type `Option<&str>`. That is,
Expand Down
Loading