Skip to content

Commit a297631

Browse files
committed
use <[u8]>::escape_ascii instead of core::ascii::escape_default
1 parent 6c943ba commit a297631

File tree

7 files changed

+16
-67
lines changed

7 files changed

+16
-67
lines changed

compiler/rustc_ast/src/util/literal.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,7 @@ impl LitKind {
164164
}
165165
LitKind::Str(symbol, ast::StrStyle::Raw(n)) => (token::StrRaw(n), symbol, None),
166166
LitKind::ByteStr(ref bytes) => {
167-
let string = bytes
168-
.iter()
169-
.cloned()
170-
.flat_map(ascii::escape_default)
171-
.map(Into::<char>::into)
172-
.collect::<String>();
167+
let string = bytes.escape_ascii().to_string();
173168
(token::ByteStr, Symbol::intern(&string), None)
174169
}
175170
LitKind::Byte(byte) => {

compiler/rustc_codegen_ssa/src/back/link.rs

+8-17
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use std::io::{BufWriter, Write};
4444
use std::ops::Deref;
4545
use std::path::{Path, PathBuf};
4646
use std::process::{ExitStatus, Output, Stdio};
47-
use std::{ascii, char, env, fmt, fs, io, mem, str};
47+
use std::{env, fmt, fs, io, mem, str};
4848

4949
pub fn ensure_removed(diag_handler: &Handler, path: &Path) {
5050
if let Err(e) = fs::remove_file(path) {
@@ -552,14 +552,6 @@ fn link_staticlib<'a>(
552552
Ok(())
553553
}
554554

555-
fn escape_stdout_stderr_string(s: &[u8]) -> String {
556-
str::from_utf8(s).map(|s| s.to_owned()).unwrap_or_else(|_| {
557-
let mut x = "Non-UTF-8 output: ".to_string();
558-
x.extend(s.iter().flat_map(|&b| ascii::escape_default(b)).map(char::from));
559-
x
560-
})
561-
}
562-
563555
/// Use `thorin` (rust implementation of a dwarf packaging utility) to link DWARF objects into a
564556
/// DWARF package.
565557
fn link_dwarf_object<'a>(
@@ -866,7 +858,7 @@ fn link_natively<'a>(
866858
if !prog.status.success() {
867859
let mut output = prog.stderr.clone();
868860
output.extend_from_slice(&prog.stdout);
869-
let escaped_output = escape_stdout_stderr_string(&output);
861+
let escaped_output = escape_string(&output);
870862
let mut err = sess.struct_err(&format!(
871863
"linking with `{}` failed: {}",
872864
linker_path.display(),
@@ -934,8 +926,8 @@ fn link_natively<'a>(
934926

935927
sess.abort_if_errors();
936928
}
937-
info!("linker stderr:\n{}", escape_stdout_stderr_string(&prog.stderr));
938-
info!("linker stdout:\n{}", escape_stdout_stderr_string(&prog.stdout));
929+
info!("linker stderr:\n{}", escape_string(&prog.stderr));
930+
info!("linker stdout:\n{}", escape_string(&prog.stdout));
939931
}
940932
Err(e) => {
941933
let linker_not_found = e.kind() == io::ErrorKind::NotFound;
@@ -1065,11 +1057,10 @@ fn strip_symbols_in_osx<'a>(sess: &'a Session, out_filename: &Path, option: Opti
10651057
}
10661058

10671059
fn escape_string(s: &[u8]) -> String {
1068-
str::from_utf8(s).map(|s| s.to_owned()).unwrap_or_else(|_| {
1069-
let mut x = "Non-UTF-8 output: ".to_string();
1070-
x.extend(s.iter().flat_map(|&b| ascii::escape_default(b)).map(char::from));
1071-
x
1072-
})
1060+
match str::from_utf8(s) {
1061+
Ok(s) => s.to_owned(),
1062+
Err(_) => format!("Non-UTF-8 output: {}", s.escape_ascii()),
1063+
}
10731064
}
10741065

10751066
fn add_sanitizer_libraries(sess: &Session, crate_type: CrateType, linker: &mut dyn Linker) {

compiler/rustc_middle/src/mir/mod.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -2643,15 +2643,7 @@ fn pretty_print_const<'tcx>(
26432643
}
26442644

26452645
fn pretty_print_byte_str(fmt: &mut Formatter<'_>, byte_str: &[u8]) -> fmt::Result {
2646-
fmt.write_str("b\"")?;
2647-
for &c in byte_str {
2648-
for e in std::ascii::escape_default(c) {
2649-
fmt.write_char(e as char)?;
2650-
}
2651-
}
2652-
fmt.write_str("\"")?;
2653-
2654-
Ok(())
2646+
write!(fmt, "b\"{}\"", byte_str.escape_ascii())
26552647
}
26562648

26572649
fn comma_sep<'tcx>(fmt: &mut Formatter<'_>, elems: Vec<ConstantKind<'tcx>>) -> fmt::Result {

compiler/rustc_middle/src/ty/print/pretty.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -1401,14 +1401,7 @@ pub trait PrettyPrinter<'tcx>:
14011401
}
14021402

14031403
fn pretty_print_byte_str(mut self, byte_str: &'tcx [u8]) -> Result<Self::Const, Self::Error> {
1404-
define_scoped_cx!(self);
1405-
p!("b\"");
1406-
for &c in byte_str {
1407-
for e in std::ascii::escape_default(c) {
1408-
self.write_char(e as char)?;
1409-
}
1410-
}
1411-
p!("\"");
1404+
write!(self, "b\"{}\"", byte_str.escape_ascii())?;
14121405
Ok(self)
14131406
}
14141407

library/core/src/ffi/c_str.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use crate::ascii;
21
use crate::cmp::Ordering;
32
use crate::ffi::c_char;
4-
use crate::fmt::{self, Write};
3+
use crate::fmt;
54
use crate::intrinsics;
65
use crate::ops;
76
use crate::slice;
@@ -161,11 +160,7 @@ impl fmt::Display for FromBytesUntilNulError {
161160
#[stable(feature = "cstr_debug", since = "1.3.0")]
162161
impl fmt::Debug for CStr {
163162
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164-
write!(f, "\"")?;
165-
for byte in self.to_bytes().iter().flat_map(|&b| ascii::escape_default(b)) {
166-
f.write_char(byte as char)?;
167-
}
168-
write!(f, "\"")
163+
write!(f, "\"{}\"", self.to_bytes().escape_ascii())
169164
}
170165
}
171166

library/proc_macro/src/lib.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1353,12 +1353,7 @@ impl Literal {
13531353
/// Byte string literal.
13541354
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
13551355
pub fn byte_string(bytes: &[u8]) -> Literal {
1356-
let string = bytes
1357-
.iter()
1358-
.cloned()
1359-
.flat_map(std::ascii::escape_default)
1360-
.map(Into::<char>::into)
1361-
.collect::<String>();
1356+
let string = bytes.escape_ascii().to_string();
13621357
Literal::new(bridge::LitKind::ByteStr, &string, None)
13631358
}
13641359

library/std/src/os/unix/net/addr.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ffi::OsStr;
22
use crate::os::unix::ffi::OsStrExt;
33
use crate::path::Path;
44
use crate::sys::cvt;
5-
use crate::{ascii, fmt, io, mem, ptr};
5+
use crate::{fmt, io, mem, ptr};
66

77
// FIXME(#43348): Make libc adapt #[doc(cfg(...))] so we don't need these fake definitions here?
88
#[cfg(not(unix))]
@@ -64,18 +64,6 @@ enum AddressKind<'a> {
6464
Abstract(&'a [u8]),
6565
}
6666

67-
struct AsciiEscaped<'a>(&'a [u8]);
68-
69-
impl<'a> fmt::Display for AsciiEscaped<'a> {
70-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
71-
write!(fmt, "\"")?;
72-
for byte in self.0.iter().cloned().flat_map(ascii::escape_default) {
73-
write!(fmt, "{}", byte as char)?;
74-
}
75-
write!(fmt, "\"")
76-
}
77-
}
78-
7967
/// An address associated with a Unix socket.
8068
///
8169
/// # Examples
@@ -343,7 +331,7 @@ impl fmt::Debug for SocketAddr {
343331
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
344332
match self.address() {
345333
AddressKind::Unnamed => write!(fmt, "(unnamed)"),
346-
AddressKind::Abstract(name) => write!(fmt, "{} (abstract)", AsciiEscaped(name)),
334+
AddressKind::Abstract(name) => write!(fmt, "\"{}\" (abstract)", name.escape_ascii()),
347335
AddressKind::Pathname(path) => write!(fmt, "{path:?} (pathname)"),
348336
}
349337
}

0 commit comments

Comments
 (0)