echo: add support for \NNN octal escape sequence

This commit is contained in:
Terts Diepraam 2023-08-29 21:54:19 +02:00
parent 4623575a66
commit 1ad10dd371
2 changed files with 37 additions and 0 deletions

View file

@ -54,6 +54,16 @@ fn print_escaped(input: &str, mut output: impl Write) -> io::Result<bool> {
continue;
}
// This is for the \NNN syntax for octal sequences.
// Note that '0' is intentionally omitted because that
// would be the \0NNN syntax.
if let Some('1'..='8') = iter.peek() {
if let Some(parsed) = parse_code(&mut iter, 8, 3) {
write!(output, "{parsed}")?;
continue;
}
}
if let Some(next) = iter.next() {
let unescaped = match next {
'\\' => '\\',

View file

@ -253,3 +253,30 @@ fn wrapping_octal() {
.succeeds()
.stdout_is("A\n");
}
#[test]
fn old_octal_syntax() {
new_ucmd!()
.arg("-e")
.arg("\\1foo")
.succeeds()
.stdout_is("\x01foo\n");
new_ucmd!()
.arg("-e")
.arg("\\43foo")
.succeeds()
.stdout_is("#foo\n");
new_ucmd!()
.arg("-e")
.arg("\\101foo")
.succeeds()
.stdout_is("Afoo\n");
new_ucmd!()
.arg("-e")
.arg("\\1011")
.succeeds()
.stdout_is("A1\n");
}