Skip to content

Commit 059008f

Browse files
committed
Merge sys_common::bytestring into os_str_bytes
1 parent 3824017 commit 059008f

File tree

5 files changed

+28
-50
lines changed

5 files changed

+28
-50
lines changed

library/std/src/sys_common/bytestring.rs

-26
This file was deleted.

library/std/src/sys_common/bytestring/tests.rs

-19
This file was deleted.

library/std/src/sys_common/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
mod tests;
2222

2323
pub mod backtrace;
24-
pub mod bytestring;
2524
pub mod condvar;
2625
pub mod fs;
2726
pub mod io;

library/std/src/sys_common/os_str_bytes.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
//! systems: just a `Vec<u8>`/`[u8]`.
33
44
use crate::borrow::Cow;
5-
65
use crate::fmt;
6+
use crate::fmt::Write;
77
use crate::mem;
88
use crate::rc::Rc;
99
use crate::str;
1010
use crate::sync::Arc;
11-
use crate::sys_common::bytestring::debug_fmt_bytestring;
1211
use crate::sys_common::{AsInner, IntoInner};
1312

14-
use core::str::lossy::Utf8Lossy;
13+
use core::str::lossy::{Utf8Lossy, Utf8LossyChunk};
14+
15+
#[cfg(test)]
16+
mod tests;
1517

1618
#[derive(Hash)]
1719
#[repr(transparent)]
@@ -26,7 +28,19 @@ pub struct Slice {
2628

2729
impl fmt::Debug for Slice {
2830
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
29-
debug_fmt_bytestring(&self.inner, formatter)
31+
// Writes out a valid unicode string with the correct escape sequences
32+
33+
formatter.write_str("\"")?;
34+
for Utf8LossyChunk { valid, broken } in Utf8Lossy::from_bytes(&self.inner).chunks() {
35+
for c in valid.chars().flat_map(|c| c.escape_debug()) {
36+
formatter.write_char(c)?
37+
}
38+
39+
for b in broken {
40+
write!(formatter, "\\x{:02X}", b)?;
41+
}
42+
}
43+
formatter.write_str("\"")
3044
}
3145
}
3246

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use super::*;
2+
3+
#[test]
4+
fn slice_debug_output() {
5+
let input = Slice::from_u8_slice(b"\xF0hello,\tworld");
6+
let expected = r#""\xF0hello,\tworld""#;
7+
let output = format!("{:?}", input);
8+
9+
assert_eq!(output, expected);
10+
}

0 commit comments

Comments
 (0)