From d83cda616b4c1ec3cdb78d6edc46115f4d58f872 Mon Sep 17 00:00:00 2001 From: pagedmov Date: Fri, 20 Mar 2026 13:43:38 -0400 Subject: [PATCH] Replace hand-rolled glob_to_regex with fnmatch_regex crate and remove unused mut --- src/expand.rs | 50 +++++++++++---------------------------------- src/readline/mod.rs | 2 +- 2 files changed, 13 insertions(+), 39 deletions(-) diff --git a/src/expand.rs b/src/expand.rs index a3b401d..d2eaae0 100644 --- a/src/expand.rs +++ b/src/expand.rs @@ -2030,44 +2030,18 @@ pub fn expand_case_pattern(raw: &str) -> ShResult { } pub fn glob_to_regex(glob: &str, anchored: bool) -> Regex { - let mut regex = String::new(); - if anchored { - regex.push('^'); - } - let mut chars = glob.chars(); - while let Some(ch) = chars.next() { - match ch { - '\\' => { - // Shell escape: next char is literal - if let Some(esc) = chars.next() { - // Some characters have special meaning after \ in regex - // (e.g. \< is word boundary), so use hex escape for safety - regex.push_str(&format!("\\x{:02x}", esc as u32)); - } - } - '*' => regex.push_str(".*"), - '?' => regex.push('.'), - '[' => { - // Pass through character class [...] as-is (glob and regex syntax match) - regex.push('['); - while let Some(bc) = chars.next() { - regex.push(bc); - if bc == ']' { - break; - } - } - } - '.' | '+' | '(' | ')' | '|' | '^' | '$' | '{' | '}' => { - regex.push('\\'); - regex.push(ch); - } - _ => regex.push(ch), - } - } - if anchored { - regex.push('$'); - } - Regex::new(®ex).unwrap() + // fnmatch_regex always produces ^...$, so get the pattern string and strip if unanchored + let pattern = fnmatch_regex::glob_to_regex_pattern(glob) + .unwrap_or_else(|_| regex::escape(glob)); + let pattern = if anchored { + pattern + } else { + pattern + .strip_prefix('^').unwrap_or(&pattern) + .strip_suffix('$').unwrap_or(&pattern) + .to_string() + }; + Regex::new(&pattern).unwrap() } #[derive(Debug)] diff --git a/src/readline/mod.rs b/src/readline/mod.rs index 67bf0be..e37c118 100644 --- a/src/readline/mod.rs +++ b/src/readline/mod.rs @@ -860,7 +860,7 @@ impl ShedVi { return Ok(None); }; - let Some(mut cmd) = cmd else { + let Some(cmd) = cmd else { return Ok(None); }; if self.should_grab_history(&cmd) {