refactor: change to use peekable

This commit is contained in:
rainy-me 2022-04-14 21:18:27 +09:00
parent 4a0f8d5175
commit 1b7008dc77
2 changed files with 18 additions and 21 deletions

View File

@ -557,39 +557,36 @@ impl<'a> StringReader<'a> {
); );
let mut nested_block_comment_open_idxs = vec![]; let mut nested_block_comment_open_idxs = vec![];
let mut last_nested_block_comment_idxs = None; let mut last_nested_block_comment_idxs = None;
let mut content_chars = self.str_from(start).char_indices(); let mut content_chars = self.str_from(start).char_indices().peekable();
if let Some((_, mut last_char)) = content_chars.next() { while let Some((idx, current_char)) = content_chars.next() {
while let Some((idx, c)) = content_chars.next() { match content_chars.peek() {
match c { Some((_, '*')) if current_char == '/' => {
'*' if last_char == '/' => {
nested_block_comment_open_idxs.push(idx); nested_block_comment_open_idxs.push(idx);
} }
'/' if last_char == '*' => { Some((_, '/')) if current_char == '*' => {
last_nested_block_comment_idxs = last_nested_block_comment_idxs =
nested_block_comment_open_idxs.pop().map(|open_idx| (open_idx, idx)); nested_block_comment_open_idxs.pop().map(|open_idx| (open_idx, idx));
} }
_ => {} _ => {}
}; };
last_char = c;
}
} }
if let Some((nested_open_idx, nested_close_idx)) = last_nested_block_comment_idxs { if let Some((nested_open_idx, nested_close_idx)) = last_nested_block_comment_idxs {
err.span_label(self.mk_sp(start, start + BytePos(2)), msg) err.span_label(self.mk_sp(start, start + BytePos(2)), msg)
.span_label( .span_label(
self.mk_sp( self.mk_sp(
start + BytePos(nested_open_idx as u32 - 1), start + BytePos(nested_open_idx as u32),
start + BytePos(nested_open_idx as u32 + 1), start + BytePos(nested_open_idx as u32 + 2),
), ),
"...as last nested comment starts here, maybe you want to close this instead?", "...as last nested comment starts here, maybe you want to close this instead?",
) )
.span_label( .span_label(
self.mk_sp( self.mk_sp(
start + BytePos(nested_close_idx as u32 - 1), start + BytePos(nested_close_idx as u32),
start + BytePos(nested_close_idx as u32 + 1), start + BytePos(nested_close_idx as u32 + 2),
), ),
"...and last nested comment terminates here", "...and last nested comment terminates here.",
); );
} }

View File

@ -14,7 +14,7 @@ LL | | /*
LL | | */ LL | | */
| |_--^ | |_--^
| | | |
| ...and last nested comment terminates here | ...and last nested comment terminates here.
error: aborting due to previous error error: aborting due to previous error