mirror of
https://github.com/uutils/coreutils
synced 2024-12-13 14:52:41 +00:00
parent
165bbc272a
commit
a8b9e042e1
2 changed files with 30 additions and 18 deletions
|
@ -99,25 +99,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
|
|
||||||
if let Some(first) = sets.first() {
|
if let Some(first) = sets.first() {
|
||||||
let slice = os_str_as_bytes(first)?;
|
let slice = os_str_as_bytes(first)?;
|
||||||
|
let trailing_backslashes = slice.iter().rev().take_while(|&&c| c == b'\\').count();
|
||||||
let mut iter = slice.iter();
|
if trailing_backslashes % 2 == 1 {
|
||||||
|
// The trailing backslash has a non-backslash character before it.
|
||||||
if let Some(b'\\') = iter.next_back() {
|
|
||||||
match iter.next_back() {
|
|
||||||
Some(b'\\') => {
|
|
||||||
// The trailing backslash has a backslash preceding it, so it is properly escaped
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
// The trailing backslash has a non-backslash character before it OR is the only character in the
|
|
||||||
// string, so the warning applies
|
|
||||||
show!(USimpleError::new(
|
show!(USimpleError::new(
|
||||||
0,
|
0,
|
||||||
"warning: an unescaped backslash at end of string is not portable"
|
"warning: an unescaped backslash at end of string is not portable"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let stdin = stdin();
|
let stdin = stdin();
|
||||||
let mut locked_stdin = stdin.lock();
|
let mut locked_stdin = stdin.lock();
|
||||||
|
|
|
@ -1456,15 +1456,37 @@ fn test_unescaped_backslash_warning_false_positive() {
|
||||||
.pipe_in(r"a\b\c\")
|
.pipe_in(r"a\b\c\")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only("abc");
|
.stdout_only("abc");
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["-d", r"\\\\"])
|
||||||
|
.pipe_in(r"a\b\c\")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("abc");
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["-d", r"\\\\\\"])
|
||||||
|
.pipe_in(r"a\b\c\")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("abc");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_trailing_backslash_is_only_input_character() {
|
fn test_trailing_backslash() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&["-d", r"\"])
|
.args(&["-d", r"\"])
|
||||||
.pipe_in(r"a\b\c\")
|
.pipe_in(r"a\b\c\")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stderr_is("tr: warning: an unescaped backslash at end of string is not portable\n")
|
.stderr_is("tr: warning: an unescaped backslash at end of string is not portable\n")
|
||||||
.stdout_is("abc");
|
.stdout_is("abc");
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["-d", r"\\\"])
|
||||||
|
.pipe_in(r"a\b\c\")
|
||||||
|
.succeeds()
|
||||||
|
.stderr_is("tr: warning: an unescaped backslash at end of string is not portable\n")
|
||||||
|
.stdout_is("abc");
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["-d", r"\\\\\"])
|
||||||
|
.pipe_in(r"a\b\c\")
|
||||||
|
.succeeds()
|
||||||
|
.stderr_is("tr: warning: an unescaped backslash at end of string is not portable\n")
|
||||||
|
.stdout_is("abc");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue