From 844097edf44bf1424d28e2d5fbefda90c1c8f46c Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 18 Aug 2026 01:57:02 +0900 Subject: refactor(pcre): hand back the match instead of copying it out Preg::match4 and Preg::replace_callback gave callers a PregMatchedGroups: an IndexMap rebuilt from the match with an owned String per group, plus a second String for a named group's name key. That is the copy PregMatches shed when it started wrapping regex::Captures, reinstated one layer up -- and nearly every regex call in the tree goes through Preg rather than the shim's preg_* directly, so almost nothing saw the borrow. PregMatchedGroups existed only to drop the null (unmatched) groups the old PregMatches held as Option values. PregMatches::get reports a non-participating group as None on its own, so the two read alike and the type collapses into it. Call sites still reach groups through get(&CaptureKey::ByIndex(N)); what changes is that the value arrives as a &str borrowed from the subject, which the signatures now carry as a lifetime. Three places needed the borrow reckoned with rather than a mechanical rewrite: PhpFileCleaner::clean and Problem::get_messages read their groups out before mutating what the match borrows, and Git::get_authentication_failure names the lifetime of its url argument, which the result borrows instead of self. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/class_map_generator.rs | 6 +++--- .../src/php_file_cleaner.rs | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) (limited to 'crates/shirabe-class-map-generator/src') diff --git a/crates/shirabe-class-map-generator/src/class_map_generator.rs b/crates/shirabe-class-map-generator/src/class_map_generator.rs index 670f8448..5b745b80 100644 --- a/crates/shirabe-class-map-generator/src/class_map_generator.rs +++ b/crates/shirabe-class-map-generator/src/class_map_generator.rs @@ -353,8 +353,8 @@ impl ClassMapGenerator { ) { prefix = r#match .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); path = substr(&path, strlen(&prefix), None); } @@ -379,8 +379,8 @@ impl ClassMapGenerator { php_regex!(r"{(?:^|://)[a-z]:$}i"), |m| { m.get(&CaptureKey::ByIndex(0)) - .cloned() .unwrap_or_default() + .to_string() .to_uppercase() }, &prefix, diff --git a/crates/shirabe-class-map-generator/src/php_file_cleaner.rs b/crates/shirabe-class-map-generator/src/php_file_cleaner.rs index 5731dbd4..8b60b4db 100644 --- a/crates/shirabe-class-map-generator/src/php_file_cleaner.rs +++ b/crates/shirabe-class-map-generator/src/php_file_cleaner.rs @@ -1,7 +1,7 @@ //! ref: composer/vendor/composer/class-map-generator/src/PhpFileCleaner.php use indexmap::IndexMap; -use shirabe_pcre::{CaptureKey, Preg, PregMatchedGroups}; +use shirabe_pcre::{CaptureKey, Preg, PregMatches}; use std::sync::Mutex; #[derive(Debug, Clone)] @@ -105,16 +105,20 @@ impl PhpFileCleaner { if let Some(r#match) = self.r#match( r#"{<<<[ \t]*(?:"([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)"|'([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)'|([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*))(?:\r\n|\n|\r)}A"#, ) { - self.index += r#match.get(&CaptureKey::ByIndex(0)).map(|s| s.len()).unwrap_or(0); + let matched_len = r#match + .get(&CaptureKey::ByIndex(0)) + .map(|s| s.len()) + .unwrap_or(0); let delimiter = [1, 2, 3] .iter() .find_map(|i| { r#match .get(&CaptureKey::ByIndex(*i)) .filter(|s| !s.is_empty()) - .cloned() + .map(str::to_string) }) .unwrap_or_default(); + self.index += matched_len; self.skip_heredoc(&delimiter); clean.push_str("null"); continue; @@ -145,11 +149,7 @@ impl PhpFileCleaner { if let Some(r#match) = Preg::is_match4(&entry.pattern, &self.contents, offset) { - return clean - + r#match - .get(&CaptureKey::ByIndex(0)) - .map(|s| s.as_str()) - .unwrap_or(""); + return clean + r#match.get(&CaptureKey::ByIndex(0)).unwrap_or(""); } } } @@ -161,8 +161,8 @@ impl PhpFileCleaner { if let Some(r#match) = self.r#match(&rest_pattern) { let m0 = r#match .get(&CaptureKey::ByIndex(0)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); clean.push(char); clean.push_str(&m0); self.index += m0.len(); @@ -285,7 +285,7 @@ impl PhpFileCleaner { self.index + 1 < self.len && self.contents.as_bytes()[self.index + 1] as char == char } - fn r#match(&self, regex: &str) -> Option { + fn r#match(&self, regex: &str) -> Option> { Preg::is_match4(regex, &self.contents, self.index) } } -- cgit v1.3.1-4-g156e