diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-14 04:01:45 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-14 04:02:58 +0900 |
| commit | 78157ab3ffeca37023381a422fccda2d4a0c64af (patch) | |
| tree | 11664aae5e585742647d4c0374495acd77b1aedb /crates/shirabe-external-packages | |
| parent | 7f00f9e7f9be826863716a5a52c141eb913d03bb (diff) | |
| download | php-shirabe-78157ab3ffeca37023381a422fccda2d4a0c64af.tar.gz php-shirabe-78157ab3ffeca37023381a422fccda2d4a0c64af.tar.zst php-shirabe-78157ab3ffeca37023381a422fccda2d4a0c64af.zip | |
refactor(pcre): drop strict-groups Preg variants
Rust's type system already distinguishes participating from
non-participating capture groups via Option, so the *StrictGroups
methods add no safety here. Remove them and switch callers to the
plain variants.
Diffstat (limited to 'crates/shirabe-external-packages')
3 files changed, 2 insertions, 181 deletions
diff --git a/crates/shirabe-external-packages/src/composer/pcre/mod.rs b/crates/shirabe-external-packages/src/composer/pcre/mod.rs index 41def3b..5d1a373 100644 --- a/crates/shirabe-external-packages/src/composer/pcre/mod.rs +++ b/crates/shirabe-external-packages/src/composer/pcre/mod.rs @@ -1,5 +1,4 @@ pub mod pcre_exception; pub mod preg; -pub mod unexpected_null_match_exception; pub use preg::*; diff --git a/crates/shirabe-external-packages/src/composer/pcre/preg.rs b/crates/shirabe-external-packages/src/composer/pcre/preg.rs index 6af67c4..4c0c2bf 100644 --- a/crates/shirabe-external-packages/src/composer/pcre/preg.rs +++ b/crates/shirabe-external-packages/src/composer/pcre/preg.rs @@ -1,7 +1,6 @@ //! ref: composer/vendor/composer/pcre/src/Preg.php use super::pcre_exception::PcreException; -use super::unexpected_null_match_exception::UnexpectedNullMatchException; use indexmap::IndexMap; pub const PREG_PATTERN_ORDER: i64 = 1; @@ -51,39 +50,8 @@ impl Preg { Ok(result == 1) } - pub fn match_strict_groups3( - pattern: &str, - subject: &str, - matches: Option<&mut IndexMap<CaptureKey, String>>, - ) -> anyhow::Result<bool> { - Self::match_strict_groups5(pattern, subject, matches, 0, 0) - } - - pub fn match_strict_groups5( - pattern: &str, - subject: &str, - matches: Option<&mut IndexMap<CaptureKey, String>>, - flags: i64, - offset: usize, - ) -> anyhow::Result<bool> { - Self::check_offset_capture(flags, "matchWithOffsets"); - - let mut internal: IndexMap<CaptureKey, Option<String>> = IndexMap::new(); - let result = preg_match( - pattern, - subject, - Some(&mut internal), - flags | PREG_UNMATCHED_AS_NULL, - offset, - ) - .ok_or_else(|| PcreException::from_function("preg_match", pattern))?; - - let enforced = Self::enforce_non_null_matches(pattern, internal, "match")?; - if let Some(out) = matches { - *out = enforced; - } - - Ok(result == 1) + pub fn match_all(pattern: &str, subject: &str) -> anyhow::Result<usize> { + Self::match_all5(pattern, subject, None, 0, 0) } pub fn match_all3( @@ -121,38 +89,6 @@ impl Preg { Ok(result as usize) } - pub fn match_all_strict_groups(pattern: &str, subject: &str) -> anyhow::Result<usize> { - Self::match_all_strict_groups5(pattern, subject, None, 0, 0) - } - - pub fn match_all_strict_groups5( - pattern: &str, - subject: &str, - matches: Option<&mut IndexMap<CaptureKey, Vec<String>>>, - flags: i64, - offset: usize, - ) -> anyhow::Result<usize> { - Self::check_offset_capture(flags, "matchAllWithOffsets"); - Self::check_set_order(flags); - - let mut internal: IndexMap<CaptureKey, Vec<Option<String>>> = IndexMap::new(); - let result = preg_match_all( - pattern, - subject, - Some(&mut internal), - flags | PREG_UNMATCHED_AS_NULL, - offset, - ) - .ok_or_else(|| PcreException::from_function("preg_match_all", pattern))?; - - let enforced = Self::enforce_non_null_match_all(pattern, internal, "matchAll")?; - if let Some(out) = matches { - *out = enforced; - } - - Ok(result as usize) - } - pub fn match_all_with_offsets5( pattern: &str, subject: &str, @@ -315,24 +251,6 @@ impl Preg { Ok(result == 1) } - pub fn is_match_strict_groups3( - pattern: &str, - subject: &str, - matches: Option<&mut IndexMap<CaptureKey, String>>, - ) -> anyhow::Result<bool> { - Self::match_strict_groups5(pattern, subject, matches, 0, 0) - } - - pub fn is_match_strict_groups5( - pattern: &str, - subject: &str, - matches: Option<&mut IndexMap<CaptureKey, String>>, - flags: i64, - offset: usize, - ) -> anyhow::Result<bool> { - Self::match_strict_groups5(pattern, subject, matches, flags, offset) - } - pub fn is_match_with_indexed_captures( pattern: &str, subject: &str, @@ -376,14 +294,6 @@ impl Preg { Ok(Self::match_all5(pattern, subject, matches, 0, 0)? > 0) } - pub fn is_match_all_strict_groups3( - pattern: &str, - subject: &str, - matches: Option<&mut IndexMap<CaptureKey, Vec<String>>>, - ) -> anyhow::Result<bool> { - Ok(Self::match_all_strict_groups5(pattern, subject, matches, 0, 0)? > 0) - } - pub fn is_match_all_with_offsets3( pattern: &str, subject: &str, @@ -406,65 +316,6 @@ impl Preg { "PREG_SET_ORDER is not supported as it changes the type of $matches" ); } - - fn enforce_non_null_matches( - pattern: &str, - matches: IndexMap<CaptureKey, Option<String>>, - variant_method: &str, - ) -> anyhow::Result<IndexMap<CaptureKey, String>> { - let mut result = IndexMap::new(); - for (group, m) in matches { - match m { - None => { - return Err(UnexpectedNullMatchException::new(format!( - "Pattern \"{}\" had an unexpected unmatched group \"{}\", make sure the pattern always matches or use {}() instead.", - pattern, - capture_key_to_string(&group), - variant_method - )) - .into()); - } - Some(value) => { - result.insert(group, value); - } - } - } - Ok(result) - } - - fn enforce_non_null_match_all( - pattern: &str, - matches: IndexMap<CaptureKey, Vec<Option<String>>>, - variant_method: &str, - ) -> anyhow::Result<IndexMap<CaptureKey, Vec<String>>> { - let mut result = IndexMap::new(); - for (group, group_matches) in matches { - let mut converted = Vec::with_capacity(group_matches.len()); - for m in group_matches { - match m { - None => { - return Err(UnexpectedNullMatchException::new(format!( - "Pattern \"{}\" had an unexpected unmatched group \"{}\", make sure the pattern always matches or use {}() instead.", - pattern, - capture_key_to_string(&group), - variant_method - )) - .into()); - } - Some(value) => converted.push(value), - } - } - result.insert(group, converted); - } - Ok(result) - } -} - -fn capture_key_to_string(key: &CaptureKey) -> String { - match key { - CaptureKey::ByIndex(index) => index.to_string(), - CaptureKey::ByName(name) => name.clone(), - } } // Drops `null` (unmatched) groups, mirroring how the public `string`-valued diff --git a/crates/shirabe-external-packages/src/composer/pcre/unexpected_null_match_exception.rs b/crates/shirabe-external-packages/src/composer/pcre/unexpected_null_match_exception.rs deleted file mode 100644 index 513870c..0000000 --- a/crates/shirabe-external-packages/src/composer/pcre/unexpected_null_match_exception.rs +++ /dev/null @@ -1,29 +0,0 @@ -//! ref: composer/vendor/composer/pcre/src/UnexpectedNullMatchException.php - -use super::pcre_exception::PcreException; - -#[derive(Debug)] -pub struct UnexpectedNullMatchException(pub PcreException); - -impl UnexpectedNullMatchException { - pub fn new(message: String) -> UnexpectedNullMatchException { - UnexpectedNullMatchException(PcreException(shirabe_php_shim::RuntimeException { - message, - code: 0, - })) - } - - pub fn from_function(_function: &str, _pattern: &str) -> UnexpectedNullMatchException { - panic!( - "fromFunction should not be called on UnexpectedNullMatchException, use PcreException" - ); - } -} - -impl std::fmt::Display for UnexpectedNullMatchException { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} - -impl std::error::Error for UnexpectedNullMatchException {} |
