aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-php-shim
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-06-14 12:48:58 +0900
committernsfisis <nsfisis@gmail.com>2026-06-14 12:48:58 +0900
commitc1070afe69f53b621adea232527080d2c922e1a9 (patch)
treebe125ece467c2d1d761cd9f8a606337f6e5233f4 /crates/shirabe-php-shim
parent4ace644474d3d0e5575d19616ce46ba7d521fc64 (diff)
downloadphp-shirabe-c1070afe69f53b621adea232527080d2c922e1a9.tar.gz
php-shirabe-c1070afe69f53b621adea232527080d2c922e1a9.tar.zst
php-shirabe-c1070afe69f53b621adea232527080d2c922e1a9.zip
refactor(pcre): consolidate duplicate preg_* shim helpers
The preg.rs shim had several near-duplicate functions: simple helpers re-implementing logic already covered by their full-featured `2` variants. Delegate or remove the redundant ones while preserving behavior, and migrate the affected callers: - preg_replace / preg_split now delegate to preg_replace2 / preg_split2 - preg_match_all_simple removed; its caller uses preg_match_all - preg_split_chars removed; its caller uses a char-boundary iterator - preg_match_offset removed; its callers use preg_match2 directly Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-php-shim')
-rw-r--r--crates/shirabe-php-shim/src/preg.rs78
1 files changed, 2 insertions, 76 deletions
diff --git a/crates/shirabe-php-shim/src/preg.rs b/crates/shirabe-php-shim/src/preg.rs
index e1b05b6..48b81ed 100644
--- a/crates/shirabe-php-shim/src/preg.rs
+++ b/crates/shirabe-php-shim/src/preg.rs
@@ -58,23 +58,12 @@ pub fn preg_match(pattern: &str, subject: &str, matches: &mut Vec<Option<String>
// Returns Some(result) on success, None on error.
pub fn preg_replace(pattern: &str, replacement: &str, subject: &str) -> Option<String> {
- let re = compile_php_pattern(pattern).ok()?;
- let mut out: Vec<u8> = Vec::new();
- let mut last = 0;
- for caps in re.captures_iter(subject) {
- let m = caps.get(0).unwrap();
- out.extend_from_slice(&subject.as_bytes()[last..m.start()]);
- php_replacement_expand(replacement, &caps, &mut out);
- last = m.end();
- }
- out.extend_from_slice(&subject.as_bytes()[last..]);
- Some(String::from_utf8_lossy(&out).into_owned())
+ preg_replace2(pattern, replacement, subject, -1, None)
}
// Returns Some(parts) on success, None on error.
pub fn preg_split(pattern: &str, subject: &str) -> Option<Vec<String>> {
- let re = compile_php_pattern(pattern).ok()?;
- Some(php_split_impl(&re, subject))
+ preg_split2(pattern, subject, -1, 0)
}
// PREG_PATTERN_ORDER: the outer vec is indexed by capture group, the inner by
@@ -95,29 +84,6 @@ pub fn preg_match_all(pattern: &str, subject: &str) -> Vec<Vec<String>> {
groups
}
-pub fn preg_match_all_simple(
- pattern: &str,
- subject: &str,
- matches: &mut Vec<Vec<String>>,
-) -> anyhow::Result<i64> {
- let re = compile_php_pattern(pattern)?;
- let group_count = re.captures_len();
- let mut groups: Vec<Vec<String>> = vec![Vec::new(); group_count];
- let mut count = 0i64;
- for caps in re.captures_iter(subject) {
- count += 1;
- for g in 0..group_count {
- groups[g].push(
- caps.get(g)
- .map(|m| m.as_str().to_string())
- .unwrap_or_default(),
- );
- }
- }
- *matches = groups;
- Ok(count)
-}
-
// PREG_SET_ORDER: the outer vec is indexed by match occurrence, the inner by
// capture group (a classic `$matches` row).
pub fn preg_match_all_set_order(
@@ -135,26 +101,6 @@ pub fn preg_match_all_set_order(
Ok(count)
}
-pub fn preg_match_offset(
- pattern: &str,
- subject: &str,
- matches: &mut Vec<String>,
- _flags: i64,
- offset: i64,
-) -> bool {
- let re = compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}"));
- match re.captures_at(subject, offset as usize) {
- Some(caps) => {
- *matches = php_match_row(&caps);
- true
- }
- None => {
- matches.clear();
- false
- }
- }
-}
-
pub fn preg_match_groups(pattern: &str, subject: &str) -> Option<Vec<String>> {
let re = compile_php_pattern(pattern).ok()?;
let caps = re.captures(subject)?;
@@ -166,11 +112,6 @@ pub fn preg_grep(pattern: &str, input: &Vec<String>) -> Vec<String> {
input.iter().filter(|s| re.is_match(s)).cloned().collect()
}
-pub fn preg_split_chars(pattern: &str, subject: &str) -> Vec<String> {
- let re = compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}"));
- php_split_impl(&re, subject)
-}
-
pub fn preg_match_all_offset_capture(
pattern: &str,
subject: &str,
@@ -551,21 +492,6 @@ fn php_replacement_group(bytes: &[u8]) -> (usize, usize) {
(group, consumed)
}
-// PHP `preg_split($pattern, $subject)` with no flags or limit: the text between
-// successive matches, including the leading and trailing pieces (which may be
-// empty). Zero-width matches split between every position.
-fn php_split_impl(re: &regex::Regex, subject: &str) -> Vec<String> {
- let mut result = Vec::new();
- let mut last = 0;
- for caps in re.captures_iter(subject) {
- let m = caps.get(0).unwrap();
- result.push(subject[last..m.start()].to_string());
- last = m.end();
- }
- result.push(subject[last..].to_string());
- result
-}
-
// Classic preg_match `$matches` row: index 0 is the full match, trailing
// unmatched groups are truncated and interior unmatched groups become "".
fn php_match_row(caps: &regex::Captures) -> Vec<String> {