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 | 7061051e19dc0ccbbdb694f030aedb05802287e4 (patch) | |
| tree | 50865f241ee5837312c80b1d007b21319b70950a /crates/shirabe-class-map-generator/src/php_file_cleaner.rs | |
| parent | a01330572b985007acae339817d171a6505bb16b (diff) | |
| download | php-shirabe-7061051e19dc0ccbbdb694f030aedb05802287e4.tar.gz php-shirabe-7061051e19dc0ccbbdb694f030aedb05802287e4.tar.zst php-shirabe-7061051e19dc0ccbbdb694f030aedb05802287e4.zip | |
refactor(preg): anchor A patterns at the call site
The shim carried the PCRE A (anchored) modifier alongside every compiled
pattern so preg_match2 could honour it by searching the sub-slice at the
offset. Only two call sites ever passed such a pattern, and each can cut
that slice itself, so the flag is gone from the cache, ResolvedPattern,
PregPattern and the php_regex! macro, and a pattern still carrying A is
now rejected rather than silently searched unanchored.
StringInput::tokenize and PhpFileCleaner::match search from their cursor
with a `^`-prefixed pattern instead. The rule is written down in
docs/dev/regex-porting.md.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-class-map-generator/src/php_file_cleaner.rs')
| -rw-r--r-- | crates/shirabe-class-map-generator/src/php_file_cleaner.rs | 13 |
1 files changed, 9 insertions, 4 deletions
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 78cee261..cfd8f77f 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_php_shim::{PregMatches, preg_match2}; +use shirabe_php_shim::{PregMatches, preg_match, preg_match2}; use std::sync::Mutex; #[derive(Debug, Clone)] @@ -52,7 +52,7 @@ impl PhpFileCleaner { } let keys: String = type_config.keys().collect(); - let rest_pattern = format!("{{[^?\"'</{}]+}}A", keys); + let rest_pattern = format!("{{^[^?\"'</{}]+}}", keys); *REST_PATTERN.lock().unwrap() = Some(rest_pattern); *TYPE_CONFIG.lock().unwrap() = Some(type_config); @@ -103,7 +103,7 @@ impl PhpFileCleaner { // no backreferences, so the three quote states (none, `'`, `"`) are expanded // into separate alternatives, each capturing the identifier in its own group. if let Some(r#match) = self.r#match( - r#"{<<<[ \t]*(?:"([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)"|'([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)'|([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*))(?:\r\n|\n|\r)}A"#, + r#"{^<<<[ \t]*(?:"([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)"|'([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)'|([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*))(?:\r\n|\n|\r)}"#, ) { let matched_len = r#match .get(0) @@ -282,7 +282,12 @@ impl PhpFileCleaner { self.index + 1 < self.len && self.contents.as_bytes()[self.index + 1] as char == char } + // Regex pattern compatibility: + // PHP runs `$regex` anchored (`A`) at `$this->index`, so it must match starting exactly there. + // The `regex` crate anchors only at the head of the haystack, so the search runs over the part + // of the contents that begins at the index and the patterns carry a leading `^` instead of the + // `A` modifier. fn r#match(&self, regex: &str) -> Option<PregMatches<'_>> { - preg_match2(regex, &self.contents, self.index) + preg_match(regex, &self.contents[self.index..]) } } |
