mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
expr: Handle trailing backslash error
This commit is contained in:
parent
cd9ce77098
commit
4555e6fe48
3 changed files with 28 additions and 0 deletions
|
@ -50,6 +50,8 @@ pub enum ExprError {
|
||||||
UnmatchedClosingBrace,
|
UnmatchedClosingBrace,
|
||||||
#[error("Invalid content of \\{{\\}}")]
|
#[error("Invalid content of \\{{\\}}")]
|
||||||
InvalidBracketContent,
|
InvalidBracketContent,
|
||||||
|
#[error("Trailing backslash")]
|
||||||
|
TrailingBackslash,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UError for ExprError {
|
impl UError for ExprError {
|
||||||
|
|
|
@ -161,6 +161,7 @@ impl StringOp {
|
||||||
match first {
|
match first {
|
||||||
Some('^') => {} // Start of string anchor is already added
|
Some('^') => {} // Start of string anchor is already added
|
||||||
Some('*') => re_string.push_str(r"\*"),
|
Some('*') => re_string.push_str(r"\*"),
|
||||||
|
Some('\\') if right.len() == 1 => return Err(ExprError::TrailingBackslash),
|
||||||
Some(char) => re_string.push(char),
|
Some(char) => re_string.push(char),
|
||||||
None => return Ok(0.into()),
|
None => return Ok(0.into()),
|
||||||
};
|
};
|
||||||
|
@ -169,6 +170,8 @@ impl StringOp {
|
||||||
let mut prev = first.unwrap_or_default();
|
let mut prev = first.unwrap_or_default();
|
||||||
let mut prev_is_escaped = false;
|
let mut prev_is_escaped = false;
|
||||||
while let Some(curr) = pattern_chars.next() {
|
while let Some(curr) = pattern_chars.next() {
|
||||||
|
let curr_is_escaped = prev == '\\' && !prev_is_escaped;
|
||||||
|
|
||||||
match curr {
|
match curr {
|
||||||
'^' => match (prev, prev_is_escaped) {
|
'^' => match (prev, prev_is_escaped) {
|
||||||
// Start of a capturing group
|
// Start of a capturing group
|
||||||
|
@ -201,6 +204,9 @@ impl StringOp {
|
||||||
re_string.push('$');
|
re_string.push('$');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
'\\' if !curr_is_escaped && pattern_chars.peek().is_none() => {
|
||||||
|
return Err(ExprError::TrailingBackslash);
|
||||||
|
}
|
||||||
_ => re_string.push(curr),
|
_ => re_string.push(curr),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -365,6 +365,26 @@ fn test_regex() {
|
||||||
.stdout_only("0\n");
|
.stdout_only("0\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_regex_trailing_backslash() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["\\", ":", "\\\\"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("1\n");
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["\\", ":", "\\"])
|
||||||
|
.fails()
|
||||||
|
.stderr_only("expr: Trailing backslash\n");
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["abc\\", ":", "abc\\\\"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("4\n");
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["abc\\", ":", "abc\\"])
|
||||||
|
.fails()
|
||||||
|
.stderr_only("expr: Trailing backslash\n");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_substr() {
|
fn test_substr() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue