Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2e53992

Browse files
committedMar 7, 2025
Separate the unescape functions for string, byte string and C string, but avoid duplicating code via macro_rules.
Also plays with NonZero, since C strings cannot contain null bytes, which can be captured in the type system.
1 parent 07b5eee commit 2e53992

File tree

9 files changed

+360
-379
lines changed

9 files changed

+360
-379
lines changed
 

‎compiler/rustc_ast/src/util/literal.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::{ascii, fmt, str};
44

55
use rustc_lexer::unescape::{
6-
MixedUnit, Mode, byte_from_char, unescape_byte, unescape_char, unescape_mixed, unescape_unicode,
6+
MixedUnit, unescape_byte, unescape_byte_str, unescape_char, unescape_cstr, unescape_str,
77
};
88
use rustc_span::{Span, Symbol, kw, sym};
99
use tracing::debug;
@@ -87,11 +87,10 @@ impl LitKind {
8787
// Force-inlining here is aggressive but the closure is
8888
// called on every char in the string, so it can be hot in
8989
// programs with many long strings containing escapes.
90-
unescape_unicode(
90+
unescape_str(
9191
s,
92-
Mode::Str,
9392
&mut #[inline(always)]
94-
|_, c| match c {
93+
|_, res| match res {
9594
Ok(c) => buf.push(c),
9695
Err(err) => {
9796
assert!(!err.is_fatal(), "failed to unescape string literal")
@@ -111,8 +110,8 @@ impl LitKind {
111110
token::ByteStr => {
112111
let s = symbol.as_str();
113112
let mut buf = Vec::with_capacity(s.len());
114-
unescape_unicode(s, Mode::ByteStr, &mut |_, c| match c {
115-
Ok(c) => buf.push(byte_from_char(c)),
113+
unescape_byte_str(s, &mut |_, res| match res {
114+
Ok(b) => buf.push(b),
116115
Err(err) => {
117116
assert!(!err.is_fatal(), "failed to unescape string literal")
118117
}
@@ -128,11 +127,11 @@ impl LitKind {
128127
token::CStr => {
129128
let s = symbol.as_str();
130129
let mut buf = Vec::with_capacity(s.len());
131-
unescape_mixed(s, Mode::CStr, &mut |_span, c| match c {
130+
unescape_cstr(s, &mut |_span, c| match c {
132131
Ok(MixedUnit::Char(c)) => {
133-
buf.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes())
132+
buf.extend_from_slice(c.get().encode_utf8(&mut [0; 4]).as_bytes())
134133
}
135-
Ok(MixedUnit::HighByte(b)) => buf.push(b),
134+
Ok(MixedUnit::HighByte(b)) => buf.push(b.get()),
136135
Err(err) => {
137136
assert!(!err.is_fatal(), "failed to unescape C string literal")
138137
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.