mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 14:00:35 +00:00
Fix parseutils.parseBiggestInt regression (#10348)
This commit is contained in:
committed by
Andreas Rumpf
parent
3cc39c2eb9
commit
86a91c1a4a
@@ -350,6 +350,10 @@ proc captureBetween*(s: string, first: char, second = '\0', start = 0): string =
|
||||
proc integerOutOfRangeError() {.noinline.} =
|
||||
raise newException(ValueError, "Parsed integer outside of valid range")
|
||||
|
||||
# See #6752
|
||||
when defined(js):
|
||||
{.push overflowChecks: off.}
|
||||
|
||||
proc rawParseInt(s: string, b: var BiggestInt, start = 0): int =
|
||||
var
|
||||
sign: BiggestInt = -1
|
||||
@@ -363,18 +367,21 @@ proc rawParseInt(s: string, b: var BiggestInt, start = 0): int =
|
||||
b = 0
|
||||
while i < s.len and s[i] in {'0'..'9'}:
|
||||
let c = ord(s[i]) - ord('0')
|
||||
if b >= (low(int) + c) div 10:
|
||||
if b >= (low(BiggestInt) + c) div 10:
|
||||
b = b * 10 - c
|
||||
else:
|
||||
integerOutOfRangeError()
|
||||
inc(i)
|
||||
while i < s.len and s[i] == '_': inc(i) # underscores are allowed and ignored
|
||||
if sign == -1 and b == low(int):
|
||||
if sign == -1 and b == low(BiggestInt):
|
||||
integerOutOfRangeError()
|
||||
else:
|
||||
b = b * sign
|
||||
result = i - start
|
||||
|
||||
when defined(js):
|
||||
{.pop.} # overflowChecks: off
|
||||
|
||||
proc parseBiggestInt*(s: string, number: var BiggestInt, start = 0): int {.
|
||||
rtl, extern: "npuParseBiggestInt", noSideEffect, raises: [ValueError].} =
|
||||
## Parses an integer starting at `start` and stores the value into `number`.
|
||||
@@ -632,4 +639,8 @@ when isMainModule:
|
||||
doAssert(parseSaturatedNatural("1_000_000", value) == 9)
|
||||
doAssert value == 1_000_000
|
||||
|
||||
var i64Value: int64
|
||||
discard parseBiggestInt("9223372036854775807", i64Value)
|
||||
doAssert i64Value == 9223372036854775807
|
||||
|
||||
{.pop.}
|
||||
|
||||
Reference in New Issue
Block a user