Skip to content

Commit 94b3659

Browse files
committed
Clarify StringReader::bump.
This commit renames the variables to make it clearer which char each one refers to. It also slightly reorders and rearranges some statements.
1 parent e263120 commit 94b3659

File tree

1 file changed

+16
-13
lines changed
  • src/libsyntax/parse/lexer

1 file changed

+16
-13
lines changed

src/libsyntax/parse/lexer/mod.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -414,27 +414,30 @@ impl<'a> StringReader<'a> {
414414
/// Advance the StringReader by one character. If a newline is
415415
/// discovered, add it to the FileMap's list of line start offsets.
416416
pub fn bump(&mut self) {
417-
self.pos = self.next_pos;
418-
let current_byte_offset = self.byte_offset(self.next_pos).to_usize();
419-
if current_byte_offset < self.source_text.len() {
420-
let last_char = self.ch.unwrap();
421-
let ch = char_at(&self.source_text, current_byte_offset);
422-
let byte_offset_diff = ch.len_utf8();
423-
self.next_pos = self.next_pos + Pos::from_usize(byte_offset_diff);
424-
self.ch = Some(ch);
425-
self.col = self.col + CharPos(1);
426-
if last_char == '\n' {
417+
let new_pos = self.next_pos;
418+
let new_byte_offset = self.byte_offset(new_pos).to_usize();
419+
if new_byte_offset < self.source_text.len() {
420+
let old_ch_is_newline = self.ch.unwrap() == '\n';
421+
let new_ch = char_at(&self.source_text, new_byte_offset);
422+
let new_ch_len = new_ch.len_utf8();
423+
424+
self.ch = Some(new_ch);
425+
self.pos = new_pos;
426+
self.next_pos = new_pos + Pos::from_usize(new_ch_len);
427+
if old_ch_is_newline {
427428
if self.save_new_lines {
428429
self.filemap.next_line(self.pos);
429430
}
430431
self.col = CharPos(0);
432+
} else {
433+
self.col = self.col + CharPos(1);
431434
}
432-
433-
if byte_offset_diff > 1 {
434-
self.filemap.record_multibyte_char(self.pos, byte_offset_diff);
435+
if new_ch_len > 1 {
436+
self.filemap.record_multibyte_char(self.pos, new_ch_len);
435437
}
436438
} else {
437439
self.ch = None;
440+
self.pos = new_pos;
438441
}
439442
}
440443

0 commit comments

Comments
 (0)