Skip to content

Commit 4c1256a

Browse files
committed
Reduce nesting somewhat with different if() order
1 parent 85f6f5b commit 4c1256a

1 file changed

Lines changed: 14 additions & 24 deletions

File tree

src/generic/stage2/numberparsing.h

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,8 @@ never_inline bool parse_large_integer(const uint8_t *const src,
290290
}
291291
}
292292
if (negative) {
293-
if (i > 0x8000000000000000) {
294-
return INVALID_NUMBER(src); // overflow: -i won't fit in INT32_MIN
295-
} else if (i == 0x8000000000000000) {
293+
if (i > 0x8000000000000000) { return INVALID_NUMBER(src); } // overflow: -i won't fit in INT32_MIN
294+
if (i == 0x8000000000000000) {
296295
// In two's complement, we cannot represent 0x8000000000000000
297296
// as a positive signed integer, but the negative version is
298297
// possible.
@@ -385,16 +384,13 @@ really_inline bool parse_number(UNUSED const uint8_t *const src,
385384
// the integer into a float in a lossless manner.
386385
++p;
387386
const char *const first_after_period = p;
388-
if (is_integer(*p)) {
389-
unsigned char digit = static_cast<unsigned char>(*p - '0');
390-
++p;
391-
i = i * 10 + digit; // might overflow + multiplication by 10 is likely
392-
// cheaper than arbitrary mult.
393-
// we will handle the overflow later
394-
} else {
395-
// There must be at least one digit after the .
396-
return INVALID_NUMBER(src);
397-
}
387+
if (!is_integer(*p)) { return INVALID_NUMBER(src); } // There must be at least one digit after the .
388+
389+
unsigned char digit = static_cast<unsigned char>(*p - '0');
390+
++p;
391+
i = i * 10 + digit; // might overflow + multiplication by 10 is likely
392+
// cheaper than arbitrary mult.
393+
// we will handle the overflow later
398394
#ifdef SWAR_NUMBER_PARSING
399395
// this helps if we have lots of decimals!
400396
// this turns out to be frequent enough.
@@ -404,15 +400,14 @@ really_inline bool parse_number(UNUSED const uint8_t *const src,
404400
}
405401
#endif
406402
while (is_integer(*p)) {
407-
unsigned char digit = static_cast<unsigned char>(*p - '0');
403+
digit = static_cast<unsigned char>(*p - '0');
408404
++p;
409405
i = i * 10 + digit; // in rare cases, this will overflow, but that's ok
410406
// because we have parse_highprecision_float later.
411407
}
412408
exponent = first_after_period - p;
413409
}
414-
int digit_count =
415-
int(p - start_digits) - 1; // used later to guard against overflows
410+
int digit_count = int(p - start_digits) - 1; // used later to guard against overflows
416411
int64_t exp_number = 0; // exponential part
417412
if (('e' == *p) || ('E' == *p)) {
418413
is_float = true;
@@ -492,15 +487,10 @@ really_inline bool parse_number(UNUSED const uint8_t *const src,
492487
double d = compute_float_64(exponent, i, negative, &success);
493488
if (!success) {
494489
// we are almost never going to get here.
495-
success = parse_float_strtod((const char *)src, &d);
496-
}
497-
if (success) {
498-
WRITE_DOUBLE(d, src, writer);
499-
return true;
500-
} else {
501-
// parse_float_strtod failed!
502-
return INVALID_NUMBER(src);
490+
if (!parse_float_strtod((const char *)src, &d)) { return INVALID_NUMBER(src); }
503491
}
492+
WRITE_DOUBLE(d, src, writer);
493+
return true;
504494
} else {
505495
if (unlikely(digit_count >= 18)) { // this is uncommon!!!
506496
// there is a good chance that we had an overflow, so we need

0 commit comments

Comments
 (0)