aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-18 01:56:31 +0900
committernsfisis <nsfisis@gmail.com>2026-08-18 01:56:32 +0900
commit5114a8199a87c9e5584d92848e95deba22b73e98 (patch)
tree596b422e5a37ad72e522978b6d456c0ff686d1cc /crates/shirabe
parent79b504e55cd4c4d1da102c3a076dc2a2e1edcd65 (diff)
downloadphp-shirabe-5114a8199a87c9e5584d92848e95deba22b73e98.tar.gz
php-shirabe-5114a8199a87c9e5584d92848e95deba22b73e98.tar.zst
php-shirabe-5114a8199a87c9e5584d92848e95deba22b73e98.zip
refactor(preg): back PregMatches with regex::Captures
PregMatches was an IndexMap of owned Strings copied out of the match, so every preg_match2/preg_replace_callback call allocated a String per capture group (twice over for a named group) whether or not the caller read it. It now wraps the regex::Captures itself, held alongside the pattern it came from so groups stay reachable by both their named and their numbered form, and hands out &str borrowed from the subject. The subject's lifetime becomes a parameter of the type. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe')
-rw-r--r--crates/shirabe/src/command/config_command.rs40
-rw-r--r--crates/shirabe/src/console/application.rs4
-rw-r--r--crates/shirabe/src/event_dispatcher/event_dispatcher.rs22
3 files changed, 48 insertions, 18 deletions
diff --git a/crates/shirabe/src/command/config_command.rs b/crates/shirabe/src/command/config_command.rs
index 86375f49..35cd2ab6 100644
--- a/crates/shirabe/src/command/config_command.rs
+++ b/crates/shirabe/src/command/config_command.rs
@@ -1038,7 +1038,7 @@ impl Command for ConfigCommand {
.borrow_mut()
.as_mut()
.unwrap()
- .remove_repository(&matches[1]);
+ .remove_repository(matches.get(&CaptureKey::ByIndex(1)).unwrap());
return Ok(0);
}
@@ -1052,7 +1052,7 @@ impl Command for ConfigCommand {
.as_mut()
.unwrap()
.add_repository(
- &matches[1],
+ matches.get(&CaptureKey::ByIndex(1)).unwrap(),
PhpMixed::Array(repo),
input.borrow().get_option("append")?.as_bool() == Some(true),
);
@@ -1072,7 +1072,7 @@ impl Command for ConfigCommand {
.as_mut()
.unwrap()
.add_repository(
- &matches[1],
+ matches.get(&CaptureKey::ByIndex(1)).unwrap(),
PhpMixed::Bool(false),
input.borrow().get_option("append")?.as_bool() == Some(true),
);
@@ -1086,7 +1086,7 @@ impl Command for ConfigCommand {
.as_mut()
.unwrap()
.add_repository(
- &matches[1],
+ matches.get(&CaptureKey::ByIndex(1)).unwrap(),
value,
input.borrow().get_option("append")?.as_bool() == Some(true),
);
@@ -1334,18 +1334,30 @@ impl Command for ConfigCommand {
.borrow_mut()
.as_mut()
.unwrap()
- .remove_config_setting(&format!("{}.{}", matches[1], matches[2]));
+ .remove_config_setting(&format!(
+ "{}.{}",
+ matches.get(&CaptureKey::ByIndex(1)).unwrap(),
+ matches.get(&CaptureKey::ByIndex(2)).unwrap()
+ ));
self.config_source
.borrow_mut()
.as_mut()
.unwrap()
- .remove_config_setting(&format!("{}.{}", matches[1], matches[2]));
+ .remove_config_setting(&format!(
+ "{}.{}",
+ matches.get(&CaptureKey::ByIndex(1)).unwrap(),
+ matches.get(&CaptureKey::ByIndex(2)).unwrap()
+ ));
return Ok(0);
}
- let key = format!("{}.{}", matches[1], matches[2]);
- if matches[1] == "bitbucket-oauth" {
+ let key = format!(
+ "{}.{}",
+ matches.get(&CaptureKey::ByIndex(1)).unwrap(),
+ matches.get(&CaptureKey::ByIndex(2)).unwrap()
+ );
+ if matches.get(&CaptureKey::ByIndex(1)).unwrap() == "bitbucket-oauth" {
if 2 != values.len() {
return Err(RuntimeException::new(format!(
"Expected two arguments (consumer-key, consumer-secret), got {}",
@@ -1372,7 +1384,9 @@ impl Command for ConfigCommand {
.as_mut()
.unwrap()
.add_config_setting(&key, PhpMixed::Array(obj));
- } else if matches[1] == "gitlab-token" && 2 == values.len() {
+ } else if matches.get(&CaptureKey::ByIndex(1)).unwrap() == "gitlab-token"
+ && 2 == values.len()
+ {
self.config_source
.borrow_mut()
.as_mut()
@@ -1387,7 +1401,7 @@ impl Command for ConfigCommand {
.unwrap()
.add_config_setting(&key, PhpMixed::Array(obj));
} else if matches!(
- matches[1].as_str(),
+ matches.get(&CaptureKey::ByIndex(1)).unwrap().as_str(),
"github-oauth" | "gitlab-oauth" | "gitlab-token" | "bearer"
) {
if 1 != values.len() {
@@ -1406,7 +1420,7 @@ impl Command for ConfigCommand {
.as_mut()
.unwrap()
.add_config_setting(&key, PhpMixed::String(values[0].clone()));
- } else if matches[1] == "http-basic" {
+ } else if matches.get(&CaptureKey::ByIndex(1)).unwrap() == "http-basic" {
if 2 != values.len() {
return Err(RuntimeException::new(format!(
"Expected two arguments (username, password), got {}",
@@ -1427,7 +1441,7 @@ impl Command for ConfigCommand {
.as_mut()
.unwrap()
.add_config_setting(&key, PhpMixed::Array(obj));
- } else if matches[1] == "custom-headers" {
+ } else if matches.get(&CaptureKey::ByIndex(1)).unwrap() == "custom-headers" {
if values.is_empty() {
return Err(RuntimeException::new(
"Expected at least one argument (header), got none".to_string(),
@@ -1468,7 +1482,7 @@ impl Command for ConfigCommand {
.as_mut()
.unwrap()
.add_config_setting(&key, PhpMixed::List(formatted_headers));
- } else if matches[1] == "forgejo-token" {
+ } else if matches.get(&CaptureKey::ByIndex(1)).unwrap() == "forgejo-token" {
if 2 != values.len() {
return Err(RuntimeException::new(format!(
"Expected two arguments (username, access token), got {}",
diff --git a/crates/shirabe/src/console/application.rs b/crates/shirabe/src/console/application.rs
index 5385f790..ca645635 100644
--- a/crates/shirabe/src/console/application.rs
+++ b/crates/shirabe/src/console/application.rs
@@ -1792,8 +1792,8 @@ impl Application {
let mut offset = 0i64;
while let Some(m) = preg_match2(php_regex!(r"/.{1,10000}/u"), &utf8_string, offset as usize)
{
- let m0 = m[&shirabe_php_shim::CaptureKey::ByIndex(0)]
- .as_deref()
+ let m0 = m
+ .get(&shirabe_php_shim::CaptureKey::ByIndex(0))
.unwrap_or("");
offset += shirabe_php_shim::strlen(m0);
diff --git a/crates/shirabe/src/event_dispatcher/event_dispatcher.rs b/crates/shirabe/src/event_dispatcher/event_dispatcher.rs
index f03f82db..6cc1670d 100644
--- a/crates/shirabe/src/event_dispatcher/event_dispatcher.rs
+++ b/crates/shirabe/src/event_dispatcher/event_dispatcher.rs
@@ -956,7 +956,13 @@ try {{
if Platform::is_windows() {
path_and_args = Preg::replace_callback(
php_regex!("{^\\S+}"),
- |m| str_replace("/", "\\", &m[0]),
+ |m| {
+ str_replace(
+ "/",
+ "\\",
+ m.get(&CaptureKey::ByIndex(0)).unwrap(),
+ )
+ },
&path_and_args,
);
}
@@ -985,7 +991,11 @@ try {{
path_and_args = format!(
"{}{}",
path_to_exec,
- substr(&path_and_args, strlen(&m[0]), None)
+ substr(
+ &path_and_args,
+ strlen(m.get(&CaptureKey::ByIndex(0)).unwrap()),
+ None
+ )
);
}
}
@@ -1001,7 +1011,13 @@ try {{
if Platform::is_windows() {
exec = Preg::replace_callback(
php_regex!("{^\\S+}"),
- |m| str_replace("/", "\\", &m[0]),
+ |m| {
+ str_replace(
+ "/",
+ "\\",
+ m.get(&CaptureKey::ByIndex(0)).unwrap(),
+ )
+ },
&exec,
);
}