Rollup merge of #111584 - nnethercote:number-lexing-tweaks, r=matklad

Number lexing tweaks

A couple of improvements to things that puzzled me when I was looking at this code.

r? `@matklad`
This commit is contained in:
Matthias Krüger 2023-05-15 17:12:46 +02:00 committed by GitHub
commit 119b72289b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -582,34 +582,38 @@ impl Cursor<'_> {
let mut base = Base::Decimal;
if first_digit == '0' {
// Attempt to parse encoding base.
let has_digits = match self.first() {
match self.first() {
'b' => {
base = Base::Binary;
self.bump();
self.eat_decimal_digits()
if !self.eat_decimal_digits() {
return Int { base, empty_int: true };
}
}
'o' => {
base = Base::Octal;
self.bump();
self.eat_decimal_digits()
if !self.eat_decimal_digits() {
return Int { base, empty_int: true };
}
}
'x' => {
base = Base::Hexadecimal;
self.bump();
self.eat_hexadecimal_digits()
if !self.eat_hexadecimal_digits() {
return Int { base, empty_int: true };
}
}
// Not a base prefix.
'0'..='9' | '_' | '.' | 'e' | 'E' => {
// Not a base prefix; consume additional digits.
'0'..='9' | '_' => {
self.eat_decimal_digits();
true
}
// Also not a base prefix; nothing more to do here.
'.' | 'e' | 'E' => {}
// Just a 0.
_ => return Int { base, empty_int: false },
};
// Base prefix was provided, but there were no digits
// after it, e.g. "0x".
if !has_digits {
return Int { base, empty_int: true };
}
} else {
// No base prefix, parse number in the usual way.