]> git.proxmox.com Git - rustc.git/blame - library/std/src/sys_common/bytestring.rs
New upstream version 1.54.0+dfsg1
[rustc.git] / library / std / src / sys_common / bytestring.rs
CommitLineData
ff7c6d11
XL
1#![allow(dead_code)]
2
1b1a35ee
XL
3#[cfg(test)]
4mod tests;
5
532ac7d7 6use crate::fmt::{Formatter, Result, Write};
83c7162d 7use core::str::lossy::{Utf8Lossy, Utf8LossyChunk};
ff7c6d11 8
532ac7d7 9pub fn debug_fmt_bytestring(slice: &[u8], f: &mut Formatter<'_>) -> Result {
ff7c6d11 10 // Writes out a valid unicode string with the correct escape sequences
532ac7d7 11 fn write_str_escaped(f: &mut Formatter<'_>, s: &str) -> Result {
ff7c6d11
XL
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}