diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-18 01:57:02 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-18 01:57:02 +0900 |
| commit | 530d085d4f3e19f94ac3cf8f8ac3b17000214b2e (patch) | |
| tree | b4de2c2443e2bb2cfc692454ac284dc1d2313e59 /crates/shirabe-class-map-generator/src/class_map_generator.rs | |
| parent | 0caac63bacefb9a1f62848636d47fca07f592bba (diff) | |
| download | php-shirabe-530d085d4f3e19f94ac3cf8f8ac3b17000214b2e.tar.gz php-shirabe-530d085d4f3e19f94ac3cf8f8ac3b17000214b2e.tar.zst php-shirabe-530d085d4f3e19f94ac3cf8f8ac3b17000214b2e.zip | |
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) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-class-map-generator/src/class_map_generator.rs')
| -rw-r--r-- | crates/shirabe-class-map-generator/src/class_map_generator.rs | 29 |
1 files changed, 16 insertions, 13 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 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("/")) } |
