From cb77f7c7076aa4bac3e6aaa1c164cf9c1d449ddc Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 18 Aug 2026 01:57:02 +0900 Subject: refactor(preg): fold preg_match into preg_match2 preg_match copied every group into a Vec> while preg_match2 handed back the borrowed captures. They now differ only in the offset argument, so preg_match delegates with offset 0 and its callers read groups through PregMatches::get. Going through preg_match2 also makes preg_match honour the PCRE A modifier, which it used to ignore; no caller passes such a pattern. VersionParser::manipulate_version_string takes an index accessor instead of a slice, and VersionParser::normalize matches against a copy of the subject because the captures outlive the assignments to $version. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe-php-shim/src/preg.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'crates/shirabe-php-shim') diff --git a/crates/shirabe-php-shim/src/preg.rs b/crates/shirabe-php-shim/src/preg.rs index 202510e9..d8bdcc10 100644 --- a/crates/shirabe-php-shim/src/preg.rs +++ b/crates/shirabe-php-shim/src/preg.rs @@ -149,20 +149,12 @@ pub fn preg_quote(str: &str, delimiter: Option) -> String { out } -// Returns None if the pattern did not match; otherwise index 0 is the full match and 1.. the -// capture groups. A group that did not participate is None. -pub fn preg_match(pattern: impl PregPattern, subject: &str) -> Option>> { - let __resolved = pattern.resolve(); - let (re, _anchored) = __resolved.parts(); - let caps = re.captures(subject)?; - Some( - (0..caps.len()) - .map(|g| caps.get(g).map(|m| m.as_str().to_string())) - .collect(), - ) +// Returns None if the pattern did not match; otherwise the match's capture groups. +pub fn preg_match<'h>(pattern: impl PregPattern, subject: &'h str) -> Option> { + preg_match2(pattern, subject, 0) } -// Returns None if the pattern did not match; otherwise the match's capture groups. +// `preg_match` with PHP's `$offset` argument: the search starts at byte offset `offset`. pub fn preg_match2<'h>( pattern: impl PregPattern, subject: &'h str, -- cgit v1.3.1-4-g156e