Skip to content

Commit 175e003

Browse files
committed
syntax: rename 'captures_len' to 'explicit_captures_len'
And do the same for 'static_captures_len'. The motivation for this is that the top-level Regex API had equivalently named methods 'captures_len' and 'static_captures_len', except those included the implicit group and were therefore always 1 more than the same APIs on Hir. We distinguish them by renaming the routines on HIR.
1 parent b23e97a commit 175e003

File tree

3 files changed

+63
-55
lines changed

3 files changed

+63
-55
lines changed

regex-syntax/src/hir/mod.rs

+48-40
Original file line numberDiff line numberDiff line change
@@ -1832,8 +1832,8 @@ struct PropertiesI {
18321832
look_set_prefix: LookSet,
18331833
look_set_suffix: LookSet,
18341834
utf8: bool,
1835-
captures_len: usize,
1836-
static_captures_len: Option<usize>,
1835+
explicit_captures_len: usize,
1836+
static_explicit_captures_len: Option<usize>,
18371837
literal: bool,
18381838
alternation_literal: bool,
18391839
}
@@ -1981,14 +1981,14 @@ impl Properties {
19811981
/// ```
19821982
/// use regex_syntax::parse;
19831983
///
1984-
/// assert_eq!(0, parse("a")?.properties().captures_len());
1985-
/// assert_eq!(1, parse("(a)")?.properties().captures_len());
1984+
/// assert_eq!(0, parse("a")?.properties().explicit_captures_len());
1985+
/// assert_eq!(1, parse("(a)")?.properties().explicit_captures_len());
19861986
///
19871987
/// # Ok::<(), Box<dyn std::error::Error>>(())
19881988
/// ```
19891989
#[inline]
1990-
pub fn captures_len(&self) -> usize {
1991-
self.0.captures_len
1990+
pub fn explicit_captures_len(&self) -> usize {
1991+
self.0.explicit_captures_len
19921992
}
19931993

19941994
/// Returns the total number of explicit capturing groups that appear in
@@ -2010,7 +2010,9 @@ impl Properties {
20102010
/// use regex_syntax::parse;
20112011
///
20122012
/// let len = |pattern| {
2013-
/// parse(pattern).map(|h| h.properties().static_captures_len())
2013+
/// parse(pattern).map(|h| {
2014+
/// h.properties().static_explicit_captures_len()
2015+
/// })
20142016
/// };
20152017
///
20162018
/// assert_eq!(Some(0), len("a")?);
@@ -2025,8 +2027,8 @@ impl Properties {
20252027
/// # Ok::<(), Box<dyn std::error::Error>>(())
20262028
/// ```
20272029
#[inline]
2028-
pub fn static_captures_len(&self) -> Option<usize> {
2029-
self.0.static_captures_len
2030+
pub fn static_explicit_captures_len(&self) -> Option<usize> {
2031+
self.0.static_explicit_captures_len
20302032
}
20312033

20322034
/// Return true if and only if this HIR is a simple literal. This is
@@ -2144,8 +2146,8 @@ impl Properties {
21442146
// alternate. If any subsequent alternate has a different number of
21452147
// static capture groups, then we overall have a variation and not a
21462148
// static number of groups.
2147-
let static_captures_len =
2148-
it.peek().and_then(|p| p.borrow().static_captures_len());
2149+
let static_explicit_captures_len =
2150+
it.peek().and_then(|p| p.borrow().static_explicit_captures_len());
21492151
// The base case is an empty alternation, which matches nothing.
21502152
// Note though that empty alternations aren't possible, because the
21512153
// Hir::alternation smart constructor rewrites those as empty character
@@ -2157,8 +2159,8 @@ impl Properties {
21572159
look_set_prefix: fix,
21582160
look_set_suffix: fix,
21592161
utf8: true,
2160-
captures_len: 0,
2161-
static_captures_len,
2162+
explicit_captures_len: 0,
2163+
static_explicit_captures_len,
21622164
literal: false,
21632165
alternation_literal: true,
21642166
};
@@ -2170,10 +2172,13 @@ impl Properties {
21702172
props.look_set_prefix.set_intersect(p.look_set_prefix());
21712173
props.look_set_suffix.set_intersect(p.look_set_suffix());
21722174
props.utf8 = props.utf8 && p.is_utf8();
2173-
props.captures_len =
2174-
props.captures_len.saturating_add(p.captures_len());
2175-
if props.static_captures_len != p.static_captures_len() {
2176-
props.static_captures_len = None;
2175+
props.explicit_captures_len = props
2176+
.explicit_captures_len
2177+
.saturating_add(p.explicit_captures_len());
2178+
if props.static_explicit_captures_len
2179+
!= p.static_explicit_captures_len()
2180+
{
2181+
props.static_explicit_captures_len = None;
21772182
}
21782183
props.alternation_literal =
21792184
props.alternation_literal && p.is_alternation_literal();
@@ -2229,8 +2234,8 @@ impl Properties {
22292234
// were false, for example, then 'a*' would also need to be false
22302235
// since it too can match the empty string.
22312236
utf8: true,
2232-
captures_len: 0,
2233-
static_captures_len: Some(0),
2237+
explicit_captures_len: 0,
2238+
static_explicit_captures_len: Some(0),
22342239
literal: false,
22352240
alternation_literal: false,
22362241
};
@@ -2246,8 +2251,8 @@ impl Properties {
22462251
look_set_prefix: LookSet::empty(),
22472252
look_set_suffix: LookSet::empty(),
22482253
utf8: core::str::from_utf8(&lit.0).is_ok(),
2249-
captures_len: 0,
2250-
static_captures_len: Some(0),
2254+
explicit_captures_len: 0,
2255+
static_explicit_captures_len: Some(0),
22512256
literal: true,
22522257
alternation_literal: true,
22532258
};
@@ -2263,8 +2268,8 @@ impl Properties {
22632268
look_set_prefix: LookSet::empty(),
22642269
look_set_suffix: LookSet::empty(),
22652270
utf8: class.is_utf8(),
2266-
captures_len: 0,
2267-
static_captures_len: Some(0),
2271+
explicit_captures_len: 0,
2272+
static_explicit_captures_len: Some(0),
22682273
literal: false,
22692274
alternation_literal: false,
22702275
};
@@ -2293,8 +2298,8 @@ impl Properties {
22932298
// considered to match invalid UTF-8. That in turn makes this
22942299
// property borderline useless.
22952300
utf8: true,
2296-
captures_len: 0,
2297-
static_captures_len: Some(0),
2301+
explicit_captures_len: 0,
2302+
static_explicit_captures_len: Some(0),
22982303
literal: false,
22992304
alternation_literal: false,
23002305
};
@@ -2321,8 +2326,8 @@ impl Properties {
23212326
look_set_prefix: LookSet::empty(),
23222327
look_set_suffix: LookSet::empty(),
23232328
utf8: p.is_utf8(),
2324-
captures_len: p.captures_len(),
2325-
static_captures_len: p.static_captures_len(),
2329+
explicit_captures_len: p.explicit_captures_len(),
2330+
static_explicit_captures_len: p.static_explicit_captures_len(),
23262331
literal: false,
23272332
alternation_literal: false,
23282333
};
@@ -2338,16 +2343,16 @@ impl Properties {
23382343
// of the repetition. Otherwise, it might change, but only when the
23392344
// repetition can match 0 times.
23402345
if rep.min == 0
2341-
&& inner.static_captures_len.map_or(false, |len| len > 0)
2346+
&& inner.static_explicit_captures_len.map_or(false, |len| len > 0)
23422347
{
23432348
// If we require a match 0 times, then our captures len is
23442349
// guaranteed to be zero. Otherwise, if we *can* match the empty
23452350
// string, then it's impossible to know how many captures will be
23462351
// in the resulting match.
23472352
if rep.max == Some(0) {
2348-
inner.static_captures_len = Some(0);
2353+
inner.static_explicit_captures_len = Some(0);
23492354
} else {
2350-
inner.static_captures_len = None;
2355+
inner.static_explicit_captures_len = None;
23512356
}
23522357
}
23532358
Properties(Box::new(inner))
@@ -2357,9 +2362,9 @@ impl Properties {
23572362
fn capture(capture: &Capture) -> Properties {
23582363
let p = capture.sub.properties();
23592364
Properties(Box::new(PropertiesI {
2360-
captures_len: p.captures_len().saturating_add(1),
2361-
static_captures_len: p
2362-
.static_captures_len()
2365+
explicit_captures_len: p.explicit_captures_len().saturating_add(1),
2366+
static_explicit_captures_len: p
2367+
.static_explicit_captures_len()
23632368
.map(|len| len.saturating_add(1)),
23642369
literal: false,
23652370
alternation_literal: false,
@@ -2380,8 +2385,8 @@ impl Properties {
23802385
look_set_prefix: LookSet::empty(),
23812386
look_set_suffix: LookSet::empty(),
23822387
utf8: true,
2383-
captures_len: 0,
2384-
static_captures_len: Some(0),
2388+
explicit_captures_len: 0,
2389+
static_explicit_captures_len: Some(0),
23852390
literal: true,
23862391
alternation_literal: true,
23872392
};
@@ -2390,11 +2395,14 @@ impl Properties {
23902395
let p = x.properties();
23912396
props.look_set.set_union(p.look_set());
23922397
props.utf8 = props.utf8 && p.is_utf8();
2393-
props.captures_len =
2394-
props.captures_len.saturating_add(p.captures_len());
2395-
props.static_captures_len = p
2396-
.static_captures_len()
2397-
.and_then(|len1| Some((len1, props.static_captures_len?)))
2398+
props.explicit_captures_len = props
2399+
.explicit_captures_len
2400+
.saturating_add(p.explicit_captures_len());
2401+
props.static_explicit_captures_len = p
2402+
.static_explicit_captures_len()
2403+
.and_then(|len1| {
2404+
Some((len1, props.static_explicit_captures_len?))
2405+
})
23982406
.and_then(|(len1, len2)| Some(len1.saturating_add(len2)));
23992407
props.literal = props.literal && p.is_literal();
24002408
props.alternation_literal =

regex-syntax/src/hir/translate.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -3189,24 +3189,24 @@ mod tests {
31893189

31903190
#[test]
31913191
fn analysis_captures_len() {
3192-
assert_eq!(0, props(r"a").captures_len());
3193-
assert_eq!(0, props(r"(?:a)").captures_len());
3194-
assert_eq!(0, props(r"(?i-u:a)").captures_len());
3195-
assert_eq!(0, props(r"(?i-u)a").captures_len());
3196-
assert_eq!(1, props(r"(a)").captures_len());
3197-
assert_eq!(1, props(r"(?P<foo>a)").captures_len());
3198-
assert_eq!(1, props(r"()").captures_len());
3199-
assert_eq!(1, props(r"()a").captures_len());
3200-
assert_eq!(1, props(r"(a)+").captures_len());
3201-
assert_eq!(2, props(r"(a)(b)").captures_len());
3202-
assert_eq!(2, props(r"(a)|(b)").captures_len());
3203-
assert_eq!(2, props(r"((a))").captures_len());
3204-
assert_eq!(1, props(r"([a&&b])").captures_len());
3192+
assert_eq!(0, props(r"a").explicit_captures_len());
3193+
assert_eq!(0, props(r"(?:a)").explicit_captures_len());
3194+
assert_eq!(0, props(r"(?i-u:a)").explicit_captures_len());
3195+
assert_eq!(0, props(r"(?i-u)a").explicit_captures_len());
3196+
assert_eq!(1, props(r"(a)").explicit_captures_len());
3197+
assert_eq!(1, props(r"(?P<foo>a)").explicit_captures_len());
3198+
assert_eq!(1, props(r"()").explicit_captures_len());
3199+
assert_eq!(1, props(r"()a").explicit_captures_len());
3200+
assert_eq!(1, props(r"(a)+").explicit_captures_len());
3201+
assert_eq!(2, props(r"(a)(b)").explicit_captures_len());
3202+
assert_eq!(2, props(r"(a)|(b)").explicit_captures_len());
3203+
assert_eq!(2, props(r"((a))").explicit_captures_len());
3204+
assert_eq!(1, props(r"([a&&b])").explicit_captures_len());
32053205
}
32063206

32073207
#[test]
32083208
fn analysis_static_captures_len() {
3209-
let len = |pattern| props(pattern).static_captures_len();
3209+
let len = |pattern| props(pattern).static_explicit_captures_len();
32103210
assert_eq!(Some(0), len(r""));
32113211
assert_eq!(Some(0), len(r"foo|bar"));
32123212
assert_eq!(None, len(r"(foo)|bar"));

src/compile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl Compiler {
162162
self.compiled.matches = vec![self.insts.len()];
163163
self.push_compiled(Inst::Match(0));
164164
self.compiled.static_captures_len =
165-
expr.properties().static_captures_len();
165+
expr.properties().static_explicit_captures_len();
166166
self.compile_finish()
167167
}
168168

0 commit comments

Comments
 (0)