Added BiggestUInt (#5378)

This commit is contained in:
flyx 2017-02-13 11:20:07 +01:00 • committed by Andreas Rumpf
commit ac9c2126e6
3 changed files with 24 additions and 13 deletions

View file

@ -250,18 +250,18 @@ proc parseInt*(s: string, number: var int, start = 0): int {.
elif result != 0: elif result != 0:
number = int(res) number = int(res)
# overflowChecks doesn't work with uint64 # overflowChecks doesn't work with BiggestUInt
proc rawParseUInt(s: string, b: var uint64, start = 0): int = proc rawParseUInt(s: string, b: var BiggestUInt, start = 0): int =
var var
res = 0'u64 res = 0.BiggestUInt
prev = 0'u64 prev = 0.BiggestUInt
i = start i = start
if s[i] == '+': inc(i) # Allow if s[i] == '+': inc(i) # Allow
if s[i] in {'0'..'9'}: if s[i] in {'0'..'9'}:
b = 0 b = 0
while s[i] in {'0'..'9'}: while s[i] in {'0'..'9'}:
prev = res prev = res
res = res * 10 + (ord(s[i]) - ord('0')).uint64 res = res * 10 + (ord(s[i]) - ord('0')).BiggestUInt
if prev > res: if prev > res:
return 0 # overflowChecks emulation return 0 # overflowChecks emulation
inc(i) inc(i)
@ -269,13 +269,13 @@ proc rawParseUInt(s: string, b: var uint64, start = 0): int =
b = res b = res
result = i - start result = i - start
proc parseBiggestUInt*(s: string, number: var uint64, start = 0): int {. proc parseBiggestUInt*(s: string, number: var BiggestUInt, start = 0): int {.
rtl, extern: "npuParseBiggestUInt", noSideEffect.} = rtl, extern: "npuParseBiggestUInt", noSideEffect.} =
## parses an unsigned integer starting at `start` and stores the value ## parses an unsigned integer starting at `start` and stores the value
## into `number`. ## into `number`.
## Result is the number of processed chars or 0 if there is no integer ## Result is the number of processed chars or 0 if there is no integer
## or overflow detected. ## or overflow detected.
var res: uint64 var res: BiggestUInt
# use 'res' for exception safety (don't write to 'number' in case of an # use 'res' for exception safety (don't write to 'number' in case of an
# overflow exception): # overflow exception):
result = rawParseUInt(s, res, start) result = rawParseUInt(s, res, start)
@ -287,12 +287,12 @@ proc parseUInt*(s: string, number: var uint, start = 0): int {.
## into `number`. ## into `number`.
## Result is the number of processed chars or 0 if there is no integer or ## Result is the number of processed chars or 0 if there is no integer or
## overflow detected. ## overflow detected.
var res: uint64 var res: BiggestUInt
result = parseBiggestUInt(s, res, start) result = parseBiggestUInt(s, res, start)
if (sizeof(uint) <= 4) and when sizeof(BiggestUInt) > sizeof(uint) and sizeof(uint) <= 4:
(res > 0xFFFF_FFFF'u64): if res > 0xFFFF_FFFF'u64:
raise newException(OverflowError, "overflow") raise newException(OverflowError, "overflow")
elif result != 0: if result != 0:
number = uint(res) number = uint(res)
proc parseBiggestFloat*(s: string, number: var BiggestFloat, start = 0): int {. proc parseBiggestFloat*(s: string, number: var BiggestFloat, start = 0): int {.

View file

@ -939,7 +939,7 @@ proc parseUInt*(s: string): uint {.noSideEffect, procvar,
if L != s.len or L == 0: if L != s.len or L == 0:
raise newException(ValueError, "invalid unsigned integer: " & s) raise newException(ValueError, "invalid unsigned integer: " & s)
proc parseBiggestUInt*(s: string): uint64 {.noSideEffect, procvar, proc parseBiggestUInt*(s: string): BiggestUInt {.noSideEffect, procvar,
rtl, extern: "nsuParseBiggestUInt".} = rtl, extern: "nsuParseBiggestUInt".} =
## Parses a decimal unsigned integer value contained in `s`. ## Parses a decimal unsigned integer value contained in `s`.
## ##

View file

@ -1512,6 +1512,17 @@ type
## compiler supports. Currently this is ``float64``, but it is ## compiler supports. Currently this is ``float64``, but it is
## platform-dependant in general. ## platform-dependant in general.
when defined(JS):
type BiggestUInt* = uint32
## is an alias for the biggest unsigned integer type the Nim compiler
## supports. Currently this is ``uint32`` for JS and ``uint64`` for other
## targets.
else:
type BiggestUInt* = uint64
## is an alias for the biggest unsigned integer type the Nim compiler
## supports. Currently this is ``uint32`` for JS and ``uint64`` for other
## targets.
{.deprecated: [TAddress: ByteAddress].} {.deprecated: [TAddress: ByteAddress].}
when defined(windows): when defined(windows):