From 8b57e90f5c563bbc1a6060d9f5e94b5d3cd91324 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 17 Aug 2026 03:09:04 +0900 Subject: fix(pcre): preserve unmatched groups in Preg::match_all*() PHP's Preg::matchAll() and matchAllWithOffsets() always set PREG_UNMATCHED_AS_NULL, so a non-participating group is `null` and its offset is -1. The Rust wrappers collapsed those to "" and 0, so callers could not tell a group that did not participate from one that matched an empty string at offset 0, and the offset value matched no PHP mode at all. Hand the shim's representation through unchanged and let each caller mirror what the PHP original does with it: `isset()` and `(string)` casts stay lenient, while `assert(is_string(...))` and the *StrictGroups() variants become `expect()`. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe-pcre/src/preg.rs | 50 ++++++----------------------------------- 1 file changed, 7 insertions(+), 43 deletions(-) (limited to 'crates/shirabe-pcre/src') diff --git a/crates/shirabe-pcre/src/preg.rs b/crates/shirabe-pcre/src/preg.rs index a761608e..822609a9 100644 --- a/crates/shirabe-pcre/src/preg.rs +++ b/crates/shirabe-pcre/src/preg.rs @@ -63,7 +63,7 @@ impl Preg { pub fn match_all3( pattern: impl PregPattern, subject: &str, - matches: Option<&mut IndexMap>>, + matches: Option<&mut IndexMap>>>, ) -> usize { Self::match_all5(pattern, subject, matches) } @@ -71,13 +71,13 @@ impl Preg { fn match_all5( pattern: impl PregPattern, subject: &str, - matches: Option<&mut IndexMap>>, + matches: Option<&mut IndexMap>>>, ) -> usize { let mut internal: IndexMap>> = IndexMap::new(); let result = preg_match_all2(pattern, subject, &mut internal); if let Some(out) = matches { - *out = null_to_empty_match_all(internal); + *out = internal; } result @@ -86,13 +86,13 @@ impl Preg { fn match_all_with_offsets5( pattern: impl PregPattern, subject: &str, - matches: Option<&mut IndexMap>>, + matches: Option<&mut IndexMap, i64)>>>, ) -> usize { let mut internal: IndexMap, i64)>> = IndexMap::new(); let result = preg_match_all_offset_capture2(pattern, subject, &mut internal); if let Some(out) = matches { - *out = null_to_empty_offset_match_all(internal); + *out = internal; } result @@ -243,7 +243,7 @@ impl Preg { pub fn is_match_all3( pattern: impl PregPattern, subject: &str, - matches: Option<&mut IndexMap>>, + matches: Option<&mut IndexMap>>>, ) -> bool { Self::match_all5(pattern, subject, matches) > 0 } @@ -251,7 +251,7 @@ impl Preg { pub fn is_match_all_with_offsets3( pattern: impl PregPattern, subject: &str, - matches: Option<&mut IndexMap>>, + matches: Option<&mut IndexMap, i64)>>>, ) -> bool { Self::match_all_with_offsets5(pattern, subject, matches) > 0 } @@ -291,39 +291,3 @@ fn drop_null_matches_ref( .filter_map(|(key, value)| value.clone().map(|value| (key.clone(), value))) .collect() } - -// In the `Vec`-valued maps a per-iteration `null` cannot be stored, so -// unmatched groups collapse to "" (the classic non-PREG_UNMATCHED_AS_NULL form). -fn null_to_empty_match_all( - matches: IndexMap>>, -) -> IndexMap> { - matches - .into_iter() - .map(|(key, values)| { - ( - key, - values - .into_iter() - .map(|value| value.unwrap_or_default()) - .collect(), - ) - }) - .collect() -} - -fn null_to_empty_offset_match_all( - matches: IndexMap, i64)>>, -) -> IndexMap> { - matches - .into_iter() - .map(|(key, values)| { - ( - key, - values - .into_iter() - .map(|(value, offset)| (value.unwrap_or_default(), offset.max(0) as usize)) - .collect(), - ) - }) - .collect() -} -- cgit v1.3.1-4-g156e