Skip to content

Commit 1cfd0b7

Browse files
committed
Match <OsString as Debug>::fmt to that of str
819247f changed <str as Debug>::fmt such that it does not escape single quotes, but neglected to apply the same choice to OsString. This commit does that.
1 parent 0df64c5 commit 1cfd0b7

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

library/core/src/str/lossy.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::char::EscapeDebugExtArgs;
12
use super::from_utf8_unchecked;
23
use super::validations::utf8_char_width;
34
use crate::fmt;
@@ -121,7 +122,11 @@ impl fmt::Debug for Debug<'_> {
121122
let valid = chunk.valid();
122123
let mut from = 0;
123124
for (i, c) in valid.char_indices() {
124-
let esc = c.escape_debug();
125+
let esc = c.escape_debug_ext(EscapeDebugExtArgs {
126+
escape_grapheme_extended: true,
127+
escape_single_quote: false,
128+
escape_double_quote: true,
129+
});
125130
// If char needs escaping, flush backlog so far and write, else skip
126131
if esc.len() != 1 {
127132
f.write_str(&valid[from..i])?;

library/core/src/wtf8.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// implementations, so, we'll have to add more doc(hidden)s anyway
2020
#![doc(hidden)]
2121

22-
use crate::char::encode_utf16_raw;
22+
use crate::char::{EscapeDebugExtArgs, encode_utf16_raw};
2323
use crate::clone::CloneToUninit;
2424
use crate::fmt::{self, Write};
2525
use crate::hash::{Hash, Hasher};
@@ -144,14 +144,20 @@ impl AsRef<[u8]> for Wtf8 {
144144
impl fmt::Debug for Wtf8 {
145145
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
146146
fn write_str_escaped(f: &mut fmt::Formatter<'_>, s: &str) -> fmt::Result {
147-
use crate::fmt::Write;
148-
for c in s.chars().flat_map(|c| c.escape_debug()) {
147+
use crate::fmt::Write as _;
148+
for c in s.chars().flat_map(|c| {
149+
c.escape_debug_ext(EscapeDebugExtArgs {
150+
escape_grapheme_extended: true,
151+
escape_single_quote: false,
152+
escape_double_quote: true,
153+
})
154+
}) {
149155
f.write_char(c)?
150156
}
151157
Ok(())
152158
}
153159

154-
formatter.write_str("\"")?;
160+
formatter.write_char('"')?;
155161
let mut pos = 0;
156162
while let Some((surrogate_pos, surrogate)) = self.next_surrogate(pos) {
157163
// SAFETY: next_surrogate provides an index for a range of valid UTF-8 bytes.
@@ -164,7 +170,7 @@ impl fmt::Debug for Wtf8 {
164170

165171
// SAFETY: after next_surrogate returns None, the remainder is valid UTF-8.
166172
write_str_escaped(formatter, unsafe { str::from_utf8_unchecked(&self.bytes[pos..]) })?;
167-
formatter.write_str("\"")
173+
formatter.write_char('"')
168174
}
169175
}
170176

library/coretests/tests/str_lossy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,5 @@ fn debug() {
8080
b"Hello\xC0\x80 There\xE6\x83 Goodbye\xf4\x8d\x93\xaa".utf8_chunks().debug(),
8181
),
8282
);
83+
assert_eq!("\"'\"", &format!("{:?}", b"'".utf8_chunks().debug()));
8384
}

library/std/src/ffi/os_str/tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,9 @@ fn clone_to_uninit() {
303303
unsafe { a.clone_to_uninit(ptr::from_mut::<OsStr>(&mut b).cast()) };
304304
assert_eq!(a, &*b);
305305
}
306+
307+
#[test]
308+
fn debug() {
309+
let s = "'single quotes'";
310+
assert_eq!(format!("{:?}", OsStr::new(s)), format!("{:?}", s));
311+
}

0 commit comments

Comments
 (0)