summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2021-06-22 21:42:39 -0700
committerGitHub <noreply@github.com>2021-06-22 21:42:39 -0700
commit5badeea17055c85ea8177baeee62c8d1d06ac13e (patch)
tree668b0c02067ca641f185c189d0d5cdffc0c677e5
parent9a81e91fa5d01cd6d60a408d0888dce4a9f49898 (diff)
downloadNim-5badeea17055c85ea8177baeee62c8d1d06ac13e.tar.gz
followup #18318: simplify `dollarImpl` and add a test (#18330)
-rw-r--r--lib/system/dollars.nim7
-rw-r--r--tests/stdlib/tdigitsutils.nim23
2 files changed, 23 insertions, 7 deletions
diff --git a/lib/system/dollars.nim b/lib/system/dollars.nim
index 75390b0e1..3d824a965 100644
--- a/lib/system/dollars.nim
+++ b/lib/system/dollars.nim
@@ -7,15 +7,8 @@ proc `$`*(x: int): string {.magic: "IntToStr", noSideEffect.}
   ## spelling `toString`:idx:.
 
 template dollarImpl(x: uint | uint64, result: var string) =
-  type destTyp = typeof(x)
-  if x == 0:
-    return "0"
-  elif x == 1:
-    return "1"
-
   let length = digits10(x)
   setLen(result, length)
-
   numToString(result, x, length)
 
 when defined(js):
diff --git a/tests/stdlib/tdigitsutils.nim b/tests/stdlib/tdigitsutils.nim
new file mode 100644
index 000000000..7cf63411f
--- /dev/null
+++ b/tests/stdlib/tdigitsutils.nim
@@ -0,0 +1,23 @@
+import std/private/digitsutils
+
+template main =
+  block: # digits10
+    doAssert digits10(0'u64) == 1
+    # checks correctness on all powers of 10 + [0,-1,1]
+    var x = 1'u64
+    var num = 1
+    while true:
+      # echo (x, num)
+      doAssert digits10(x) == num
+      doAssert digits10(x+1) == num
+      if x > 1:
+        doAssert digits10(x-1) == num - 1
+      num += 1
+      let xOld = x
+      x *= 10
+      if x < xOld:
+        # wrap-around
+        break
+
+static: main()
+main()