Rollup merge of #85001 - CDirkx:bytestring, r=JohnTitor
Merge `sys_common::bytestring` back into `os_str_bytes` `bytestring` contains code for correctly debug formatting a byte slice (`[u8]`). This functionality is and has historically only been used to provide the debug formatting of byte-based os-strings (on unix etc.). Having this functionality in the separate `bytestring` module was useful in the past to reduce duplication, as [when it was added](https://github.com/rust-lang/rust/pull/46798) `os_str_bytes` was still split into `sys::{unix, redox, wasi, etc.}::os_str`. However, now that is no longer the case, there is not much reason for the `bytestring` functionality to be separate from `os_str_bytes`; I don't think it is very likely that another part of std will need to handle formatting byte strings that are not os-strings in the future (everything should be `utf8`). This is why this PR merges the functionality of `bytestring` directly into the debug implementation in `os_str_bytes`.
This commit is contained in:
commit
fb736d96c3
@ -1,26 +0,0 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use crate::fmt::{Formatter, Result, Write};
|
||||
use core::str::lossy::{Utf8Lossy, Utf8LossyChunk};
|
||||
|
||||
pub fn debug_fmt_bytestring(slice: &[u8], f: &mut Formatter<'_>) -> Result {
|
||||
// Writes out a valid unicode string with the correct escape sequences
|
||||
fn write_str_escaped(f: &mut Formatter<'_>, s: &str) -> Result {
|
||||
for c in s.chars().flat_map(|c| c.escape_debug()) {
|
||||
f.write_char(c)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
f.write_str("\"")?;
|
||||
for Utf8LossyChunk { valid, broken } in Utf8Lossy::from_bytes(slice).chunks() {
|
||||
write_str_escaped(f, valid)?;
|
||||
for b in broken {
|
||||
write!(f, "\\x{:02X}", b)?;
|
||||
}
|
||||
}
|
||||
f.write_str("\"")
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
use super::*;
|
||||
use crate::fmt::{Debug, Formatter, Result};
|
||||
|
||||
#[test]
|
||||
fn smoke() {
|
||||
struct Helper<'a>(&'a [u8]);
|
||||
|
||||
impl Debug for Helper<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
||||
debug_fmt_bytestring(self.0, f)
|
||||
}
|
||||
}
|
||||
|
||||
let input = b"\xF0hello,\tworld";
|
||||
let expected = r#""\xF0hello,\tworld""#;
|
||||
let output = format!("{:?}", Helper(input));
|
||||
|
||||
assert!(output == expected);
|
||||
}
|
@ -21,7 +21,6 @@
|
||||
mod tests;
|
||||
|
||||
pub mod backtrace;
|
||||
pub mod bytestring;
|
||||
pub mod condvar;
|
||||
pub mod fs;
|
||||
pub mod io;
|
||||
|
@ -2,16 +2,18 @@
|
||||
//! systems: just a `Vec<u8>`/`[u8]`.
|
||||
|
||||
use crate::borrow::Cow;
|
||||
|
||||
use crate::fmt;
|
||||
use crate::fmt::Write;
|
||||
use crate::mem;
|
||||
use crate::rc::Rc;
|
||||
use crate::str;
|
||||
use crate::sync::Arc;
|
||||
use crate::sys_common::bytestring::debug_fmt_bytestring;
|
||||
use crate::sys_common::{AsInner, IntoInner};
|
||||
|
||||
use core::str::lossy::Utf8Lossy;
|
||||
use core::str::lossy::{Utf8Lossy, Utf8LossyChunk};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[derive(Hash)]
|
||||
#[repr(transparent)]
|
||||
@ -26,7 +28,19 @@ pub struct Slice {
|
||||
|
||||
impl fmt::Debug for Slice {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
debug_fmt_bytestring(&self.inner, formatter)
|
||||
// Writes out a valid unicode string with the correct escape sequences
|
||||
|
||||
formatter.write_str("\"")?;
|
||||
for Utf8LossyChunk { valid, broken } in Utf8Lossy::from_bytes(&self.inner).chunks() {
|
||||
for c in valid.chars().flat_map(|c| c.escape_debug()) {
|
||||
formatter.write_char(c)?
|
||||
}
|
||||
|
||||
for b in broken {
|
||||
write!(formatter, "\\x{:02X}", b)?;
|
||||
}
|
||||
}
|
||||
formatter.write_str("\"")
|
||||
}
|
||||
}
|
||||
|
||||
|
10
library/std/src/sys_common/os_str_bytes/tests.rs
Normal file
10
library/std/src/sys_common/os_str_bytes/tests.rs
Normal file
@ -0,0 +1,10 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn slice_debug_output() {
|
||||
let input = Slice::from_u8_slice(b"\xF0hello,\tworld");
|
||||
let expected = r#""\xF0hello,\tworld""#;
|
||||
let output = format!("{:?}", input);
|
||||
|
||||
assert_eq!(output, expected);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user