From c7aa10384a2548167466b4c61f9eff29ed8611f6 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 17 Aug 2026 07:13:02 +0900 Subject: refactor(preg): report unmatched groups as null throughout The shim carried two reporting modes for the preg_* $matches maps: PHP's default (trailing unmatched groups dropped, interior ones ""), and the PREG_UNMATCHED_AS_NULL form, picked by calling a *_unmatched_as_null() variant. The regex crate hands out Option, which maps onto the null form directly, and no caller distinguished a dropped group from a null one -- preg_match() and preg_match_all2() already reported nulls unconditionally. Keep only the null form; the shim API no longer mirrors PHP's flag set, which is intended. Preg::is_match_with_indexed_captures() modelled PHP's "unset" as a truncated Vec, and now returns Vec>. That is what Composer actually does: Preg::isMatch() always sets PREG_UNMATCHED_AS_NULL, and its callers test groups with `!== null`. preg_match_all(), preg_match_all_set_order() and preg_split_delim_capture() still hand back Vec and keep the "" form. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe-pcre/src/preg.rs | 50 +++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 32 deletions(-) (limited to 'crates/shirabe-pcre/src') diff --git a/crates/shirabe-pcre/src/preg.rs b/crates/shirabe-pcre/src/preg.rs index 3a997ad3..aa986ae8 100644 --- a/crates/shirabe-pcre/src/preg.rs +++ b/crates/shirabe-pcre/src/preg.rs @@ -13,9 +13,8 @@ pub use shirabe_php_shim::{CaptureKey, PregMatches, PregMatchesAll, PregMatchesAllWithOffsets}; use shirabe_php_shim::{ - PregPattern, preg_grep, preg_match_all_offset_capture_unmatched_as_null, preg_match_all2, - preg_match_map, preg_match2, preg_match2_unmatched_as_null, preg_replace_callback, - preg_replace2, + PregPattern, preg_grep, preg_match_all_offset_capture, preg_match_all2, preg_match_map, + preg_match2, preg_replace_callback, preg_replace2, }; preg_match_map! { @@ -48,7 +47,7 @@ impl Preg { offset: usize, ) -> bool { let mut internal = PregMatches::new(); - let result = preg_match2_unmatched_as_null(pattern, subject, &mut internal, offset); + let result = preg_match2(pattern, subject, &mut internal, offset); if let Some(out) = matches { *out = drop_null_matches(internal); @@ -76,8 +75,7 @@ impl Preg { matches: Option<&mut PregMatchesAllWithOffsets>, ) -> usize { let mut internal = PregMatchesAllWithOffsets::new(); - let result = - preg_match_all_offset_capture_unmatched_as_null(pattern, subject, &mut internal); + let result = preg_match_all_offset_capture(pattern, subject, &mut internal); if let Some(out) = matches { *out = internal; @@ -153,7 +151,7 @@ impl Preg { matches: &mut PregNamedGroups, ) -> bool { let mut internal = PregMatches::new(); - let result = preg_match2_unmatched_as_null(pattern, subject, &mut internal, 0); + let result = preg_match2(pattern, subject, &mut internal, 0); matches.clear(); for (key, value) in internal { @@ -165,38 +163,26 @@ impl Preg { result } + /// `is_match3` with the groups positioned by number rather than keyed, for callers that only + /// read numbered groups. Index 0 is the full match; an unmatched group is `None`. pub fn is_match_with_indexed_captures( pattern: impl PregPattern, subject: &str, - ) -> Option> { - // Classic preg_match semantics (no PREG_UNMATCHED_AS_NULL): trailing - // unmatched groups are truncated, interior unmatched groups become "". + ) -> Option>> { let mut internal = PregMatches::new(); - let result = preg_match2(pattern, subject, &mut internal, 0); - - if !result { + if !preg_match2(pattern, subject, &mut internal, 0) { return None; } - let max_index = internal - .keys() - .filter_map(|key| match key { - CaptureKey::ByIndex(index) => Some(*index), - CaptureKey::ByName(_) => None, - }) - .max() - .unwrap_or(0); - - let mut captures = Vec::with_capacity(max_index + 1); - for index in 0..=max_index { - let value = internal - .get(&CaptureKey::ByIndex(index)) - .and_then(|value| value.clone()) - .unwrap_or_default(); - captures.push(value); - } - - Some(captures) + Some( + internal + .into_iter() + .filter_map(|(key, value)| match key { + CaptureKey::ByIndex(_) => Some(value), + CaptureKey::ByName(_) => None, + }) + .collect(), + ) } pub fn is_match_all( -- cgit v1.3.1-4-g156e