Skip to content

Commit e00522b

Browse files
committed
Restructure rustc_lexer::unescape
Separate the functions for unescaping each kind of string and unit: - this duplicates some code, but also gets rid of code that is only there for genericity - each function is now simpler by inlining booleans, which might lead to faster code Use a Peekable<CharIndices<'_>> instead of going back and forth between string slice and chars iterator. - this gets rid of most position computations - allows removal of double traversal for correct backslash newline escapes in skip_ascii_whitespace Improves documentation
1 parent 8239a37 commit e00522b

File tree

7 files changed

+443
-371
lines changed

7 files changed

+443
-371
lines changed

compiler/rustc_ast/src/util/literal.rs

+6-7
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,7 +127,7 @@ 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)) => {
133132
buf.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes())
134133
}

0 commit comments

Comments
 (0)