Skip to content

Commit 48fa913

Browse files
authored
Rollup merge of #148798 - tamird:esc-single-quote, r=Amanieu
Match <OsString as Debug>::fmt to that of str Fixes #114583.
2 parents 8b74790 + eb84efc commit 48fa913

File tree

8 files changed

+25
-55
lines changed

8 files changed

+25
-55
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/env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl Iterator for Vars {
170170
impl fmt::Debug for Vars {
171171
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172172
let Self { inner: VarsOs { inner } } = self;
173-
f.debug_struct("Vars").field("inner", &inner.str_debug()).finish()
173+
f.debug_struct("Vars").field("inner", inner).finish()
174174
}
175175
}
176176

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+
}

library/std/src/sys/env/common.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,10 @@ pub struct Env {
55
iter: vec::IntoIter<(OsString, OsString)>,
66
}
77

8-
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
9-
pub struct EnvStrDebug<'a> {
10-
slice: &'a [(OsString, OsString)],
11-
}
12-
13-
impl fmt::Debug for EnvStrDebug<'_> {
14-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15-
f.debug_list()
16-
.entries(self.slice.iter().map(|(a, b)| (a.to_str().unwrap(), b.to_str().unwrap())))
17-
.finish()
18-
}
19-
}
20-
218
impl Env {
229
pub(super) fn new(env: Vec<(OsString, OsString)>) -> Self {
2310
Env { iter: env.into_iter() }
2411
}
25-
26-
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
27-
EnvStrDebug { slice: self.iter.as_slice() }
28-
}
2912
}
3013

3114
impl fmt::Debug for Env {

library/std/src/sys/env/unsupported.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@ use crate::{fmt, io};
33

44
pub struct Env(!);
55

6-
impl Env {
7-
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
8-
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
9-
self.0
10-
}
11-
}
12-
136
impl fmt::Debug for Env {
147
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
158
self.0

library/std/src/sys/env/windows.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,6 @@ pub struct Env {
88
iter: EnvIterator,
99
}
1010

11-
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
12-
pub struct EnvStrDebug<'a> {
13-
iter: &'a EnvIterator,
14-
}
15-
16-
impl fmt::Debug for EnvStrDebug<'_> {
17-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18-
let Self { iter } = self;
19-
let iter: EnvIterator = (*iter).clone();
20-
let mut list = f.debug_list();
21-
for (a, b) in iter {
22-
list.entry(&(a.to_str().unwrap(), b.to_str().unwrap()));
23-
}
24-
list.finish()
25-
}
26-
}
27-
28-
impl Env {
29-
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
30-
let Self { base: _, iter } = self;
31-
EnvStrDebug { iter }
32-
}
33-
}
34-
3511
impl fmt::Debug for Env {
3612
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3713
let Self { base: _, iter } = self;

0 commit comments

Comments
 (0)