From c1070afe69f53b621adea232527080d2c922e1a9 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 14 Jun 2026 12:48:58 +0900 Subject: 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) --- crates/shirabe/src/console/application.rs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'crates/shirabe/src/console') diff --git a/crates/shirabe/src/console/application.rs b/crates/shirabe/src/console/application.rs index 00453f8..4be2f90 100644 --- a/crates/shirabe/src/console/application.rs +++ b/crates/shirabe/src/console/application.rs @@ -2703,17 +2703,33 @@ impl Application { let mut line = String::new(); let mut offset = 0i64; - let mut m: Vec = Vec::new(); - while shirabe_php_shim::preg_match_offset(r"/.{1,10000}/u", &utf8_string, &mut m, 0, offset) + let mut m: indexmap::IndexMap> = + indexmap::IndexMap::new(); + while shirabe_php_shim::preg_match2( + r"/.{1,10000}/u", + &utf8_string, + Some(&mut m), + 0, + offset as usize, + ) + .expect("invalid regex") + == 1 { - offset += shirabe_php_shim::strlen(&m[0]); - - for char in shirabe_php_shim::preg_split_chars(r"//u", &m[0]) { + let m0 = m[&shirabe_php_shim::CaptureKey::ByIndex(0)] + .as_deref() + .unwrap_or(""); + offset += shirabe_php_shim::strlen(m0); + + let chunk = m0; + for char in chunk + .char_indices() + .map(|(i, c)| &chunk[i..i + c.len_utf8()]) + { // test if $char could be appended to current line if shirabe_php_shim::mb_strwidth(&format!("{}{}", line, char), Some("utf8")) <= width { - line.push_str(&char); + line.push_str(char); continue; } // if not, push current line to array and make new line @@ -2723,7 +2739,7 @@ impl Application { " ", shirabe_php_shim::STR_PAD_RIGHT, )); - line = char; + line = char.to_string(); } } -- cgit v1.3.1