diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-14 13:12:06 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-14 13:12:06 +0900 |
| commit | 614616d4ceeb78dcf22df3e36a0fceb4c2d110c8 (patch) | |
| tree | dc036aca8b4b9d10fb6caf002219759bf93db58f /crates | |
| parent | d7c686f6c488739fc9d061bb0f89d73e6445fade (diff) | |
| download | php-shirabe-614616d4ceeb78dcf22df3e36a0fceb4c2d110c8.tar.gz php-shirabe-614616d4ceeb78dcf22df3e36a0fceb4c2d110c8.tar.zst php-shirabe-614616d4ceeb78dcf22df3e36a0fceb4c2d110c8.zip | |
refactor(pcre): return bool/usize from preg_*2 shim helpers
preg_match2 returns bool and the preg_match_all* helpers return usize
(the match count is never negative), matching how callers use them.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates')
4 files changed, 20 insertions, 24 deletions
diff --git a/crates/shirabe-external-packages/src/composer/pcre/preg.rs b/crates/shirabe-external-packages/src/composer/pcre/preg.rs index d35a3aa..4034649 100644 --- a/crates/shirabe-external-packages/src/composer/pcre/preg.rs +++ b/crates/shirabe-external-packages/src/composer/pcre/preg.rs @@ -50,7 +50,7 @@ impl Preg { *out = drop_null_matches(internal); } - result == 1 + result } pub fn match_all(pattern: &str, subject: &str) -> usize { @@ -234,7 +234,7 @@ impl Preg { } } - result == 1 + result } pub fn is_match_with_indexed_captures(pattern: &str, subject: &str) -> Option<Vec<String>> { @@ -243,7 +243,7 @@ impl Preg { let mut internal: IndexMap<CaptureKey, Option<String>> = IndexMap::new(); let result = shirabe_php_shim::preg_match2(pattern, subject, Some(&mut internal), 0, 0); - if result == 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 f57fcf6..03aaa64 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,9 +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) - == 1 - { + if shirabe_php_shim::preg_match2(r"/\s+/A", input, Some(&mut m), 0, cursor as usize) { if token.is_some() { tokens.push(token.take().unwrap()); } @@ -69,8 +67,7 @@ impl StringInput { Some(&mut m), 0, cursor as usize, - ) == 1 - { + ) { let inner = shirabe_php_shim::substr( m[&CaptureKey::ByIndex(3)].as_deref().unwrap_or(""), 1, @@ -93,8 +90,7 @@ impl StringInput { Some(&mut m), 0, cursor as usize, - ) == 1 - { + ) { token = Some(format!( "{}{}", token.unwrap_or_default(), @@ -112,8 +108,7 @@ impl StringInput { Some(&mut m), 0, cursor as usize, - ) == 1 - { + ) { token = Some(format!( "{}{}", token.unwrap_or_default(), diff --git a/crates/shirabe-php-shim/src/preg.rs b/crates/shirabe-php-shim/src/preg.rs index 510f6ba..e9ee3bc 100644 --- a/crates/shirabe-php-shim/src/preg.rs +++ b/crates/shirabe-php-shim/src/preg.rs @@ -88,13 +88,13 @@ pub fn preg_match_all_set_order( pattern: &str, subject: &str, matches: &mut Vec<Vec<String>>, -) -> i64 { +) -> usize { let re = compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}")); let mut rows: Vec<Vec<String>> = Vec::new(); for caps in re.captures_iter(subject) { rows.push(php_match_row(&caps)); } - let count = rows.len() as i64; + let count = rows.len(); *matches = rows; count } @@ -108,7 +108,7 @@ pub fn preg_match_all_offset_capture( pattern: &str, subject: &str, matches: &mut PregOffsetCaptureMatches, -) -> i64 { +) -> usize { let re = compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}")); let group_count = re.captures_len(); matches.groups = vec![Vec::new(); group_count]; @@ -130,6 +130,7 @@ pub fn preg_match_all_offset_capture( count } + pub fn preg_replace_callback<F>( pattern: &str, mut callback: F, @@ -155,14 +156,15 @@ where Ok(String::from_utf8_lossy(&out).into_owned()) } -// Returns 0|1. Unmatched groups are reported as None (PREG_UNMATCHED_AS_NULL). +// Returns whether the pattern matched. Unmatched groups are reported as None +// (PREG_UNMATCHED_AS_NULL). pub fn preg_match2( pattern: &str, subject: &str, matches: Option<&mut indexmap::IndexMap<CaptureKey, Option<String>>>, flags: i64, offset: usize, -) -> i64 { +) -> bool { let re = compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}")); let unmatched_as_null = flags & PREG_UNMATCHED_AS_NULL != 0; let caps = re.captures_at(subject, offset); @@ -175,7 +177,7 @@ pub fn preg_match2( } } - if caps.is_some() { 1 } else { 0 } + caps.is_some() } pub fn preg_match_all2( @@ -184,7 +186,7 @@ pub fn preg_match_all2( matches: Option<&mut indexmap::IndexMap<CaptureKey, Vec<Option<String>>>>, flags: i64, offset: usize, -) -> i64 { +) -> usize { let re = compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}")); let unmatched_as_null = flags & PREG_UNMATCHED_AS_NULL != 0; let group_count = re.captures_len(); @@ -192,7 +194,7 @@ pub fn preg_match_all2( // PREG_PATTERN_ORDER: one column per group, one row per match occurrence. let mut groups: Vec<Vec<Option<String>>> = vec![Vec::new(); group_count]; - let mut count = 0i64; + let mut count = 0; for caps in re.captures_iter(&subject[offset..]) { count += 1; for (g, column) in groups.iter_mut().enumerate() { @@ -224,14 +226,14 @@ pub fn preg_match_all_offset_capture2( matches: Option<&mut indexmap::IndexMap<CaptureKey, Vec<(Option<String>, i64)>>>, flags: i64, offset: usize, -) -> i64 { +) -> usize { let re = compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}")); let unmatched_as_null = flags & PREG_UNMATCHED_AS_NULL != 0; let group_count = re.captures_len(); let names: Vec<Option<&str>> = re.capture_names().collect(); let mut groups: Vec<Vec<(Option<String>, i64)>> = vec![Vec::new(); group_count]; - let mut count = 0i64; + let mut count = 0; for caps in re.captures_iter(&subject[offset..]) { count += 1; for (g, column) in groups.iter_mut().enumerate() { diff --git a/crates/shirabe/src/console/application.rs b/crates/shirabe/src/console/application.rs index ad0073f..2bd4017 100644 --- a/crates/shirabe/src/console/application.rs +++ b/crates/shirabe/src/console/application.rs @@ -2711,8 +2711,7 @@ impl Application { Some(&mut m), 0, offset as usize, - ) == 1 - { + ) { let m0 = m[&shirabe_php_shim::CaptureKey::ByIndex(0)] .as_deref() .unwrap_or(""); |
