Replace hand-rolled glob_to_regex with fnmatch_regex crate and remove unused mut
This commit is contained in:
@@ -2030,44 +2030,18 @@ pub fn expand_case_pattern(raw: &str) -> ShResult<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn glob_to_regex(glob: &str, anchored: bool) -> Regex {
|
pub fn glob_to_regex(glob: &str, anchored: bool) -> Regex {
|
||||||
let mut regex = String::new();
|
// fnmatch_regex always produces ^...$, so get the pattern string and strip if unanchored
|
||||||
if anchored {
|
let pattern = fnmatch_regex::glob_to_regex_pattern(glob)
|
||||||
regex.push('^');
|
.unwrap_or_else(|_| regex::escape(glob));
|
||||||
}
|
let pattern = if anchored {
|
||||||
let mut chars = glob.chars();
|
pattern
|
||||||
while let Some(ch) = chars.next() {
|
} else {
|
||||||
match ch {
|
pattern
|
||||||
'\\' => {
|
.strip_prefix('^').unwrap_or(&pattern)
|
||||||
// Shell escape: next char is literal
|
.strip_suffix('$').unwrap_or(&pattern)
|
||||||
if let Some(esc) = chars.next() {
|
.to_string()
|
||||||
// Some characters have special meaning after \ in regex
|
};
|
||||||
// (e.g. \< is word boundary), so use hex escape for safety
|
Regex::new(&pattern).unwrap()
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|||||||
@@ -860,7 +860,7 @@ impl ShedVi {
|
|||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
|
|
||||||
let Some(mut cmd) = cmd else {
|
let Some(cmd) = cmd else {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
if self.should_grab_history(&cmd) {
|
if self.should_grab_history(&cmd) {
|
||||||
|
|||||||
Reference in New Issue
Block a user