@@ -1547,36 +1547,46 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) {
1547
1547
bool isNegative = *current == ' -' ;
1548
1548
if (isNegative)
1549
1549
++current;
1550
- // TODO: Help the compiler do the div and mod at compile time or get rid of
1551
- // them.
1552
- Value::LargestUInt maxIntegerValue =
1553
- isNegative ? Value::LargestUInt (Value::minLargestInt)
1554
- : Value::maxLargestUInt;
1555
- Value::LargestUInt threshold = maxIntegerValue / 10 ;
1550
+
1551
+ // TODO(issue #960): Change to constexpr
1552
+ static const auto positive_threshold = Value::maxLargestUInt / 10 ;
1553
+ static const auto positive_last_digit = Value::maxLargestUInt % 10 ;
1554
+ static const auto negative_threshold =
1555
+ Value::LargestUInt (Value::minLargestInt) / 10 ;
1556
+ static const auto negative_last_digit =
1557
+ Value::LargestUInt (Value::minLargestInt) % 10 ;
1558
+
1559
+ const auto threshold = isNegative ? negative_threshold : positive_threshold;
1560
+ const auto last_digit =
1561
+ isNegative ? negative_last_digit : positive_last_digit;
1562
+
1556
1563
Value::LargestUInt value = 0 ;
1557
1564
while (current < token.end_ ) {
1558
1565
Char c = *current++;
1559
1566
if (c < ' 0' || c > ' 9' )
1560
1567
return decodeDouble (token, decoded);
1561
- auto digit (static_cast <Value::UInt>(c - ' 0' ));
1568
+
1569
+ const auto digit (static_cast <Value::UInt>(c - ' 0' ));
1562
1570
if (value >= threshold) {
1563
1571
// We've hit or exceeded the max value divided by 10 (rounded down). If
1564
- // a) we've only just touched the limit, b) this is the last digit, and
1572
+ // a) we've only just touched the limit, meaing value == threshold,
1573
+ // b) this is the last digit, or
1565
1574
// c) it's small enough to fit in that rounding delta, we're okay.
1566
1575
// Otherwise treat this number as a double to avoid overflow.
1567
- if (value > threshold || current != token.end_ ||
1568
- digit > maxIntegerValue % 10 ) {
1576
+ if (value > threshold || current != token.end_ || digit > last_digit) {
1569
1577
return decodeDouble (token, decoded);
1570
1578
}
1571
1579
}
1572
1580
value = value * 10 + digit;
1573
1581
}
1582
+
1574
1583
if (isNegative)
1575
1584
decoded = -Value::LargestInt (value);
1576
- else if (value <= Value::LargestUInt (Value::maxInt ))
1585
+ else if (value <= Value::LargestUInt (Value::maxLargestInt ))
1577
1586
decoded = Value::LargestInt (value);
1578
1587
else
1579
1588
decoded = value;
1589
+
1580
1590
return true ;
1581
1591
}
1582
1592
0 commit comments