Skip to content
Closed
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
Prev Previous commit
Removed unnecessary unicode-reliant test and ran fmt.
  • Loading branch information
orlp committed Sep 24, 2022
commit 559606c3af200c7e2236eb055b0c063cf712a2a1
8 changes: 5 additions & 3 deletions regex-syntax/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,9 +579,11 @@ impl Hir {
let x = info.is_alternation_literal() && e.is_literal();
info.set_alternation_literal(x);
}
let mut capture_counts = exprs.iter().map(|e| e.info.static_capture_count);
let mut capture_counts =
exprs.iter().map(|e| e.info.static_capture_count);
let first = capture_counts.next().unwrap_or(Some(0));
info.static_capture_count = capture_counts.fold(first, |a, b| if a == b { a } else { None });
info.static_capture_count = capture_counts
.fold(first, |a, b| if a == b { a } else { None });
Copy link
Member

Choose a reason for hiding this comment

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

It looks like all of this logic can be pushed up into the loop as well.

Hir { kind: HirKind::Alternation(exprs), info }
}
}
Expand Down Expand Up @@ -732,7 +734,7 @@ impl Hir {
pub fn is_alternation_literal(&self) -> bool {
self.info.is_alternation_literal()
}

/// Returns the number of captures groups that would participate in a
/// successful match of this expression. If this number can not be
/// statically determined from the regex this function returns `None`.
Expand Down
3 changes: 2 additions & 1 deletion src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ impl Compiler {
self.compiled.start = dotstar_patch.entry;
}
self.compiled.captures = vec![None];
self.compiled.participating_captures_len = expr.participating_captures_len();
self.compiled.participating_captures_len =
expr.participating_captures_len();
let patch =
self.c_capture(0, expr)?.unwrap_or_else(|| self.next_inst());
if self.compiled.needs_dotstar() {
Expand Down
2 changes: 1 addition & 1 deletion src/re_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ impl Regex {
pub fn captures_len(&self) -> usize {
self.0.capture_names().len()
}

/// Returns the number of participating captures that this regex will
/// return on a successful match. If this number can not be statically
/// determined from the regex this function returns `None`.
Expand Down
2 changes: 1 addition & 1 deletion src/re_unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ impl Regex {
pub fn captures_len(&self) -> usize {
self.0.capture_names().len()
}

/// Returns the number of participating captures that this regex will
/// return on a successful match. If this number can not be statically
/// determined from the regex this function returns `None`.
Expand Down
7 changes: 5 additions & 2 deletions tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,15 @@ fn participating_captures_len() {
("(foo)(?:bar)", Some(1)),
("(foo(?:bar)+)(?:baz(boo))", Some(2)),
("(?P<bar>foo)(?:bar)(bal|loon)", Some(2)),
(r"(?:(\w)(\s))?", None),
(r#"<(a)[^>]+href="([^"]+)"|<(img)[^>]+src="([^"]+)""#, Some(2)),
];
for (test_regex, expected) in tests {
let re = regex!(test_regex);
assert_eq!(re.participating_captures_len(), expected, "for regex {test_regex}");
assert_eq!(
re.participating_captures_len(),
expected,
"for regex {test_regex}"
);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Could you move these tests to regex-syntax/src/hir/translate.rs? That's where all of the existing "analysis" tests are. It also pushes them closer to where they are implemented.


Expand Down