diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-16 19:06:49 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-16 19:07:15 +0900 |
| commit | b6604e1395f003748fe40a44e1190470632a40ce (patch) | |
| tree | 27ce3c137c860a7bd8914852ba5f851e213d448c /crates | |
| parent | c5605cc072c1880af7d70647278c6aa0de43f402 (diff) | |
| download | php-shirabe-b6604e1395f003748fe40a44e1190470632a40ce.tar.gz php-shirabe-b6604e1395f003748fe40a44e1190470632a40ce.tar.zst php-shirabe-b6604e1395f003748fe40a44e1190470632a40ce.zip | |
refactor(preg): import preg_*() instead of qualifying them
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates')
19 files changed, 106 insertions, 130 deletions
diff --git a/crates/shirabe-pcre/src/preg.rs b/crates/shirabe-pcre/src/preg.rs index 1c6fbfc1..0ebc0ff2 100644 --- a/crates/shirabe-pcre/src/preg.rs +++ b/crates/shirabe-pcre/src/preg.rs @@ -15,7 +15,8 @@ use indexmap::IndexMap; pub use shirabe_php_shim::CaptureKey; use shirabe_php_shim::{ PREG_OFFSET_CAPTURE, PREG_SET_ORDER, PREG_SPLIT_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL, - PregPattern, + PregPattern, preg_grep2, preg_match_all_offset_capture2, preg_match_all2, preg_match2, + preg_replace_callback, preg_replace2, preg_split2, }; #[derive(Debug)] @@ -40,7 +41,7 @@ impl Preg { Self::check_offset_capture(flags, "matchWithOffsets"); let mut internal: IndexMap<CaptureKey, Option<String>> = IndexMap::new(); - let result = shirabe_php_shim::preg_match2( + let result = preg_match2( pattern, subject, &mut internal, @@ -78,7 +79,7 @@ impl Preg { Self::check_set_order(flags); let mut internal: IndexMap<CaptureKey, Vec<Option<String>>> = IndexMap::new(); - let result = shirabe_php_shim::preg_match_all2( + let result = preg_match_all2( pattern, subject, &mut internal, @@ -103,7 +104,7 @@ impl Preg { Self::check_set_order(flags); let mut internal: IndexMap<CaptureKey, Vec<(Option<String>, i64)>> = IndexMap::new(); - let result = shirabe_php_shim::preg_match_all_offset_capture2( + let result = preg_match_all_offset_capture2( pattern, subject, &mut internal, @@ -151,7 +152,7 @@ impl Preg { // `$subject` is statically a string here, so the is_scalar/is_array // guards (ARRAY_MSG / INVALID_TYPE_MSG) of the PHP original are // unreachable and not reproduced. - shirabe_php_shim::preg_replace2(pattern, replacement, subject, limit, count) + preg_replace2(pattern, replacement, subject, limit, count) } pub fn replace_callback<F: FnMut(&IndexMap<CaptureKey, String>) -> String>( @@ -163,8 +164,7 @@ impl Preg { Ok(replacement(&drop_null_matches_ref(internal))) }; - shirabe_php_shim::preg_replace_callback(pattern, adapter, subject) - .expect("$replacement cannot fail") + preg_replace_callback(pattern, adapter, subject).expect("$replacement cannot fail") } pub fn split(pattern: impl PregPattern, subject: &str) -> Vec<String> { @@ -177,7 +177,7 @@ impl Preg { "PREG_SPLIT_OFFSET_CAPTURE is not supported as it changes the type of $matches, use splitWithOffsets() instead" ); - shirabe_php_shim::preg_split2(pattern, subject, limit, flags) + preg_split2(pattern, subject, limit, flags) } pub fn grep(pattern: impl PregPattern, array: &[&str]) -> Vec<String> { @@ -185,7 +185,7 @@ impl Preg { } pub fn grep3(pattern: impl PregPattern, array: &[&str], flags: i64) -> Vec<String> { - shirabe_php_shim::preg_grep2(pattern, array, flags) + preg_grep2(pattern, array, flags) } pub fn is_match(pattern: impl PregPattern, subject: &str) -> bool { @@ -216,13 +216,7 @@ impl Preg { matches: &mut IndexMap<String, String>, ) -> bool { let mut internal: IndexMap<CaptureKey, Option<String>> = IndexMap::new(); - let result = shirabe_php_shim::preg_match2( - pattern, - subject, - &mut internal, - PREG_UNMATCHED_AS_NULL, - 0, - ); + let result = preg_match2(pattern, subject, &mut internal, PREG_UNMATCHED_AS_NULL, 0); matches.clear(); for (key, value) in internal { @@ -241,7 +235,7 @@ impl Preg { // Classic preg_match semantics (no PREG_UNMATCHED_AS_NULL): trailing // unmatched groups are truncated, interior unmatched groups become "". let mut internal: IndexMap<CaptureKey, Option<String>> = IndexMap::new(); - let result = shirabe_php_shim::preg_match2(pattern, subject, &mut internal, 0, 0); + let result = preg_match2(pattern, subject, &mut internal, 0, 0); if !result { return None; diff --git a/crates/shirabe-semver/src/version_parser.rs b/crates/shirabe-semver/src/version_parser.rs index 287716d9..d0031f04 100644 --- a/crates/shirabe-semver/src/version_parser.rs +++ b/crates/shirabe-semver/src/version_parser.rs @@ -4,7 +4,7 @@ use crate::constraint::AnyConstraint; use crate::constraint::MatchAllConstraint; use crate::constraint::MultiConstraint; use crate::constraint::SimpleConstraint; -use shirabe_php_shim::php_regex; +use shirabe_php_shim::{php_regex, preg_match, preg_quote, preg_replace, preg_split}; // Regex to match pre-release data (sort of). // @@ -25,7 +25,7 @@ pub struct VersionParser; impl VersionParser { pub fn parse_stability(version: &str) -> String { - let version = shirabe_php_shim::preg_replace(php_regex!("{#.+$}"), "", version); + let version = preg_replace(php_regex!("{#.+$}"), "", version); if version.starts_with("dev-") || version.ends_with("-dev") { return "dev".to_string(); @@ -34,7 +34,7 @@ impl VersionParser { let pattern = format!("{{{}(?:\\+.*)?$}}i", MODIFIER_REGEX); let lower = shirabe_php_shim::strtolower(&version); let mut match_: Vec<Option<String>> = Vec::new(); - shirabe_php_shim::preg_match(&pattern, &lower, &mut match_); + preg_match(&pattern, &lower, &mut match_); // match_[3] = the ([.-]?dev)? capture if match_ @@ -87,7 +87,7 @@ impl VersionParser { // strip off aliasing let mut match_: Vec<Option<String>> = Vec::new(); - if shirabe_php_shim::preg_match( + if preg_match( php_regex!("{^([^,\\s]++) ++as ++([^,\\s]++)$}"), &version, &mut match_, @@ -98,7 +98,7 @@ impl VersionParser { // strip off stability flag let stab_pattern = format!("{{@(?:{})$}}i", STABILITIES_REGEX); let mut match_: Vec<Option<String>> = Vec::new(); - if shirabe_php_shim::preg_match(&stab_pattern, &version, &mut match_) { + if preg_match(&stab_pattern, &version, &mut match_) { let match0_len = match_[0].as_deref().unwrap_or("").len(); version = version[..version.len() - match0_len].to_string(); } @@ -116,7 +116,7 @@ impl VersionParser { // strip off build metadata let mut match_: Vec<Option<String>> = Vec::new(); - if shirabe_php_shim::preg_match( + if preg_match( php_regex!("{^([^,\\s+]++)\\+[^\\s]++$}"), &version, &mut match_, @@ -137,7 +137,7 @@ impl VersionParser { "{{^v?(\\d{{1,5}})(\\.\\d++)?(\\.\\d++)?(\\.\\d++)?{}$}}i", MODIFIER_REGEX ); - if shirabe_php_shim::preg_match(&classical_pattern, &version, &mut matches) { + if preg_match(&classical_pattern, &version, &mut matches) { let m2 = matches[2].as_deref().unwrap_or(""); let m3 = matches[3].as_deref().unwrap_or(""); let m4 = matches[4].as_deref().unwrap_or(""); @@ -155,8 +155,8 @@ impl VersionParser { "{{^v?(\\d{{4}}(?:[.:-]?\\d{{2}}){{1,6}}(?:[.:-]?\\d{{1,3}}){{0,2}}){}$}}i", MODIFIER_REGEX ); - if shirabe_php_shim::preg_match(&datetime_pattern, &version, &mut matches) { - version = shirabe_php_shim::preg_replace( + if preg_match(&datetime_pattern, &version, &mut matches) { + version = preg_replace( php_regex!("{\\D}"), ".", matches[1].as_deref().unwrap_or(""), @@ -202,7 +202,7 @@ impl VersionParser { // match dev branches let mut match_: Vec<Option<String>> = Vec::new(); - if shirabe_php_shim::preg_match(php_regex!("{(.*?)[.-]?dev$}i"), &version, &mut match_) { + if preg_match(php_regex!("{(.*?)[.-]?dev$}i"), &version, &mut match_) { let branch_name = match_[1].clone().unwrap_or_default(); // a branch ending with -dev is only valid if it is numeric // if it gets prefixed with dev- it means the branch name should @@ -214,10 +214,10 @@ impl VersionParser { } } - let extra_message = if shirabe_php_shim::preg_match( + let extra_message = if preg_match( format!( "{{ +as +{}(?:@(?:{}))?$}}", - shirabe_php_shim::preg_quote(&version, None), + preg_quote(&version, None), STABILITIES_REGEX ), &full_version, @@ -227,10 +227,10 @@ impl VersionParser { " in \"{}\", the alias must be an exact version", full_version ) - } else if shirabe_php_shim::preg_match( + } else if preg_match( format!( "{{^{}(?:@(?:{}))? +as +}}", - shirabe_php_shim::preg_quote(&version, None), + preg_quote(&version, None), STABILITIES_REGEX ), &full_version, @@ -255,7 +255,7 @@ impl VersionParser { pub fn parse_numeric_alias_prefix(&self, branch: &str) -> Option<String> { let mut matches: Vec<Option<String>> = Vec::new(); // matches['version'] == matches[1] ((?P<version>...) is group 1) - if shirabe_php_shim::preg_match( + if preg_match( php_regex!("{^(?P<version>(\\d++\\.)*\\d++)(?:\\.x)?-dev$}i"), branch, &mut matches, @@ -274,7 +274,7 @@ impl VersionParser { // Groups: 1=major, 2=".minor"(outer), 3=minor(inner), 4=".patch"(outer), // 5=patch(inner), 6=".fourth"(outer), 7=fourth(inner). // We use the outer groups [1,2,4,6] to replicate PHP's groups [1,2,3,4]. - if shirabe_php_shim::preg_match( + if preg_match( php_regex!("{^v?(\\d++)(\\.(\\d++|[xX*]))?(\\.(\\d++|[xX*]))?(\\.(\\d++|[xX*]))?$}i"), &name, &mut matches, @@ -309,7 +309,7 @@ impl VersionParser { pub fn parse_constraints(&self, constraints: &str) -> anyhow::Result<AnyConstraint> { let pretty_constraint = constraints.to_string(); - let or_constraints = shirabe_php_shim::preg_split( + let or_constraints = preg_split( php_regex!("{\\s*\\|\\|?\\s*}"), &shirabe_php_shim::trim(constraints, None), ); @@ -350,7 +350,7 @@ impl VersionParser { // strip off aliasing let mut match_: Vec<Option<String>> = Vec::new(); - if shirabe_php_shim::preg_match( + if preg_match( php_regex!("{^([^,\\s]++) ++as ++([^,\\s]++)$}"), &constraint, &mut match_, @@ -362,7 +362,7 @@ impl VersionParser { let mut stability_modifier: Option<String> = None; let mut match_: Vec<Option<String>> = Vec::new(); let stab_pattern = format!("{{^([^,\\s]*?)@({})$}}i", STABILITIES_REGEX); - if shirabe_php_shim::preg_match(&stab_pattern, &constraint, &mut match_) { + if preg_match(&stab_pattern, &constraint, &mut match_) { let m1 = match_[1].as_deref().unwrap_or(""); constraint = if !m1.is_empty() { m1.to_string() @@ -377,7 +377,7 @@ impl VersionParser { // get rid of #refs as those are used by composer only let mut match_: Vec<Option<String>> = Vec::new(); - if shirabe_php_shim::preg_match( + if preg_match( php_regex!("{^(dev-[^,\\s@]+?|[^,\\s@]+?\\.x-dev)#.+$}i"), &constraint, &mut match_, @@ -386,7 +386,7 @@ impl VersionParser { } let mut match_: Vec<Option<String>> = Vec::new(); - if shirabe_php_shim::preg_match( + if preg_match( php_regex!("{^(v)?[xX*](\\.[xX*])*$}i"), &constraint, &mut match_, @@ -425,7 +425,7 @@ impl VersionParser { // the current version is used instead. let mut matches: Vec<Option<String>> = Vec::new(); let tilde_pattern = format!("{{^~>?{}$}}i", version_regex); - if shirabe_php_shim::preg_match(&tilde_pattern, &constraint, &mut matches) { + if preg_match(&tilde_pattern, &constraint, &mut matches) { if constraint.starts_with("~>") { anyhow::bail!( "Could not parse version constraint {}: Invalid operator \"~>\", you probably \ @@ -488,7 +488,7 @@ impl VersionParser { // and above, patch updates for versions 0.X >=0.1.0, and no updates for versions 0.0.X let mut matches: Vec<Option<String>> = Vec::new(); let caret_pattern = format!("{{^\\^{}($)}}i", version_regex); - if shirabe_php_shim::preg_match(&caret_pattern, &constraint, &mut matches) { + if preg_match(&caret_pattern, &constraint, &mut matches) { // Work out which position in the version we are operating at let m1 = matches[1].as_deref().unwrap_or(""); let m2 = matches[2].as_deref().unwrap_or(""); @@ -536,7 +536,7 @@ impl VersionParser { // [major, minor, patch] tuple. A partial version range is treated as an X-Range, so the // special character is in fact optional. let mut matches: Vec<Option<String>> = Vec::new(); - if shirabe_php_shim::preg_match( + if preg_match( php_regex!("{^v?(\\d++)(?:\\.(\\d++))?(?:\\.(\\d++))?(?:\\.[xX*])++$}"), &constraint, &mut matches, @@ -586,7 +586,7 @@ impl VersionParser { "{{^(?P<from>{}) +- +(?P<to>{})($)}}i", version_regex, version_regex ); - if shirabe_php_shim::preg_match(&hyphen_pattern, &constraint, &mut matches) { + if preg_match(&hyphen_pattern, &constraint, &mut matches) { // matches[1]='from' string, matches[2..9]=from captures, matches[10]='to' string, // matches[11..18]=to captures, matches[19]='($)' // matches[6]=from stability, matches[8]=from dev, matches[9]=from wildcard-dev @@ -652,7 +652,7 @@ impl VersionParser { // Basic Comparators let mut match_: Vec<Option<String>> = Vec::new(); - if shirabe_php_shim::preg_match( + if preg_match( php_regex!("{^(<>|!=|>=?|<=?|==?)?\\s*(.*)}"), &constraint, &mut match_, @@ -667,7 +667,7 @@ impl VersionParser { // dev-foobar except if the constraint uses a known operator, in which // case it must be a parse error if version_str.ends_with("-dev") - && shirabe_php_shim::preg_match( + && preg_match( php_regex!("{^[0-9a-zA-Z-./]+$}"), &version_str, &mut Vec::new(), @@ -696,7 +696,7 @@ impl VersionParser { } if op == "<" || op == ">=" { let modifier_pattern = format!("{{-{}$}}", MODIFIER_REGEX); - if !shirabe_php_shim::preg_match( + if !preg_match( &modifier_pattern, &shirabe_php_shim::strtolower(&version_str), &mut Vec::new(), @@ -886,7 +886,7 @@ fn match_and_delimiter(b: &[u8], i: usize) -> Option<usize> { } #[cfg(test)] -mod split_and_constraints_tests { +mod tests { use super::split_and_constraints; fn split(s: &str) -> Vec<String> { diff --git a/crates/shirabe-spdx-licenses/src/spdx_licenses.rs b/crates/shirabe-spdx-licenses/src/spdx_licenses.rs index 82f2ae70..07b911fc 100644 --- a/crates/shirabe-spdx-licenses/src/spdx_licenses.rs +++ b/crates/shirabe-spdx-licenses/src/spdx_licenses.rs @@ -296,7 +296,7 @@ fn ws0_end(s: &[u8], pos: usize) -> usize { } #[cfg(test)] -mod is_valid_license_string_tests { +mod tests { use super::SpdxLicenses; // Every case below was cross-checked against `Composer\Spdx\SpdxLicenses::validate()` running diff --git a/crates/shirabe-symfony-console/src/command/command.rs b/crates/shirabe-symfony-console/src/command/command.rs index 6f25595b..b2af47e2 100644 --- a/crates/shirabe-symfony-console/src/command/command.rs +++ b/crates/shirabe-symfony-console/src/command/command.rs @@ -11,7 +11,7 @@ use crate::input::InputInterface; use crate::input::InputOption; use crate::output::OutputInterface; use indexmap::IndexMap; -use shirabe_php_shim::{PhpMixed, php_regex}; +use shirabe_php_shim::{PhpMixed, php_regex, preg_match}; use std::cell::{Cell, Ref}; /// The base-class state of the PHP `Command` class. @@ -109,8 +109,7 @@ impl CommandData { /// Throws InvalidArgumentException when the name is invalid. fn validate_name(&self, name: &str) -> anyhow::Result<Result<(), InvalidArgumentException>> { let mut matches: Vec<Option<String>> = Vec::new(); - if !shirabe_php_shim::preg_match(php_regex!(r"/^[^\:]++(\:[^\:]++)*$/"), name, &mut matches) - { + if !preg_match(php_regex!(r"/^[^\:]++(\:[^\:]++)*$/"), name, &mut matches) { return Ok(Err(InvalidArgumentException::new(format!( "Command name \"{}\" is invalid.", name diff --git a/crates/shirabe-symfony-console/src/completion/completion_input.rs b/crates/shirabe-symfony-console/src/completion/completion_input.rs index a0adff6c..2f146a39 100644 --- a/crates/shirabe-symfony-console/src/completion/completion_input.rs +++ b/crates/shirabe-symfony-console/src/completion/completion_input.rs @@ -3,7 +3,7 @@ use crate::input::ArgvInput; use crate::input::InputDefinition; use crate::input::InputOption; -use shirabe_php_shim::{PhpMixed, php_regex}; +use shirabe_php_shim::{PhpMixed, php_regex, preg_match_all}; /// An input specialized for shell completion. /// @@ -29,7 +29,7 @@ impl CompletionInput { /// /// This is required for shell completions without COMP_WORDS support. pub fn from_string(input_str: &str, current_index: i64) -> anyhow::Result<Self> { - let tokens = shirabe_php_shim::preg_match_all( + let tokens = preg_match_all( php_regex!("/(?<=^|\\s)(['\"]?)(.+?)(?<!\\\\)\\1(?=$|\\s)/"), input_str, ); diff --git a/crates/shirabe-symfony-console/src/formatter/output_formatter.rs b/crates/shirabe-symfony-console/src/formatter/output_formatter.rs index 960187d4..4943e65b 100644 --- a/crates/shirabe-symfony-console/src/formatter/output_formatter.rs +++ b/crates/shirabe-symfony-console/src/formatter/output_formatter.rs @@ -6,7 +6,10 @@ use crate::formatter::output_formatter_style::OutputFormatterStyle; use crate::formatter::output_formatter_style_interface::OutputFormatterStyleInterface; use crate::formatter::output_formatter_style_stack::OutputFormatterStyleStack; use crate::formatter::wrappable_output_formatter_interface::WrappableOutputFormatterInterface; -use shirabe_php_shim::php_regex; +use shirabe_php_shim::{ + php_regex, preg_match, preg_match_all, preg_match_all_offset_capture, preg_match_all_set_order, + preg_replace, +}; use shirabe_symfony_string::b; /// Formatter class for console output. @@ -25,8 +28,7 @@ pub struct OutputFormatter { impl OutputFormatter { /// Escapes "<" and ">" special chars in given text. pub fn escape(text: &str) -> anyhow::Result<String> { - let text = - shirabe_php_shim::preg_replace(php_regex!("/([^\\\\]|^)([<>])/"), "$1\\\\$2", text); + let text = preg_replace(php_regex!("/([^\\\\]|^)([<>])/"), "$1\\\\$2", text); Ok(Self::escape_trailing_backslash(&text)) } @@ -108,11 +110,7 @@ impl OutputFormatter { } let mut matches: Vec<Vec<String>> = vec![]; - if shirabe_php_shim::preg_match_all_set_order( - php_regex!("/([^=]+)=([^;]+)(;|$)/"), - string, - &mut matches, - ) == 0 + if preg_match_all_set_order(php_regex!("/([^=]+)=([^;]+)(;|$)/"), string, &mut matches) == 0 { return Ok(None); } @@ -128,11 +126,10 @@ impl OutputFormatter { } else if r#match[0] == "bg" { style.set_background(Some(&shirabe_php_shim::strtolower(&r#match[1]))); } else if r#match[0] == "href" { - let url = - shirabe_php_shim::preg_replace(php_regex!("{\\\\([<>])}"), "$1", &r#match[1]); + let url = preg_replace(php_regex!("{\\\\([<>])}"), "$1", &r#match[1]); style.set_href(&url); } else if r#match[0] == "options" { - let mut options = shirabe_php_shim::preg_match_all( + let mut options = preg_match_all( php_regex!("([^,;]+)"), &shirabe_php_shim::strtolower(&r#match[1]), ); @@ -184,7 +181,7 @@ impl OutputFormatter { } let mut matches: Vec<Option<String>> = vec![]; - shirabe_php_shim::preg_match(php_regex!("~(\\n)$~"), &text, &mut matches); + preg_match(php_regex!("~(\\n)$~"), &text, &mut matches); text = format!("{}{}", prefix, self.add_line_breaks(&text, width)); let trailing = matches.get(1).and_then(|m| m.clone()).unwrap_or_default(); text = format!("{}{}", shirabe_php_shim::rtrim(&text, Some("\n")), trailing); @@ -294,7 +291,7 @@ impl WrappableOutputFormatterInterface for OutputFormatter { let close_tag_regex = "[a-z][^<>]*"; let mut current_line_length: i64 = 0; let mut matches: shirabe_php_shim::PregOffsetCaptureMatches = Default::default(); - shirabe_php_shim::preg_match_all_offset_capture( + preg_match_all_offset_capture( format!("#<(({open_tag_regex}) | /({close_tag_regex})?)>#ix"), message, &mut matches, diff --git a/crates/shirabe-symfony-console/src/helper/helper.rs b/crates/shirabe-symfony-console/src/helper/helper.rs index 4f0447f3..21f20d34 100644 --- a/crates/shirabe-symfony-console/src/helper/helper.rs +++ b/crates/shirabe-symfony-console/src/helper/helper.rs @@ -2,7 +2,7 @@ use crate::formatter::OutputFormatterInterface; use crate::helper::HelperSet; -use shirabe_php_shim::php_regex; +use shirabe_php_shim::{php_regex, preg_match, preg_replace}; use shirabe_symfony_string::unicode_string::UnicodeString; /// Helper is the base class for all helper classes. @@ -40,7 +40,7 @@ impl Helper { /// Returns the width of a string, using mb_strwidth if it is available. /// The width is how many characters positions the string will use. pub fn width(string: &str) -> i64 { - if shirabe_php_shim::preg_match(php_regex!("//u"), string, &mut Vec::new()) { + if preg_match(php_regex!("//u"), string, &mut Vec::new()) { return UnicodeString::new(string).width(false); } @@ -56,7 +56,7 @@ impl Helper { /// Returns the length of a string, using mb_strlen if it is available. /// The length is related to how many bytes the string will use. pub fn length(string: &str) -> i64 { - if shirabe_php_shim::preg_match(php_regex!("//u"), string, &mut Vec::new()) { + if preg_match(php_regex!("//u"), string, &mut Vec::new()) { return UnicodeString::new(string).length(); } @@ -148,9 +148,9 @@ impl Helper { // remove <...> formatting let string = formatter.format(Some(string)).unwrap().unwrap_or_default(); // remove already formatted characters - let string = shirabe_php_shim::preg_replace(php_regex!("/\u{1b}\\[[^m]*m/"), "", &string); + let string = preg_replace(php_regex!("/\u{1b}\\[[^m]*m/"), "", &string); // remove terminal hyperlinks - let string = shirabe_php_shim::preg_replace( + let string = preg_replace( php_regex!("/\u{1b}]8;[^;]*;[^\u{1b}]*\u{1b}\\\\/"), "", &string, diff --git a/crates/shirabe-symfony-console/src/helper/progress_bar.rs b/crates/shirabe-symfony-console/src/helper/progress_bar.rs index dea1c3ad..1158c507 100644 --- a/crates/shirabe-symfony-console/src/helper/progress_bar.rs +++ b/crates/shirabe-symfony-console/src/helper/progress_bar.rs @@ -9,7 +9,7 @@ use crate::output::OutputInterface; use crate::output::output_interface; use crate::terminal::Terminal; use indexmap::IndexMap; -use shirabe_php_shim::CaptureKey; +use shirabe_php_shim::{CaptureKey, preg_replace_callback}; pub const FORMAT_VERBOSE: &str = "verbose"; pub const FORMAT_VERY_VERBOSE: &str = "very_verbose"; @@ -831,6 +831,6 @@ impl ProgressBar { }) }; - shirabe_php_shim::preg_replace_callback(regex, callback, &format) + preg_replace_callback(regex, callback, &format) } } diff --git a/crates/shirabe-symfony-console/src/input/argv_input.rs b/crates/shirabe-symfony-console/src/input/argv_input.rs index 200c7384..b2b663a6 100644 --- a/crates/shirabe-symfony-console/src/input/argv_input.rs +++ b/crates/shirabe-symfony-console/src/input/argv_input.rs @@ -6,7 +6,7 @@ use crate::input::InputDefinition; use crate::input::InputInterface; use crate::input::StreamableInputInterface; use indexmap::IndexMap; -use shirabe_php_shim::{PhpMixed, php_regex}; +use shirabe_php_shim::{PhpMixed, php_regex, preg_match}; /// ArgvInput represents an input coming from the CLI arguments. /// @@ -524,8 +524,7 @@ impl std::fmt::Display for ArgvInput { .iter() .map(|token| { let mut r#match: Vec<Option<String>> = Vec::new(); - if shirabe_php_shim::preg_match(php_regex!("{^(-[^=]+=)(.+)}"), token, &mut r#match) - { + if preg_match(php_regex!("{^(-[^=]+=)(.+)}"), token, &mut r#match) { return format!( "{}{}", r#match[1].as_deref().unwrap_or(""), diff --git a/crates/shirabe-symfony-console/src/input/input.rs b/crates/shirabe-symfony-console/src/input/input.rs index 7744a4f1..89eedb03 100644 --- a/crates/shirabe-symfony-console/src/input/input.rs +++ b/crates/shirabe-symfony-console/src/input/input.rs @@ -4,7 +4,7 @@ use crate::exception::InvalidArgumentException; use crate::exception::RuntimeException; use crate::input::InputDefinition; use indexmap::IndexMap; -use shirabe_php_shim::{PhpMixed, PhpResource, php_regex}; +use shirabe_php_shim::{PhpMixed, PhpResource, php_regex, preg_match}; /// Input is the base class for all concrete Input classes. /// @@ -208,7 +208,7 @@ impl Input { /// Escapes a token through escapeshellarg if it contains unsafe chars. pub fn escape_token(&self, token: &str) -> String { let mut matches: Vec<Option<String>> = vec![]; - if shirabe_php_shim::preg_match(php_regex!("{^[\\w-]+$}"), token, &mut matches) { + if preg_match(php_regex!("{^[\\w-]+$}"), token, &mut matches) { token.to_string() } else { shirabe_php_shim::escapeshellarg(token) diff --git a/crates/shirabe-symfony-console/src/input/input_option.rs b/crates/shirabe-symfony-console/src/input/input_option.rs index 6734f374..3b24d914 100644 --- a/crates/shirabe-symfony-console/src/input/input_option.rs +++ b/crates/shirabe-symfony-console/src/input/input_option.rs @@ -2,7 +2,7 @@ use crate::exception::InvalidArgumentException; use crate::exception::LogicException; -use shirabe_php_shim::{PhpMixed, php_regex}; +use shirabe_php_shim::{PhpMixed, php_regex, preg_split}; #[derive(Debug, Clone)] pub struct InputOption { @@ -99,7 +99,7 @@ impl InputOption { fn normalize_shortcut(s: String) -> anyhow::Result<Option<String>> { let stripped = shirabe_php_shim::ltrim(&s, Some("-")); - let parts = shirabe_php_shim::preg_split(php_regex!(r"{(\|)-?}"), &stripped); + let parts = preg_split(php_regex!(r"{(\|)-?}"), &stripped); let filtered: Vec<String> = shirabe_php_shim::array_filter(&parts, |s: &String| !s.is_empty()); let result = shirabe_php_shim::implode("|", &filtered); diff --git a/crates/shirabe-symfony-console/src/input/string_input.rs b/crates/shirabe-symfony-console/src/input/string_input.rs index 453bca45..1ec6e853 100644 --- a/crates/shirabe-symfony-console/src/input/string_input.rs +++ b/crates/shirabe-symfony-console/src/input/string_input.rs @@ -6,7 +6,7 @@ use crate::input::InputDefinition; use crate::input::InputInterface; use crate::input::StreamableInputInterface; use indexmap::IndexMap; -use shirabe_php_shim::{CaptureKey, PhpMixed, php_regex}; +use shirabe_php_shim::{CaptureKey, PhpMixed, php_regex, preg_match2}; /// StringInput represents an input provided as a string. /// @@ -58,19 +58,13 @@ impl StringInput { } let mut m: IndexMap<CaptureKey, Option<String>> = IndexMap::new(); - if shirabe_php_shim::preg_match2( - php_regex!(r"/\s+/A"), - input, - &mut m, - 0, - cursor as usize, - ) { + if preg_match2(php_regex!(r"/\s+/A"), input, &mut m, 0, cursor as usize) { if token.is_some() { tokens.push(token.take().unwrap()); } cursor += shirabe_php_shim::strlen(m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or("")); - } else if shirabe_php_shim::preg_match2( + } else if preg_match2( format!(r#"/([^="'\s]+?)(=?)({}+)/A"#, Self::REGEX_QUOTED_STRING), input, &mut m, @@ -93,7 +87,7 @@ impl StringInput { )); cursor += shirabe_php_shim::strlen(m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or("")); - } else if shirabe_php_shim::preg_match2( + } else if preg_match2( format!(r"/{}/A", Self::REGEX_QUOTED_STRING), input, &mut m, @@ -111,7 +105,7 @@ impl StringInput { )); cursor += shirabe_php_shim::strlen(m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or("")); - } else if shirabe_php_shim::preg_match2( + } else if preg_match2( format!(r"/{}/A", Self::REGEX_UNQUOTED_STRING), input, &mut m, diff --git a/crates/shirabe-symfony-console/src/output/stream_output.rs b/crates/shirabe-symfony-console/src/output/stream_output.rs index c183444b..43cee3a8 100644 --- a/crates/shirabe-symfony-console/src/output/stream_output.rs +++ b/crates/shirabe-symfony-console/src/output/stream_output.rs @@ -5,7 +5,7 @@ use crate::formatter::OutputFormatterInterface; use crate::output::OutputInterface; use crate::output::VERBOSITY_NORMAL; use crate::output::{DoWrite, Output}; -use shirabe_php_shim::php_regex; +use shirabe_php_shim::{php_regex, preg_match}; /// StreamOutput writes the output to a given stream. /// @@ -121,7 +121,7 @@ impl StreamOutput { // See https://github.com/chalk/supports-color/blob/d4f413efaf8da045c5ab440ed418ef02dbb28bf1/index.js#L157 let mut matches: Vec<Option<String>> = Vec::new(); - shirabe_php_shim::preg_match( + preg_match( php_regex!( "/^((screen|xterm|vt100|vt220|putty|rxvt|ansi|cygwin|linux).*)|(.*-256(color)?(-bce)?)$/" ), diff --git a/crates/shirabe-symfony-console/src/question/choice_question.rs b/crates/shirabe-symfony-console/src/question/choice_question.rs index 8c0bafc3..b8ac16d8 100644 --- a/crates/shirabe-symfony-console/src/question/choice_question.rs +++ b/crates/shirabe-symfony-console/src/question/choice_question.rs @@ -5,7 +5,7 @@ use crate::exception::LogicException; use crate::question::Question; use crate::question::QuestionInterface; use indexmap::IndexMap; -use shirabe_php_shim::{PhpMixed, php_regex}; +use shirabe_php_shim::{PhpMixed, php_regex, preg_match}; /// Represents a choice question. #[derive(Debug)] @@ -123,7 +123,7 @@ impl ChoiceQuestion { let selected_choices: Vec<PhpMixed> = if multiselect { // Check for a separated comma values let mut matches: Vec<Option<String>> = Vec::new(); - if !shirabe_php_shim::preg_match( + if !preg_match( php_regex!("/^[^,]+(?:,[^,]+)*$/"), &shirabe_php_shim::strval(&selected), &mut matches, diff --git a/crates/shirabe-symfony-console/src/question/confirmation_question.rs b/crates/shirabe-symfony-console/src/question/confirmation_question.rs index 65dd19ab..6ec9281f 100644 --- a/crates/shirabe-symfony-console/src/question/confirmation_question.rs +++ b/crates/shirabe-symfony-console/src/question/confirmation_question.rs @@ -3,7 +3,7 @@ use crate::exception::InvalidArgumentException; use crate::question::Question; use crate::question::QuestionInterface; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{PhpMixed, preg_match}; /// Represents a yes/no question. #[derive(Debug)] @@ -40,11 +40,7 @@ impl ConfirmationQuestion { let answer_is_true = { let mut matches: Vec<Option<String>> = Vec::new(); - shirabe_php_shim::preg_match( - ®ex, - &shirabe_php_shim::strval(&answer), - &mut matches, - ) + preg_match(®ex, &shirabe_php_shim::strval(&answer), &mut matches) }; // false === $default diff --git a/crates/shirabe-symfony-console/src/terminal.rs b/crates/shirabe-symfony-console/src/terminal.rs index 9af712ad..8ff0431a 100644 --- a/crates/shirabe-symfony-console/src/terminal.rs +++ b/crates/shirabe-symfony-console/src/terminal.rs @@ -1,6 +1,6 @@ //! ref: composer/vendor/symfony/console/Terminal.php -use shirabe_php_shim::{PhpMixed, php_regex}; +use shirabe_php_shim::{PhpMixed, php_regex, preg_match}; use std::cell::Cell; thread_local! { @@ -81,7 +81,7 @@ impl Terminal { let ansicon = shirabe_php_shim::getenv("ANSICON"); let mut matches: Vec<Option<String>> = Vec::new(); if let Some(ansicon) = &ansicon - && shirabe_php_shim::preg_match( + && preg_match( php_regex!("/^(\\d+)x(\\d+)(?: \\((\\d+)x(\\d+)\\))?$/"), &shirabe_php_shim::trim(&ansicon.to_string_lossy(), None), &mut matches, @@ -138,7 +138,7 @@ impl Terminal { return; } let mut matches: Vec<Option<String>> = Vec::new(); - if shirabe_php_shim::preg_match( + if preg_match( php_regex!("/rows.(\\d+);.columns.(\\d+);/i"), &stty_string, &mut matches, @@ -154,7 +154,7 @@ impl Terminal { matches[1].clone().unwrap_or_default(), )))) }); - } else if shirabe_php_shim::preg_match( + } else if preg_match( php_regex!("/;.(\\d+).rows;.(\\d+).columns/i"), &stty_string, &mut matches, @@ -182,7 +182,7 @@ impl Terminal { let info = info?; let mut matches: Vec<Option<String>> = Vec::new(); - if !shirabe_php_shim::preg_match( + if !preg_match( php_regex!("/--------+\\r?\\n.+?(\\d+)\\r?\\n.+?(\\d+)\\r?\\n/"), &info, &mut matches, diff --git a/crates/shirabe-symfony-process/src/process.rs b/crates/shirabe-symfony-process/src/process.rs index 556238cf..5307e1ae 100644 --- a/crates/shirabe-symfony-process/src/process.rs +++ b/crates/shirabe-symfony-process/src/process.rs @@ -11,7 +11,10 @@ use crate::pipes::unix_pipes::UnixPipes; use crate::pipes::windows_pipes::WindowsPipes; use crate::process_utils::ProcessUtils; use indexmap::IndexMap; -use shirabe_php_shim::{CaptureKey, Descriptor, PhpMixed, PhpResource, php_regex}; +use shirabe_php_shim::{ + CaptureKey, Descriptor, PhpMixed, PhpResource, php_regex, preg_match, preg_replace, + preg_replace_callback, +}; use std::sync::OnceLock; /// A user-supplied callback invoked with the output type ("out"/"err") and a chunk of output. @@ -925,7 +928,7 @@ impl Process { let uid = shirabe_php_shim::uniqid("", true); let mut var_count = 0; let mut var_cache: IndexMap<String, String> = IndexMap::new(); - let cmd = shirabe_php_shim::preg_replace_callback( + let cmd = preg_replace_callback( php_regex!( r#"/"(?:( [^"%!^]*+ @@ -963,7 +966,7 @@ impl Process { } value = format!( "\"{}\"", - shirabe_php_shim::preg_replace(php_regex!(r#"/(\\*)"/"#), "$1$1\\\"", &value) + preg_replace(php_regex!(r#"/(\\*)"/"#), "$1$1\\\"", &value) ); var_count += 1; let var = format!("{}{}", uid, var_count); @@ -985,11 +988,7 @@ impl Process { .map(|spec| { format!( "\"{}\"", - shirabe_php_shim::preg_replace( - php_regex!(r#"{(\\*+)"}"#), - "$1$1\\\"", - &spec, - ) + preg_replace(php_regex!(r#"{(\\*+)"}"#), "$1$1\\\"", &spec,) ) }) }) @@ -1044,14 +1043,14 @@ impl Process { if argument.contains('\0') { argument = argument.replace('\0', "?"); } - if !shirabe_php_shim::preg_match( + if !preg_match( php_regex!(r#"/[()%!^"<>&|\s\[\]=;*?'$]/"#), &argument, &mut Vec::new(), ) { return argument; } - argument = shirabe_php_shim::preg_replace(php_regex!(r"/(\\+)$/"), "$1$1", &argument); + argument = preg_replace(php_regex!(r"/(\\+)$/"), "$1$1", &argument); let mut result = argument; for (from, to) in [ @@ -1071,7 +1070,7 @@ impl Process { commandline: &str, env: &IndexMap<String, PhpMixed>, ) -> anyhow::Result<String> { - shirabe_php_shim::preg_replace_callback( + preg_replace_callback( php_regex!(r#"/"\$\{:([_a-zA-Z]+[_a-zA-Z0-9]*)\}"/"#), |matches: &IndexMap<CaptureKey, Option<String>>| -> anyhow::Result<String> { let key = matches diff --git a/crates/shirabe/src/command/show_command.rs b/crates/shirabe/src/command/show_command.rs index ead034da..d02295d5 100644 --- a/crates/shirabe/src/command/show_command.rs +++ b/crates/shirabe/src/command/show_command.rs @@ -40,7 +40,7 @@ use shirabe_pcre::{CaptureKey, Preg}; use shirabe_php_shim::{ CmpOp, DATE_ATOM, InvalidArgumentException, LogicException, PhpMixed, UnexpectedValueException, array_search, date_format_to_strftime, date_local, extension_loaded, impl_php_class, - in_array_loose, in_array_strict, php_regex, realpath, strtolower, version_compare, + in_array_loose, in_array_strict, php_regex, preg_quote, realpath, strtolower, version_compare, }; use shirabe_semver::Semver; use shirabe_semver::constraint::AnyConstraint; @@ -2279,7 +2279,7 @@ impl Command for ShowCommand { let mut packages: IndexMap<String, IndexMap<String, PackageOrName>> = IndexMap::new(); let mut package_filter_regex: Option<String> = None; if let Some(ref pf) = package_filter { - let escaped = shirabe_php_shim::preg_quote(pf, None); + let escaped = preg_quote(pf, None); package_filter_regex = Some(format!("{{^{}$}}i", escaped.replace("\\*", ".*?"))); } diff --git a/crates/shirabe/src/console/application.rs b/crates/shirabe/src/console/application.rs index 04819b81..9033700b 100644 --- a/crates/shirabe/src/console/application.rs +++ b/crates/shirabe/src/console/application.rs @@ -61,8 +61,8 @@ use shirabe_php_shim::{ dirname, disk_free_space, extension_loaded, file_exists, file_get_contents, file_put_contents, function_exists, getcwd, getmypid, glob, ini_set, is_array, is_dir, is_file, is_string, json_decode_assoc, memory_get_peak_usage, memory_get_usage, microtime, php_regex, php_uname, - posix_getuid, random_bytes, realpath, restore_error_handler, round, str_replace, strpos, - strtoupper, sys_get_temp_dir, time, unlink, + posix_getuid, preg_grep, preg_match2, preg_quote, preg_split, random_bytes, realpath, + restore_error_handler, round, str_replace, strpos, strtoupper, sys_get_temp_dir, time, unlink, }; use shirabe_seld_json_lint::ParsingException; use shirabe_symfony_console::application::Application as BaseApplication; @@ -950,10 +950,10 @@ impl Application { // implode('[^:]*:', array_map('preg_quote', explode(':', $namespace))).'[^:]*' let parts: Vec<String> = shirabe_php_shim::explode(":", namespace) .into_iter() - .map(|p| shirabe_php_shim::preg_quote(&p, None)) + .map(|p| preg_quote(&p, None)) .collect(); let expr = format!("{}{}", shirabe_php_shim::implode("[^:]*:", &parts), "[^:]*"); - let namespaces = shirabe_php_shim::preg_grep(format!("{{^{}}}", expr), &all_namespaces); + let namespaces = preg_grep(format!("{{^{}}}", expr), &all_namespaces); if namespaces.is_empty() { let mut message = format!( @@ -1046,19 +1046,17 @@ impl Application { let parts: Vec<String> = shirabe_php_shim::explode(":", name) .into_iter() - .map(|p| shirabe_php_shim::preg_quote(&p, None)) + .map(|p| preg_quote(&p, None)) .collect(); let expr = format!("{}{}", shirabe_php_shim::implode("[^:]*:", &parts), "[^:]*"); - let mut commands = shirabe_php_shim::preg_grep(format!("{{^{}}}", expr), &all_commands); + let mut commands = preg_grep(format!("{{^{}}}", expr), &all_commands); if commands.is_empty() { - commands = shirabe_php_shim::preg_grep(format!("{{^{}}}i", expr), &all_commands); + commands = preg_grep(format!("{{^{}}}i", expr), &all_commands); } // if no commands matched or we just matched namespaces - if commands.is_empty() - || shirabe_php_shim::preg_grep(format!("{{^{}$}}i", expr), &commands).is_empty() - { + if commands.is_empty() || preg_grep(format!("{{^{}$}}i", expr), &commands).is_empty() { if let Some(pos) = shirabe_php_shim::strrpos(name, ":") { // check if a namespace exists and contains commands self.find_namespace(&name[..pos])?; @@ -1344,7 +1342,7 @@ impl Application { }; let mut lines: Vec<(String, i64)> = Vec::new(); let split = if !message.is_empty() { - shirabe_php_shim::preg_split(php_regex!(r"/\r?\n/"), &message) + preg_split(php_regex!(r"/\r?\n/"), &message) } else { Vec::new() }; @@ -1788,7 +1786,7 @@ impl Application { let mut offset = 0i64; let mut m: indexmap::IndexMap<shirabe_php_shim::CaptureKey, Option<String>> = indexmap::IndexMap::new(); - while shirabe_php_shim::preg_match2( + while preg_match2( php_regex!(r"/.{1,10000}/u"), &utf8_string, &mut m, |
