Skip to content

Commit a010633

Browse files
committed
fix: list non-ascii as octal bytes
1 parent f083fa0 commit a010633

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

‎src/sed/processor.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,13 @@ fn list(output: &mut OutputBuffer, line: &IOChunk, max_width: usize) -> UResult<
392392
'\t' => Cow::Borrowed(r"\t"),
393393
c if c.is_ascii_control() => Cow::Owned(format!("\\{:03o}", ch as u8)),
394394
c if c == ' ' || c.is_ascii_graphic() => Cow::Borrowed(ch.encode_utf8(&mut char_buff)),
395-
c if (c as u32) <= 0xFFFF => Cow::Owned(format!("\\u{:04X}", c as u32)),
396-
_ => Cow::Owned(format!("\\U{:08X}", ch as u32)),
395+
_ => Cow::Owned(
396+
ch.to_string()
397+
.as_bytes()
398+
.iter()
399+
.map(|b| format!("\\{b:03o}"))
400+
.collect(),
401+
),
397402
};
398403

399404
// See if folding is required before adding out_str and terminator.

‎tests/by-util/test_sed.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,15 @@ check_output!(list_ascii, ["-n", "l 60", "input/ascii"]);
829829
check_output!(list_empty, ["-n", "l 60", "input/empty"]);
830830
check_output!(list_unicode, ["l 60", "input/unicode"]);
831831

832+
#[test]
833+
fn list_non_ascii_uses_octal_bytes() {
834+
new_ucmd!()
835+
.arg("l")
836+
.pipe_in("cÃ🧰y\n")
837+
.succeeds()
838+
.stdout_is("c\\303\\203\\360\\237\\247\\260y$\ncÃ🧰y\n");
839+
}
840+
832841
////////////////////////////////////////////////////////////
833842
// In-place editing
834843
#[test]
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Hello World or \u039A\u03B1\u03BB\u03B7\u03BC\u03AD\u03C1\
2-
\u03B1 \u03BA\u03CC\u03C3\u03BC\u03B5 or \u3053\u3093\u306B\
3-
\u3061\u306F \u4E16\u754C \U0001F600$
1+
Hello World or \316\232\316\261\316\273\316\267\316\274\
2+
\316\255\317\201\316\261 \316\272\317\214\317\203\316\274\
3+
\316\265 or \343\201\223\343\202\223\343\201\253\
4+
\343\201\241\343\201\257 \344\270\226\347\225\214 \
5+
\360\237\230\200$
46
Hello World or Καλημέρα κόσμε or こんにちは 世界 😀

0 commit comments

Comments
 (0)