]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys_common/bytestring.rs
Auto merge of #86166 - tmiasko:no-alloca-for-zsts, r=nagisa
[rust.git] / library / std / src / sys_common / bytestring.rs
1 #![allow(dead_code)]
2
3 #[cfg(test)]
4 mod tests;
5
6 use crate::fmt::{Formatter, Result, Write};
7 use core::str::lossy::{Utf8Lossy, Utf8LossyChunk};
8
9 pub fn debug_fmt_bytestring(slice: &[u8], f: &mut Formatter<'_>) -> Result {
10     // Writes out a valid unicode string with the correct escape sequences
11     fn write_str_escaped(f: &mut Formatter<'_>, s: &str) -> Result {
12         for c in s.chars().flat_map(|c| c.escape_debug()) {
13             f.write_char(c)?
14         }
15         Ok(())
16     }
17
18     f.write_str("\"")?;
19     for Utf8LossyChunk { valid, broken } in Utf8Lossy::from_bytes(slice).chunks() {
20         write_str_escaped(f, valid)?;
21         for b in broken {
22             write!(f, "\\x{:02X}", b)?;
23         }
24     }
25     f.write_str("\"")
26 }