Skip to content

Commit 9e718c3

Browse files
committed
Remove superfluous escaping from byte, byte str, and c str literals
1 parent 4fbcbc4 commit 9e718c3

File tree

3 files changed

+103
-21
lines changed

3 files changed

+103
-21
lines changed

library/proc_macro/src/escape.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use std::fmt::Write as _;
2+
3+
#[derive(Copy, Clone)]
4+
pub(crate) struct EscapeOptions {
5+
/// Produce \'.
6+
pub escape_single_quote: bool,
7+
/// Produce \".
8+
pub escape_double_quote: bool,
9+
/// Produce \x escapes for non-ASCII, and use \x rather than \u for ASCII
10+
/// control characters.
11+
pub escape_nonascii: bool,
12+
}
13+
14+
pub(crate) fn escape_bytes(bytes: &[u8], opt: EscapeOptions) -> String {
15+
let mut repr = String::new();
16+
17+
if opt.escape_nonascii {
18+
for &byte in bytes {
19+
escape_single_byte(byte, opt, &mut repr);
20+
}
21+
} else {
22+
let mut chunks = bytes.utf8_chunks();
23+
while let Some(chunk) = chunks.next() {
24+
for ch in chunk.valid().chars() {
25+
escape_single_char(ch, opt, &mut repr);
26+
}
27+
for &byte in chunk.invalid() {
28+
escape_single_byte(byte, opt, &mut repr);
29+
}
30+
}
31+
}
32+
33+
repr
34+
}
35+
36+
fn escape_single_byte(byte: u8, opt: EscapeOptions, repr: &mut String) {
37+
if byte == b'\0' {
38+
repr.push_str("\\0");
39+
} else if (byte == b'\'' && !opt.escape_single_quote)
40+
|| (byte == b'"' && !opt.escape_double_quote)
41+
{
42+
repr.push(byte as char);
43+
} else {
44+
// Escapes \t, \r, \n, \\, \', \", and uses \x## for non-ASCII and
45+
// for ASCII control characters.
46+
write!(repr, "{}", byte.escape_ascii()).unwrap();
47+
}
48+
}
49+
50+
fn escape_single_char(ch: char, opt: EscapeOptions, repr: &mut String) {
51+
if (ch == '\'' && !opt.escape_single_quote) || (ch == '"' && !opt.escape_double_quote) {
52+
repr.push(ch);
53+
} else {
54+
// Escapes \0, \t, \r, \n, \\, \', \", and uses \u{...} for
55+
// non-printable characters and for Grapheme_Extend characters, which
56+
// includes things like U+0300 "Combining Grave Accent".
57+
write!(repr, "{}", ch.escape_debug()).unwrap();
58+
}
59+
}

library/proc_macro/src/lib.rs

+37-14
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@
4343
pub mod bridge;
4444

4545
mod diagnostic;
46+
mod escape;
4647

4748
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
4849
pub use diagnostic::{Diagnostic, Level, MultiSpan};
4950

51+
use crate::escape::{escape_bytes, EscapeOptions};
5052
use std::ffi::CStr;
5153
use std::ops::{Range, RangeBounds};
5254
use std::path::PathBuf;
@@ -1344,40 +1346,61 @@ impl Literal {
13441346
/// String literal.
13451347
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
13461348
pub fn string(string: &str) -> Literal {
1347-
let quoted = format!("{:?}", string);
1348-
assert!(quoted.starts_with('"') && quoted.ends_with('"'));
1349-
let symbol = &quoted[1..quoted.len() - 1];
1350-
Literal::new(bridge::LitKind::Str, symbol, None)
1349+
let escape = EscapeOptions {
1350+
escape_single_quote: false,
1351+
escape_double_quote: true,
1352+
escape_nonascii: false,
1353+
};
1354+
let repr = escape_bytes(string.as_bytes(), escape);
1355+
Literal::new(bridge::LitKind::Str, &repr, None)
13511356
}
13521357

13531358
/// Character literal.
13541359
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
13551360
pub fn character(ch: char) -> Literal {
1356-
let quoted = format!("{:?}", ch);
1357-
assert!(quoted.starts_with('\'') && quoted.ends_with('\''));
1358-
let symbol = &quoted[1..quoted.len() - 1];
1359-
Literal::new(bridge::LitKind::Char, symbol, None)
1361+
let escape = EscapeOptions {
1362+
escape_single_quote: true,
1363+
escape_double_quote: false,
1364+
escape_nonascii: false,
1365+
};
1366+
let repr = escape_bytes(ch.encode_utf8(&mut [0u8; 4]).as_bytes(), escape);
1367+
Literal::new(bridge::LitKind::Char, &repr, None)
13601368
}
13611369

13621370
/// Byte character literal.
13631371
#[stable(feature = "proc_macro_byte_character", since = "CURRENT_RUSTC_VERSION")]
13641372
pub fn byte_character(byte: u8) -> Literal {
1365-
let string = [byte].escape_ascii().to_string();
1366-
Literal::new(bridge::LitKind::Byte, &string, None)
1373+
let escape = EscapeOptions {
1374+
escape_single_quote: true,
1375+
escape_double_quote: false,
1376+
escape_nonascii: true,
1377+
};
1378+
let repr = escape_bytes(&[byte], escape);
1379+
Literal::new(bridge::LitKind::Byte, &repr, None)
13671380
}
13681381

13691382
/// Byte string literal.
13701383
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
13711384
pub fn byte_string(bytes: &[u8]) -> Literal {
1372-
let string = bytes.escape_ascii().to_string();
1373-
Literal::new(bridge::LitKind::ByteStr, &string, None)
1385+
let escape = EscapeOptions {
1386+
escape_single_quote: false,
1387+
escape_double_quote: true,
1388+
escape_nonascii: true,
1389+
};
1390+
let repr = escape_bytes(bytes, escape);
1391+
Literal::new(bridge::LitKind::ByteStr, &repr, None)
13741392
}
13751393

13761394
/// C string literal.
13771395
#[stable(feature = "proc_macro_c_str_literals", since = "CURRENT_RUSTC_VERSION")]
13781396
pub fn c_string(string: &CStr) -> Literal {
1379-
let string = string.to_bytes().escape_ascii().to_string();
1380-
Literal::new(bridge::LitKind::CStr, &string, None)
1397+
let escape = EscapeOptions {
1398+
escape_single_quote: false,
1399+
escape_double_quote: true,
1400+
escape_nonascii: false,
1401+
};
1402+
let repr = escape_bytes(string.to_bytes(), escape);
1403+
Literal::new(bridge::LitKind::CStr, &repr, None)
13811404
}
13821405

13831406
/// Returns the span encompassing this literal.

tests/ui/proc-macro/auxiliary/api/literal.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ fn test_display_literal() {
2929

3030
assert_eq!(Literal::byte_string(b"aA").to_string(), r#" b"aA" "#.trim());
3131
assert_eq!(Literal::byte_string(b"\t").to_string(), r#" b"\t" "#.trim());
32-
assert_eq!(Literal::byte_string(b"'").to_string(), r#" b"\'" "#.trim());
32+
assert_eq!(Literal::byte_string(b"'").to_string(), r#" b"'" "#.trim());
3333
assert_eq!(Literal::byte_string(b"\"").to_string(), r#" b"\"" "#.trim());
34-
assert_eq!(Literal::byte_string(b"\0").to_string(), r#" b"\x00" "#.trim());
34+
assert_eq!(Literal::byte_string(b"\0").to_string(), r#" b"\0" "#.trim());
3535
assert_eq!(Literal::byte_string(b"\x01").to_string(), r#" b"\x01" "#.trim());
3636

3737
assert_eq!(Literal::c_string(c"aA").to_string(), r#" c"aA" "#.trim());
3838
assert_eq!(Literal::c_string(c"\t").to_string(), r#" c"\t" "#.trim());
39-
assert_eq!(Literal::c_string(c"❤").to_string(), r#" c"\xe2\x9d\xa4" "#.trim());
40-
assert_eq!(Literal::c_string(c"\'").to_string(), r#" c"\'" "#.trim());
39+
assert_eq!(Literal::c_string(c"❤").to_string(), r#" c"" "#.trim());
40+
assert_eq!(Literal::c_string(c"\'").to_string(), r#" c"'" "#.trim());
4141
assert_eq!(Literal::c_string(c"\"").to_string(), r#" c"\"" "#.trim());
42-
assert_eq!(Literal::c_string(c"\x7f\xff\xfe\u{333}").to_string(), r#" c"\x7f\xff\xfe\xcc\xb3" "#.trim());
42+
assert_eq!(Literal::c_string(c"\x7f\xff\xfe\u{333}").to_string(), r#" c"\u{7f}\xff\xfe\u{333}" "#.trim());
4343

4444
assert_eq!(Literal::character('a').to_string(), r#" 'a' "#.trim());
4545
assert_eq!(Literal::character('\t').to_string(), r#" '\t' "#.trim());
@@ -52,8 +52,8 @@ fn test_display_literal() {
5252
assert_eq!(Literal::byte_character(b'a').to_string(), r#" b'a' "#.trim());
5353
assert_eq!(Literal::byte_character(b'\t').to_string(), r#" b'\t' "#.trim());
5454
assert_eq!(Literal::byte_character(b'\'').to_string(), r#" b'\'' "#.trim());
55-
assert_eq!(Literal::byte_character(b'"').to_string(), r#" b'\"' "#.trim());
56-
assert_eq!(Literal::byte_character(0).to_string(), r#" b'\x00' "#.trim());
55+
assert_eq!(Literal::byte_character(b'"').to_string(), r#" b'"' "#.trim());
56+
assert_eq!(Literal::byte_character(0).to_string(), r#" b'\0' "#.trim());
5757
assert_eq!(Literal::byte_character(1).to_string(), r#" b'\x01' "#.trim());
5858
}
5959

0 commit comments

Comments
 (0)