From 530d085d4f3e19f94ac3cf8f8ac3b17000214b2e Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 18 Aug 2026 01:57:02 +0900 Subject: refactor(pcre): inline Preg into its call sites and drop the crate Preg had shed everything it owned: after the last few rounds its methods were one-line forwards to the shim's preg_*(), differing only in a default argument or a wrapper the caller unwrapped anyway. The 460 call sites now name the shim function, and shirabe-pcre is gone from the workspace along with its LICENSE entry. The forwards expand as they read: isMatch becomes preg_match2(.., 0).is_some() (is_none() where PHP negates it), isMatch3 and match3 drop the .is_some(), matchAll counts through preg_match_all2(..).occurrence_count(), and replace4/replace5 spell out the limit and count arguments preg_replace2 takes. Callbacks are the one place the shapes differ: preg_replace_callback carries an error out of the callback, so the fourteen infallible closures wrap their result in Ok() and expect() it back. Config::process() is the fifteenth, and it drops the `error` cell it captured to smuggle a failure past a closure that could only return a String. The `?` in the closure now carries it, which is what the PHP does -- a throw from the callback leaves preg_replace_callback at the failing match rather than running the remaining replacements and reporting the last error. The module doc that explained why composer/pcre's exceptions and *StrictGroups() variants have no counterpart moves to the shim's preg module, where the functions it describes live. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe-class-map-generator/Cargo.toml | 1 - .../shirabe-class-map-generator/src/class_map.rs | 5 ++-- .../src/class_map_generator.rs | 29 ++++++++++++---------- .../src/php_file_cleaner.rs | 6 ++--- .../src/php_file_parser.rs | 10 ++++---- 5 files changed, 26 insertions(+), 25 deletions(-) (limited to 'crates/shirabe-class-map-generator') diff --git a/crates/shirabe-class-map-generator/Cargo.toml b/crates/shirabe-class-map-generator/Cargo.toml index 4d66ac00..73ad1901 100644 --- a/crates/shirabe-class-map-generator/Cargo.toml +++ b/crates/shirabe-class-map-generator/Cargo.toml @@ -8,7 +8,6 @@ repository.workspace = true license.workspace = true [dependencies] -shirabe-pcre.workspace = true shirabe-php-rpc.workspace = true shirabe-php-shim.workspace = true shirabe-symfony-finder.workspace = true diff --git a/crates/shirabe-class-map-generator/src/class_map.rs b/crates/shirabe-class-map-generator/src/class_map.rs index 4eb6ae48..982d8494 100644 --- a/crates/shirabe-class-map-generator/src/class_map.rs +++ b/crates/shirabe-class-map-generator/src/class_map.rs @@ -1,8 +1,7 @@ //! ref: composer/vendor/composer/class-map-generator/src/ClassMap.php use indexmap::IndexMap; -use shirabe_pcre::Preg; -use shirabe_php_shim::{OutOfBoundsException, rtrim, strpos, strtr}; +use shirabe_php_shim::{OutOfBoundsException, preg_match2, rtrim, strpos, strtr}; #[derive(Debug, Clone)] pub struct PsrViolationEntry { @@ -67,7 +66,7 @@ impl ClassMap { for (class, paths) in &self.ambiguous_classes { let paths: Vec = paths .iter() - .filter(|path| !Preg::is_match(duplicates_filter, &strtr(path, "\\", "/"))) + .filter(|path| preg_match2(duplicates_filter, &strtr(path, "\\", "/"), 0).is_none()) .cloned() .collect(); if !paths.is_empty() { 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 0ee9d992..c4130cff 100644 --- a/crates/shirabe-class-map-generator/src/class_map_generator.rs +++ b/crates/shirabe-class-map-generator/src/class_map_generator.rs @@ -3,11 +3,11 @@ use crate::class_map::ClassMap; use crate::file_list::FileList; use crate::php_file_parser::PhpFileParser; -use shirabe_pcre::Preg; use shirabe_php_shim::{ InvalidArgumentException, LogicException, PATHINFO_EXTENSION, RuntimeException, explode, - getcwd, implode, is_dir, is_file, pathinfo, php_regex, preg_quote, realpath, str_replace, - stream_get_wrappers, strlen, strpos, strrpos, strtr, substr, + getcwd, implode, is_dir, is_file, pathinfo, php_regex, preg_match2, preg_quote, preg_replace, + preg_replace_callback, realpath, str_replace, stream_get_wrappers, strlen, strpos, strrpos, + strtr, substr, }; use shirabe_symfony_finder::Finder; use std::path::PathBuf; @@ -134,7 +134,8 @@ impl ClassMapGenerator { continue; } - let is_stream_wrapper_path = Preg::is_match(&self.stream_wrappers_regex, &file_path); + let is_stream_wrapper_path = + preg_match2(&self.stream_wrappers_regex, &file_path, 0).is_some(); if !Self::is_absolute_path(&file_path) && !is_stream_wrapper_path { file_path = format!("{}/{}", cwd, file_path); file_path = Self::normalize_path(&file_path); @@ -146,7 +147,7 @@ impl ClassMapGenerator { // optional leading group `(^|[^:])` that is re-emitted in the replacement. Slash runs // are always separated by path-segment characters, so consuming the single preceding // char never prevents an adjacent run from matching. - file_path = Preg::replace(php_regex!(r"{(^|[^:])[\\/]{2,}}"), "${1}/", &file_path); + file_path = preg_replace(php_regex!(r"{(^|[^:])[\\/]{2,}}"), "${1}/", &file_path); } if file_path.is_empty() { @@ -182,11 +183,11 @@ impl ClassMapGenerator { // check the realpath of the file against the excluded paths as the path might be a symlink and the excluded path is realpath'd so symlink are resolved if let Some(ref excluded) = excluded { - if Preg::is_match(excluded, &strtr(&real_path, "\\", "/")) { + if preg_match2(excluded, &strtr(&real_path, "\\", "/"), 0).is_some() { continue; } // check non-realpath of file for directories symlink in project dir - if Preg::is_match(excluded, &strtr(&file_path, "\\", "/")) { + if preg_match2(excluded, &strtr(&file_path, "\\", "/"), 0).is_some() { continue; } } @@ -297,12 +298,12 @@ impl ClassMapGenerator { None => cwd_str, }; let cwd = Self::normalize_path(&cwd); - let short_path = Preg::replace( + let short_path = preg_replace( format!("{{^{}}}", preg_quote(&cwd, None)), ".", &Self::normalize_path(file_path), ); - let short_base_path = Preg::replace( + let short_base_path = preg_replace( format!("{{^{}}}", preg_quote(&cwd, None)), ".", &Self::normalize_path(base_path), @@ -347,9 +348,10 @@ impl ClassMapGenerator { } // extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive: - if let Some(r#match) = Preg::is_match3( + if let Some(r#match) = preg_match2( php_regex!(r"{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix"), &path, + 0, ) { prefix = r#match.get(1).unwrap_or_default().to_string(); path = substr(&path, strlen(&prefix), None); @@ -372,11 +374,12 @@ impl ClassMapGenerator { } // ensure c: is normalized to C: - let prefix = Preg::replace_callback( + let prefix = preg_replace_callback( php_regex!(r"{(?:^|://)[a-z]:$}i"), - |m| m.get(0).unwrap_or_default().to_string().to_uppercase(), + |m| Ok(m.get(0).unwrap_or_default().to_string().to_uppercase()), &prefix, - ); + ) + .expect("the replacement callback cannot fail"); format!("{}{}{}", prefix, absolute, parts.join("/")) } 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 18b051c9..78cee261 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::{Preg, PregMatches}; +use shirabe_php_shim::{PregMatches, preg_match2}; use std::sync::Mutex; #[derive(Debug, Clone)] @@ -147,7 +147,7 @@ impl PhpFileCleaner { if end <= self.len && self.contents[self.index..end] == entry.name { let offset = if self.index > 0 { self.index - 1 } else { 0 }; if let Some(r#match) = - Preg::is_match4(&entry.pattern, &self.contents, offset) + preg_match2(&entry.pattern, &self.contents, offset) { return clean + r#match.get(0).unwrap_or(""); } @@ -283,6 +283,6 @@ impl PhpFileCleaner { } fn r#match(&self, regex: &str) -> Option> { - Preg::is_match4(regex, &self.contents, self.index) + preg_match2(regex, &self.contents, 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 e05f4f1a..0ed78c7f 100644 --- a/crates/shirabe-class-map-generator/src/php_file_parser.rs +++ b/crates/shirabe-class-map-generator/src/php_file_parser.rs @@ -1,10 +1,10 @@ //! ref: composer/vendor/composer/class-map-generator/src/PhpFileParser.php use crate::php_file_cleaner::PhpFileCleaner; -use shirabe_pcre::{CaptureKey, Preg}; 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, + CaptureKey, PHP_EOL, RuntimeException, file_exists, file_get_contents, function_exists, + is_file, is_readable, ltrim, php_strip_whitespace, preg_match_all2, str_replace_array, strrpos, + substr, trim, }; use std::sync::OnceLock; @@ -58,7 +58,7 @@ impl PhpFileParser { // return early if there is no chance of matching anything in this file let pattern = format!("{{\\b(?:class|interface|trait{})\\s}}i", extra_types); - let max_matches = Preg::match_all(&pattern, &contents); + let max_matches = preg_match_all2(&pattern, &contents).occurrence_count(); if max_matches == 0 { return Ok(vec![]); } @@ -84,7 +84,7 @@ impl PhpFileParser { }}ix", et = extra_types ); - let matches = Preg::match_all2(&pattern2, &contents); + let matches = preg_match_all2(&pattern2, &contents); let mut classes = vec![]; let mut namespace = String::new(); -- cgit v1.3.1-4-g156e