Skip to content

Commit 6e350bd

Browse files
committed
librustc_lexer: Simplify "raw_double_quoted_string" method
1 parent d6f722d commit 6e350bd

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

src/librustc_lexer/src/lib.rs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -600,36 +600,45 @@ impl Cursor<'_> {
600600
/// (amount of the '#' symbols, raw string started, raw string terminated)
601601
fn raw_double_quoted_string(&mut self) -> (usize, bool, bool) {
602602
debug_assert!(self.prev() == 'r');
603+
let mut started: bool = false;
604+
let mut finished: bool = false;
605+
603606
// Count opening '#' symbols.
604-
let n_hashes = {
605-
let mut acc: usize = 0;
606-
loop {
607-
match self.bump() {
608-
Some('#') => acc += 1,
609-
Some('"') => break acc,
610-
None | Some(_) => return (acc, false, false),
611-
}
607+
let n_hashes = self.eat_while(|c| c == '#');
608+
609+
// Check that string is started.
610+
match self.bump() {
611+
Some('"') => started = true,
612+
_ => return (n_hashes, started, finished),
613+
}
614+
615+
// Skip the string contents and on each '#' character met, check if this is
616+
// a raw string termination.
617+
while !finished {
618+
self.eat_while(|c| c != '"');
619+
620+
if self.is_eof() {
621+
return (n_hashes, started, finished);
612622
}
613-
};
614623

615-
// Skip the string itself and check that amount of closing '#'
616-
// symbols is equal to the amount of opening ones.
617-
loop {
618-
match self.bump() {
619-
Some('"') => {
620-
let mut acc = n_hashes;
621-
while self.nth_char(0) == '#' && acc > 0 {
622-
self.bump();
623-
acc -= 1;
624-
}
625-
if acc == 0 {
626-
return (n_hashes, true, true);
627-
}
624+
// Eat closing double quote.
625+
self.bump();
626+
627+
// Check that amount of closing '#' symbols
628+
// is equal to the amount of opening ones.
629+
let mut hashes_left = n_hashes;
630+
let is_closing_hash = |c| {
631+
if c == '#' && hashes_left != 0 {
632+
hashes_left -= 1;
633+
true
634+
} else {
635+
false
628636
}
629-
Some(_) => (),
630-
None => return (n_hashes, true, false),
631-
}
637+
};
638+
finished = self.eat_while(is_closing_hash) == n_hashes;
632639
}
640+
641+
(n_hashes, started, finished)
633642
}
634643

635644
fn eat_decimal_digits(&mut self) -> bool {

0 commit comments

Comments
 (0)