aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-class-map-generator
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-17 06:43:52 +0900
committernsfisis <nsfisis@gmail.com>2026-08-17 06:43:52 +0900
commit52a0665acbbe5bf5b8c6875118fb9cdf7952453b (patch)
tree7302b0b87f7b0d406126da734ef82c80abf64204 /crates/shirabe-class-map-generator
parentc81e9f89d9e4388f36a4427bbaa95eced659d2ce (diff)
downloadphp-shirabe-52a0665acbbe5bf5b8c6875118fb9cdf7952453b.tar.gz
php-shirabe-52a0665acbbe5bf5b8c6875118fb9cdf7952453b.tar.zst
php-shirabe-52a0665acbbe5bf5b8c6875118fb9cdf7952453b.zip
refactor(preg): wrap the preg_* $matches maps in newtypes
The five IndexMap shapes that the preg_* functions and Preg fill in are now distinct types generated by preg_match_map!, so a matches map no longer interchanges with any other map of the same key and value type. Index<usize> is kept alongside Index<&Q> because call sites such as config_command and event_dispatcher reach for a group by its position in the map rather than by its capture key.
Diffstat (limited to 'crates/shirabe-class-map-generator')
-rw-r--r--crates/shirabe-class-map-generator/src/class_map_generator.rs5
-rw-r--r--crates/shirabe-class-map-generator/src/php_file_cleaner.rs10
-rw-r--r--crates/shirabe-class-map-generator/src/php_file_parser.rs5
3 files changed, 9 insertions, 11 deletions
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 2207416b..478781c8 100644
--- a/crates/shirabe-class-map-generator/src/class_map_generator.rs
+++ b/crates/shirabe-class-map-generator/src/class_map_generator.rs
@@ -3,8 +3,7 @@
use crate::class_map::ClassMap;
use crate::file_list::FileList;
use crate::php_file_parser::PhpFileParser;
-use indexmap::indexmap;
-use shirabe_pcre::{CaptureKey, Preg};
+use shirabe_pcre::{CaptureKey, Preg, PregMatchedGroups};
use shirabe_php_shim::{
InvalidArgumentException, LogicException, PATHINFO_EXTENSION, RuntimeException, explode,
getcwd, implode, is_dir, is_file, pathinfo, php_regex, preg_quote, realpath, str_replace,
@@ -348,7 +347,7 @@ impl ClassMapGenerator {
}
// extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive:
- let mut r#match: indexmap::IndexMap<_, _> = indexmap![];
+ let mut r#match = PregMatchedGroups::new();
if Preg::is_match3(
php_regex!(r"{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix"),
&path,
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 c5952389..78041061 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};
+use shirabe_pcre::{CaptureKey, Preg, PregMatchedGroups};
use std::sync::Mutex;
#[derive(Debug, Clone)]
@@ -97,7 +97,7 @@ impl PhpFileCleaner {
}
if char == '<' && self.peek('<') {
- let mut r#match: IndexMap<CaptureKey, String> = IndexMap::new();
+ let mut r#match = PregMatchedGroups::new();
// Regex pattern compatibility:
// PHP matches `<<<`, an optional quote, the identifier, then requires the
// closing quote to be the exact same character via `\1`. The `regex` crate has
@@ -144,7 +144,7 @@ impl PhpFileCleaner {
let end = self.index + entry.length;
if end <= self.len && self.contents[self.index..end] == entry.name {
let offset = if self.index > 0 { self.index - 1 } else { 0 };
- let mut r#match: IndexMap<CaptureKey, String> = IndexMap::new();
+ let mut r#match = PregMatchedGroups::new();
if Preg::is_match4(
&entry.pattern,
&self.contents,
@@ -164,7 +164,7 @@ impl PhpFileCleaner {
self.index += 1;
let rest_pattern = REST_PATTERN.lock().unwrap().clone();
if let Some(rest_pattern) = rest_pattern {
- let mut r#match: IndexMap<CaptureKey, String> = IndexMap::new();
+ let mut r#match = PregMatchedGroups::new();
if self.r#match(&rest_pattern, Some(&mut r#match)) {
let m0 = r#match
.get(&CaptureKey::ByIndex(0))
@@ -292,7 +292,7 @@ impl PhpFileCleaner {
self.index + 1 < self.len && self.contents.as_bytes()[self.index + 1] as char == char
}
- fn r#match(&self, regex: &str, r#match: Option<&mut IndexMap<CaptureKey, String>>) -> bool {
+ fn r#match(&self, regex: &str, r#match: Option<&mut PregMatchedGroups>) -> bool {
Preg::is_match4(regex, &self.contents, r#match, self.index)
}
}
diff --git a/crates/shirabe-class-map-generator/src/php_file_parser.rs b/crates/shirabe-class-map-generator/src/php_file_parser.rs
index b35988fb..a1207140 100644
--- a/crates/shirabe-class-map-generator/src/php_file_parser.rs
+++ b/crates/shirabe-class-map-generator/src/php_file_parser.rs
@@ -1,8 +1,7 @@
//! ref: composer/vendor/composer/class-map-generator/src/PhpFileParser.php
use crate::php_file_cleaner::PhpFileCleaner;
-use indexmap::IndexMap;
-use shirabe_pcre::{CaptureKey, Preg};
+use shirabe_pcre::{CaptureKey, Preg, PregMatchesAll};
use shirabe_php_shim::{
PHP_EOL, RuntimeException, file_exists, file_get_contents, function_exists, is_file,
is_readable, ltrim, php_strip_whitespace, str_replace_array, strrpos, substr, trim,
@@ -85,7 +84,7 @@ impl PhpFileParser {
}}ix",
et = extra_types
);
- let mut matches: IndexMap<_, _> = IndexMap::new();
+ let mut matches = PregMatchesAll::new();
Preg::match_all2(&pattern2, &contents, &mut matches);
let mut classes = vec![];