aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/command/init_command.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-18 01:57:02 +0900
committernsfisis <nsfisis@gmail.com>2026-08-18 01:57:02 +0900
commit6aeda8b237fcbf7a56ca0e8c0fff415477d31d22 (patch)
tree13942695ae0e7c749cfcdb12d5824daf7134c008 /crates/shirabe/src/command/init_command.rs
parentfed0a6e7ac361af9b963c1f62411b1a85478230c (diff)
downloadphp-shirabe-6aeda8b237fcbf7a56ca0e8c0fff415477d31d22.tar.gz
php-shirabe-6aeda8b237fcbf7a56ca0e8c0fff415477d31d22.tar.zst
php-shirabe-6aeda8b237fcbf7a56ca0e8c0fff415477d31d22.zip
refactor(preg): make preg_match_all yield matches per occurrence
PHP's PREG_PATTERN_ORDER is column-oriented, but 7 of the 10 call sites read it row-wise, rebuilding each occurrence by indexing every column at the same offset. Return an iterator of PregMatches instead, which is also what the set-order and offset-capture variants were carrying, so the three functions collapse into one and PregMatchesAll, PregMatchesAllWithOffsets, CaptureKey and preg_match_map! all go away. The offset-capture call sites are served by the new PregMatches get_offset/name_offset accessors. The search stays eager: regex::Captures borrows only the subject, so the matches outlive the pattern resolved for the call, and PHP's preg_match_all is eager too. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/command/init_command.rs')
-rw-r--r--crates/shirabe/src/command/init_command.rs26
1 files changed, 10 insertions, 16 deletions
diff --git a/crates/shirabe/src/command/init_command.rs b/crates/shirabe/src/command/init_command.rs
index 54ebc995..add7c237 100644
--- a/crates/shirabe/src/command/init_command.rs
+++ b/crates/shirabe/src/command/init_command.rs
@@ -21,7 +21,7 @@ use crate::util::Silencer;
use indexmap::IndexMap;
use shirabe_php_shim::Catch as _;
use shirabe_php_shim::{
- CaptureKey, FILE_IGNORE_NEW_LINES, InvalidArgumentException, PHP_EOL, PHP_SERVER, PhpMixed,
+ FILE_IGNORE_NEW_LINES, InvalidArgumentException, PHP_EOL, PHP_SERVER, PhpMixed,
array_flip_strings, array_intersect_key, array_map, basename, empty, explode, file,
file_exists, file_get_contents, file_put_contents, get_current_user, impl_php_class, implode,
is_dir, is_string, php_regex, preg_is_match, preg_match, preg_match_all, preg_quote,
@@ -167,21 +167,15 @@ impl InitCommand {
) == 0
{
*self.git_config.borrow_mut() = Some(IndexMap::new());
- let m = preg_match_all(php_regex!(r"{^([^=]+)=(.*)$}m"), &output);
- if m.occurrence_count() > 0 {
- let keys: Vec<Option<String>> =
- m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default();
- let values: Vec<Option<String>> =
- m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default();
- for (key, value) in keys.iter().zip(values.iter()) {
- self.git_config.borrow_mut().as_mut().unwrap().insert(
- key.clone()
- .expect("group 1 participates whenever the pattern matches"),
- value
- .clone()
- .expect("group 2 participates whenever the pattern matches"),
- );
- }
+ for m in preg_match_all(php_regex!(r"{^([^=]+)=(.*)$}m"), &output) {
+ self.git_config.borrow_mut().as_mut().unwrap().insert(
+ m.get(1)
+ .expect("group 1 participates whenever the pattern matches")
+ .to_string(),
+ m.get(2)
+ .expect("group 2 participates whenever the pattern matches")
+ .to_string(),
+ );
}
return self.git_config.borrow().clone().unwrap_or_default();