Skip to content

Commit 649a524

Browse files
committed
librustc_lexer: Simplify "single_quoted_string" method
1 parent e0c45f7 commit 649a524

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/librustc_lexer/src/lib.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -537,26 +537,30 @@ impl Cursor<'_> {
537537

538538
fn single_quoted_string(&mut self) -> bool {
539539
debug_assert!(self.prev() == '\'');
540-
// Parse `'''` as a single char literal.
541-
if self.nth_char(0) == '\'' && self.nth_char(1) == '\'' {
540+
// Check if it's a one-symbol literal.
541+
if self.second() == '\'' && self.first() != '\\' {
542542
self.bump();
543+
self.bump();
544+
return true;
543545
}
546+
547+
// Literal has more than one symbol.
548+
544549
// Parse until either quotes are terminated or error is detected.
545-
let mut first = true;
546550
loop {
547551
match self.first() {
548-
// Probably beginning of the comment, which we don't want to include
549-
// to the error report.
550-
'/' if !first => break,
551-
// Newline without following '\'' means unclosed quote, stop parsing.
552-
'\n' if self.nth_char(1) != '\'' => break,
553-
// End of file, stop parsing.
554-
EOF_CHAR if self.is_eof() => break,
555552
// Quotes are terminated, finish parsing.
556553
'\'' => {
557554
self.bump();
558555
return true;
559556
}
557+
// Probably beginning of the comment, which we don't want to include
558+
// to the error report.
559+
'/' => break,
560+
// Newline without following '\'' means unclosed quote, stop parsing.
561+
'\n' if self.second() != '\'' => break,
562+
// End of file, stop parsing.
563+
EOF_CHAR if self.is_eof() => break,
560564
// Escaped slash is considered one character, so bump twice.
561565
'\\' => {
562566
self.bump();
@@ -567,8 +571,8 @@ impl Cursor<'_> {
567571
self.bump();
568572
}
569573
}
570-
first = false;
571574
}
575+
// String was not terminated.
572576
false
573577
}
574578

0 commit comments

Comments
 (0)