Skip to content

Commit d848f33

Browse files
committed
Simplify integer parsing
1 parent c643675 commit d848f33

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

src/generic/stage2/numberparsing.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -416,19 +416,14 @@ really_inline bool parse_number(UNUSED const uint8_t *const src,
416416
//
417417
// Parse the integer part.
418418
//
419+
// PERF NOTE: we don't use is_made_of_eight_digits_fast because large integers like 123456789 are rare
419420
const char *const start_digits = p;
420-
uint64_t i;
421-
if (!parse_first_digit(*p, i)) { return INVALID_NUMBER(src); }
422-
++p;
421+
uint64_t i = 0;
422+
while (parse_digit(*p, i)) { p++; }
423423

424-
// If the integer starts with 0, just check that there are no more digits.
425-
if (i == 0) {
426-
if (static_cast<unsigned char>(*p - '0') <= 9) { return INVALID_NUMBER(src); } // 0 cannot be followed by an integer
427-
} else {
428-
// Integer starts with 1-9. Parse the rest of the integer
429-
// PERF NOTE: we don't use is_made_of_eight_digits_fast because large integers like 123456789 are rare
430-
while (parse_digit(*p, i)) { p++; }
431-
}
424+
// If there were no digits, or if the integer starts with 0 and has more than one digit, it's an error.
425+
int digit_count = int(p - start_digits);
426+
if (digit_count == 0 || ('0' == *start_digits && digit_count > 1)) { return INVALID_NUMBER(src); }
432427

433428
//
434429
// Handle floats if there is a . or e (or both)
@@ -439,8 +434,8 @@ really_inline bool parse_number(UNUSED const uint8_t *const src,
439434
is_float = true;
440435
++p;
441436
if (!parse_decimal(src, p, i, exponent)) { return false; }
437+
digit_count = int(p - start_digits); // used later to guard against overflows
442438
}
443-
int digit_count = int(p - start_digits); // used later to guard against overflows
444439
if (('e' == *p) || ('E' == *p)) {
445440
is_float = true;
446441
++p;

0 commit comments

Comments
 (0)