diff options
author | flywind <xzsflywind@gmail.com> | 2021-06-22 23:02:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-22 17:02:32 +0200 |
commit | 9a81e91fa5d01cd6d60a408d0888dce4a9f49898 (patch) | |
tree | 0c699301c696ec41583e344eefb3b2b755753bc0 /lib/system/dollars.nim | |
parent | 037715285c088af46c94939f36abef63c95406e8 (diff) | |
download | Nim-9a81e91fa5d01cd6d60a408d0888dce4a9f49898.tar.gz |
merge similar procs regarding digits (#18318)
Diffstat (limited to 'lib/system/dollars.nim')
-rw-r--r-- | lib/system/dollars.nim | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/lib/system/dollars.nim b/lib/system/dollars.nim index 5a7a77e56..75390b0e1 100644 --- a/lib/system/dollars.nim +++ b/lib/system/dollars.nim @@ -1,3 +1,6 @@ +import std/private/digitsutils + + proc `$`*(x: int): string {.magic: "IntToStr", noSideEffect.} ## The stringify operator for an integer argument. Returns `x` ## converted to a decimal string. `$` is Nim's general way of @@ -6,22 +9,14 @@ proc `$`*(x: int): string {.magic: "IntToStr", noSideEffect.} template dollarImpl(x: uint | uint64, result: var string) = type destTyp = typeof(x) if x == 0: - result = "0" - else: - result = newString(60) - var i = 0 - var n = x - while n != 0: - let nn = n div destTyp(10) - result[i] = char(n - destTyp(10) * nn + ord('0')) - inc i - n = nn - result.setLen i - - let half = i div 2 - # Reverse - for t in 0 .. half-1: swap(result[t], result[i-t-1]) + return "0" + elif x == 1: + return "1" + + let length = digits10(x) + setLen(result, length) + numToString(result, x, length) when defined(js): import std/private/since |