diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-14 13:30:49 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-14 13:30:49 +0900 |
| commit | b7ffae965e39bfd6023994305b97a99c000cbf81 (patch) | |
| tree | 62fbc076f0a0536fe3f86957ebb43cf2126f4abc /crates | |
| parent | 614616d4ceeb78dcf22df3e36a0fceb4c2d110c8 (diff) | |
| download | php-shirabe-b7ffae965e39bfd6023994305b97a99c000cbf81.tar.gz php-shirabe-b7ffae965e39bfd6023994305b97a99c000cbf81.tar.zst php-shirabe-b7ffae965e39bfd6023994305b97a99c000cbf81.zip | |
refactor(pcre): take &mut matches in preg_*2 shim helpers
The optional matches output was always passed Some(&mut ...) at every
call site, so the Option wrapper added no value. Take &mut directly and
inline the former internal helpers.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates')
4 files changed, 27 insertions, 33 deletions
diff --git a/crates/shirabe-external-packages/src/composer/pcre/preg.rs b/crates/shirabe-external-packages/src/composer/pcre/preg.rs index 4034649..72800dd 100644 --- a/crates/shirabe-external-packages/src/composer/pcre/preg.rs +++ b/crates/shirabe-external-packages/src/composer/pcre/preg.rs @@ -41,7 +41,7 @@ impl Preg { let result = shirabe_php_shim::preg_match2( pattern, subject, - Some(&mut internal), + &mut internal, flags | PREG_UNMATCHED_AS_NULL, offset, ); @@ -79,7 +79,7 @@ impl Preg { let result = shirabe_php_shim::preg_match_all2( pattern, subject, - Some(&mut internal), + &mut internal, flags | PREG_UNMATCHED_AS_NULL, offset, ); @@ -104,7 +104,7 @@ impl Preg { let result = shirabe_php_shim::preg_match_all_offset_capture2( pattern, subject, - Some(&mut internal), + &mut internal, flags | PREG_UNMATCHED_AS_NULL | PREG_OFFSET_CAPTURE, offset, ); @@ -222,7 +222,7 @@ impl Preg { let result = shirabe_php_shim::preg_match2( pattern, subject, - Some(&mut internal), + &mut internal, PREG_UNMATCHED_AS_NULL, 0, ); @@ -241,7 +241,7 @@ impl Preg { // Classic preg_match semantics (no PREG_UNMATCHED_AS_NULL): trailing // unmatched groups are truncated, interior unmatched groups become "". let mut internal: IndexMap<CaptureKey, Option<String>> = IndexMap::new(); - let result = shirabe_php_shim::preg_match2(pattern, subject, Some(&mut internal), 0, 0); + let result = shirabe_php_shim::preg_match2(pattern, subject, &mut internal, 0, 0); if !result { return None; diff --git a/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs b/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs index 03aaa64..4dbc4e1 100644 --- a/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs +++ b/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs @@ -55,7 +55,7 @@ impl StringInput { } let mut m: IndexMap<CaptureKey, Option<String>> = IndexMap::new(); - if shirabe_php_shim::preg_match2(r"/\s+/A", input, Some(&mut m), 0, cursor as usize) { + if shirabe_php_shim::preg_match2(r"/\s+/A", input, &mut m, 0, cursor as usize) { if token.is_some() { tokens.push(token.take().unwrap()); } @@ -64,7 +64,7 @@ impl StringInput { } else if shirabe_php_shim::preg_match2( &format!(r#"/([^="'\s]+?)(=?)({}+)/A"#, Self::REGEX_QUOTED_STRING), input, - Some(&mut m), + &mut m, 0, cursor as usize, ) { @@ -87,7 +87,7 @@ impl StringInput { } else if shirabe_php_shim::preg_match2( &format!(r"/{}/A", Self::REGEX_QUOTED_STRING), input, - Some(&mut m), + &mut m, 0, cursor as usize, ) { @@ -105,7 +105,7 @@ impl StringInput { } else if shirabe_php_shim::preg_match2( &format!(r"/{}/A", Self::REGEX_UNQUOTED_STRING), input, - Some(&mut m), + &mut m, 0, cursor as usize, ) { diff --git a/crates/shirabe-php-shim/src/preg.rs b/crates/shirabe-php-shim/src/preg.rs index e9ee3bc..330ed4d 100644 --- a/crates/shirabe-php-shim/src/preg.rs +++ b/crates/shirabe-php-shim/src/preg.rs @@ -161,7 +161,7 @@ where pub fn preg_match2( pattern: &str, subject: &str, - matches: Option<&mut indexmap::IndexMap<CaptureKey, Option<String>>>, + matches: &mut indexmap::IndexMap<CaptureKey, Option<String>>, flags: i64, offset: usize, ) -> bool { @@ -169,12 +169,10 @@ pub fn preg_match2( let unmatched_as_null = flags & PREG_UNMATCHED_AS_NULL != 0; let caps = re.captures_at(subject, offset); - if let Some(out) = matches { - out.clear(); - if let Some(caps) = &caps { - let names: Vec<Option<&str>> = re.capture_names().collect(); - *out = single_match_map(caps, &names, unmatched_as_null); - } + matches.clear(); + if let Some(caps) = &caps { + let names: Vec<Option<&str>> = re.capture_names().collect(); + *matches = single_match_map(caps, &names, unmatched_as_null); } caps.is_some() @@ -183,7 +181,7 @@ pub fn preg_match2( pub fn preg_match_all2( pattern: &str, subject: &str, - matches: Option<&mut indexmap::IndexMap<CaptureKey, Vec<Option<String>>>>, + matches: &mut indexmap::IndexMap<CaptureKey, Vec<Option<String>>>, flags: i64, offset: usize, ) -> usize { @@ -207,14 +205,12 @@ pub fn preg_match_all2( } } - if let Some(out) = matches { - out.clear(); - for (g, column) in groups.into_iter().enumerate() { - if let Some(Some(name)) = names.get(g) { - out.insert(CaptureKey::ByName((*name).to_string()), column.clone()); - } - out.insert(CaptureKey::ByIndex(g), column); + matches.clear(); + for (g, column) in groups.into_iter().enumerate() { + if let Some(Some(name)) = names.get(g) { + matches.insert(CaptureKey::ByName((*name).to_string()), column.clone()); } + matches.insert(CaptureKey::ByIndex(g), column); } count @@ -223,7 +219,7 @@ pub fn preg_match_all2( pub fn preg_match_all_offset_capture2( pattern: &str, subject: &str, - matches: Option<&mut indexmap::IndexMap<CaptureKey, Vec<(Option<String>, i64)>>>, + matches: &mut indexmap::IndexMap<CaptureKey, Vec<(Option<String>, i64)>>, flags: i64, offset: usize, ) -> usize { @@ -246,14 +242,12 @@ pub fn preg_match_all_offset_capture2( } } - if let Some(out) = matches { - out.clear(); - for (g, column) in groups.into_iter().enumerate() { - if let Some(Some(name)) = names.get(g) { - out.insert(CaptureKey::ByName((*name).to_string()), column.clone()); - } - out.insert(CaptureKey::ByIndex(g), column); + matches.clear(); + for (g, column) in groups.into_iter().enumerate() { + if let Some(Some(name)) = names.get(g) { + matches.insert(CaptureKey::ByName((*name).to_string()), column.clone()); } + matches.insert(CaptureKey::ByIndex(g), column); } count diff --git a/crates/shirabe/src/console/application.rs b/crates/shirabe/src/console/application.rs index 2bd4017..37c0bb0 100644 --- a/crates/shirabe/src/console/application.rs +++ b/crates/shirabe/src/console/application.rs @@ -2708,7 +2708,7 @@ impl Application { while shirabe_php_shim::preg_match2( r"/.{1,10000}/u", &utf8_string, - Some(&mut m), + &mut m, 0, offset as usize, ) { |
