3 use crate::fmt::{Formatter, Result, Write};
4 use core::str::lossy::{Utf8Lossy, Utf8LossyChunk};
6 pub fn debug_fmt_bytestring(slice: &[u8], f: &mut Formatter<'_>) -> Result {
7 // Writes out a valid unicode string with the correct escape sequences
8 fn write_str_escaped(f: &mut Formatter<'_>, s: &str) -> Result {
9 for c in s.chars().flat_map(|c| c.escape_debug()) {
16 for Utf8LossyChunk { valid, broken } in Utf8Lossy::from_bytes(slice).chunks() {
17 write_str_escaped(f, valid)?;
19 write!(f, "\\x{:02X}", b)?;
28 use crate::fmt::{Debug, Formatter, Result};
32 struct Helper<'a>(&'a [u8]);
34 impl Debug for Helper<'_> {
35 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
36 debug_fmt_bytestring(self.0, f)
40 let input = b"\xF0hello,\tworld";
41 let expected = r#""\xF0hello,\tworld""#;
42 let output = format!("{:?}", Helper(input));
44 assert!(output == expected);