Skip to content

Commit f32e678

Browse files
committed
Rename some variables.
These have been bugging me for a while. - `literal_text`: `src` is also used and is shorter and better. - `first_char`: used even when "first" doesn't make sense; `c` is shorter and better. - `curr`: `c` is shorter and better. - `unescaped_char`: `result` is also used and is shorter and better. - `second_char`: these have a single use and can be elided.
1 parent d726c84 commit f32e678

File tree

1 file changed

+32
-38
lines changed

1 file changed

+32
-38
lines changed

compiler/rustc_lexer/src/unescape.rs

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -78,54 +78,52 @@ impl EscapeError {
7878
/// Takes a contents of a literal (without quotes) and produces a
7979
/// sequence of escaped characters or errors.
8080
/// Values are returned through invoking of the provided callback.
81-
pub fn unescape_literal<F>(literal_text: &str, mode: Mode, callback: &mut F)
81+
pub fn unescape_literal<F>(src: &str, mode: Mode, callback: &mut F)
8282
where
8383
F: FnMut(Range<usize>, Result<char, EscapeError>),
8484
{
8585
match mode {
8686
Mode::Char | Mode::Byte => {
87-
let mut chars = literal_text.chars();
87+
let mut chars = src.chars();
8888
let result = unescape_char_or_byte(&mut chars, mode);
8989
// The Chars iterator moved forward.
90-
callback(0..(literal_text.len() - chars.as_str().len()), result);
90+
callback(0..(src.len() - chars.as_str().len()), result);
9191
}
92-
Mode::Str | Mode::ByteStr => unescape_str_or_byte_str(literal_text, mode, callback),
92+
Mode::Str | Mode::ByteStr => unescape_str_or_byte_str(src, mode, callback),
9393
// NOTE: Raw strings do not perform any explicit character escaping, here we
9494
// only translate CRLF to LF and produce errors on bare CR.
95-
Mode::RawStr | Mode::RawByteStr => {
96-
unescape_raw_str_or_raw_byte_str(literal_text, mode, callback)
97-
}
95+
Mode::RawStr | Mode::RawByteStr => unescape_raw_str_or_raw_byte_str(src, mode, callback),
9896
}
9997
}
10098

10199
/// Takes a contents of a byte, byte string or raw byte string (without quotes)
102100
/// and produces a sequence of bytes or errors.
103101
/// Values are returned through invoking of the provided callback.
104-
pub fn unescape_byte_literal<F>(literal_text: &str, mode: Mode, callback: &mut F)
102+
pub fn unescape_byte_literal<F>(src: &str, mode: Mode, callback: &mut F)
105103
where
106104
F: FnMut(Range<usize>, Result<u8, EscapeError>),
107105
{
108106
debug_assert!(mode.is_bytes());
109-
unescape_literal(literal_text, mode, &mut |range, result| {
107+
unescape_literal(src, mode, &mut |range, result| {
110108
callback(range, result.map(byte_from_char));
111109
})
112110
}
113111

114112
/// Takes a contents of a char literal (without quotes), and returns an
115113
/// unescaped char or an error
116-
pub fn unescape_char(literal_text: &str) -> Result<char, (usize, EscapeError)> {
117-
let mut chars = literal_text.chars();
114+
pub fn unescape_char(src: &str) -> Result<char, (usize, EscapeError)> {
115+
let mut chars = src.chars();
118116
unescape_char_or_byte(&mut chars, Mode::Char)
119-
.map_err(|err| (literal_text.len() - chars.as_str().len(), err))
117+
.map_err(|err| (src.len() - chars.as_str().len(), err))
120118
}
121119

122120
/// Takes a contents of a byte literal (without quotes), and returns an
123121
/// unescaped byte or an error.
124-
pub fn unescape_byte(literal_text: &str) -> Result<u8, (usize, EscapeError)> {
125-
let mut chars = literal_text.chars();
122+
pub fn unescape_byte(src: &str) -> Result<u8, (usize, EscapeError)> {
123+
let mut chars = src.chars();
126124
unescape_char_or_byte(&mut chars, Mode::Byte)
127125
.map(byte_from_char)
128-
.map_err(|err| (literal_text.len() - chars.as_str().len(), err))
126+
.map_err(|err| (src.len() - chars.as_str().len(), err))
129127
}
130128

131129
/// What kind of literal do we parse.
@@ -157,10 +155,7 @@ impl Mode {
157155

158156
fn scan_escape(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
159157
// Previous character was '\\', unescape what follows.
160-
161-
let second_char = chars.next().ok_or(EscapeError::LoneSlash)?;
162-
163-
let res = match second_char {
158+
let res = match chars.next().ok_or(EscapeError::LoneSlash)? {
164159
'"' => '"',
165160
'n' => '\n',
166161
'r' => '\r',
@@ -249,23 +244,23 @@ fn scan_escape(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
249244
}
250245

251246
#[inline]
252-
fn ascii_check(first_char: char, mode: Mode) -> Result<char, EscapeError> {
253-
if mode.is_bytes() && !first_char.is_ascii() {
247+
fn ascii_check(c: char, mode: Mode) -> Result<char, EscapeError> {
248+
if mode.is_bytes() && !c.is_ascii() {
254249
// Byte literal can't be a non-ascii character.
255250
Err(EscapeError::NonAsciiCharInByte)
256251
} else {
257-
Ok(first_char)
252+
Ok(c)
258253
}
259254
}
260255

261256
fn unescape_char_or_byte(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
262257
debug_assert!(mode == Mode::Char || mode == Mode::Byte);
263-
let first_char = chars.next().ok_or(EscapeError::ZeroChars)?;
264-
let res = match first_char {
258+
let c = chars.next().ok_or(EscapeError::ZeroChars)?;
259+
let res = match c {
265260
'\\' => scan_escape(chars, mode),
266261
'\n' | '\t' | '\'' => Err(EscapeError::EscapeOnlyChar),
267262
'\r' => Err(EscapeError::BareCarriageReturn),
268-
_ => ascii_check(first_char, mode),
263+
_ => ascii_check(c, mode),
269264
}?;
270265
if chars.next().is_some() {
271266
return Err(EscapeError::MoreThanOneChar);
@@ -282,13 +277,12 @@ where
282277
debug_assert!(mode == Mode::Str || mode == Mode::ByteStr);
283278
let initial_len = src.len();
284279
let mut chars = src.chars();
285-
while let Some(first_char) = chars.next() {
286-
let start = initial_len - chars.as_str().len() - first_char.len_utf8();
280+
while let Some(c) = chars.next() {
281+
let start = initial_len - chars.as_str().len() - c.len_utf8();
287282

288-
let unescaped_char = match first_char {
283+
let result = match c {
289284
'\\' => {
290-
let second_char = chars.clone().next();
291-
match second_char {
285+
match chars.clone().next() {
292286
Some('\n') => {
293287
// Rust language specification requires us to skip whitespaces
294288
// if unescaped '\' character is followed by '\n'.
@@ -304,10 +298,10 @@ where
304298
'\t' => Ok('\t'),
305299
'"' => Err(EscapeError::EscapeOnlyChar),
306300
'\r' => Err(EscapeError::BareCarriageReturn),
307-
_ => ascii_check(first_char, mode),
301+
_ => ascii_check(c, mode),
308302
};
309303
let end = initial_len - chars.as_str().len();
310-
callback(start..end, unescaped_char);
304+
callback(start..end, result);
311305
}
312306

313307
fn skip_ascii_whitespace<F>(chars: &mut Chars<'_>, start: usize, callback: &mut F)
@@ -341,18 +335,18 @@ where
341335
/// sequence of characters or errors.
342336
/// NOTE: Raw strings do not perform any explicit character escaping, here we
343337
/// only translate CRLF to LF and produce errors on bare CR.
344-
fn unescape_raw_str_or_raw_byte_str<F>(literal_text: &str, mode: Mode, callback: &mut F)
338+
fn unescape_raw_str_or_raw_byte_str<F>(src: &str, mode: Mode, callback: &mut F)
345339
where
346340
F: FnMut(Range<usize>, Result<char, EscapeError>),
347341
{
348342
debug_assert!(mode == Mode::RawStr || mode == Mode::RawByteStr);
349-
let initial_len = literal_text.len();
343+
let initial_len = src.len();
350344

351-
let mut chars = literal_text.chars();
352-
while let Some(curr) = chars.next() {
353-
let start = initial_len - chars.as_str().len() - curr.len_utf8();
345+
let mut chars = src.chars();
346+
while let Some(c) = chars.next() {
347+
let start = initial_len - chars.as_str().len() - c.len_utf8();
354348

355-
let result = match curr {
349+
let result = match c {
356350
'\r' => Err(EscapeError::BareCarriageReturnInRawString),
357351
c if mode.is_bytes() && !c.is_ascii() => Err(EscapeError::NonAsciiCharInByteString),
358352
c => Ok(c),

0 commit comments

Comments
 (0)