From a01330572b985007acae339817d171a6505bb16b Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 18 Aug 2026 01:57:02 +0900 Subject: refactor(preg): drop the Vec-returning preg_match_all The two preg_match_all variants took the same arguments and differed only in what they returned: a Vec of columns, or the named-and-numbered PregMatchesAll. The latter is the one all but two call sites already used, so preg_match_all2 takes over the plain PHP name and the Vec variant goes away. Its remaining readers only ever wanted group 0's column, which they now take through CaptureKey::ByIndex(0); in the formatter this replaces the array_shift that popped that column off the PREG_PATTERN_ORDER array. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe-php-shim/src/preg.rs | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) (limited to 'crates/shirabe-php-shim/src') diff --git a/crates/shirabe-php-shim/src/preg.rs b/crates/shirabe-php-shim/src/preg.rs index d8bdcc10..e84f8463 100644 --- a/crates/shirabe-php-shim/src/preg.rs +++ b/crates/shirabe-php-shim/src/preg.rs @@ -175,24 +175,9 @@ pub fn preg_match2<'h>( Some(PregMatches::new(caps)) } -// PREG_PATTERN_ORDER: the outer vec is indexed by capture group, the inner by -// match occurrence. A non-participating group is reported as None. -pub fn preg_match_all(pattern: impl PregPattern, subject: &str) -> Vec>> { - let __resolved = pattern.resolve(); - let (re, _anchored) = __resolved.parts(); - let group_count = re.captures_len(); - let mut groups: Vec>> = vec![Vec::new(); group_count]; - for caps in re.captures_iter(subject) { - for (g, group) in groups.iter_mut().enumerate() { - group.push(caps.get(g).map(|m| m.as_str().to_string())); - } - } - groups -} - // The number of occurrences the caller would get from PHP's return value is the length of any // one column, as `PregMatchesAll::occurrence_count` reports it. -pub fn preg_match_all2(pattern: impl PregPattern, subject: &str) -> PregMatchesAll { +pub fn preg_match_all(pattern: impl PregPattern, subject: &str) -> PregMatchesAll { let __resolved = pattern.resolve(); let (re, _anchored) = __resolved.parts(); let group_count = re.captures_len(); @@ -473,9 +458,8 @@ fn translate_php_pattern(pattern: &str) -> anyhow::Result<(String, bool)> { .collect(); // PCRE's `A` (PCRE_ANCHORED) modifier requires the match to start exactly at the search offset. - // The `regex` crate has no per-search anchoring, so the offset-based callers - // (`preg_match2`/`preg_match_all2`) honour it by searching a sub-slice that begins at the offset; - // here we only surface the flag. + // The `regex` crate has no per-search anchoring, so `preg_match2` honours it by searching a + // sub-slice that begins at the offset; here we only surface the flag. let anchored = modifiers.contains('A'); let inner = translate_pcre_literals(inner); -- cgit v1.3.1-4-g156e