summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/rodutils.nim8
-rw-r--r--lib/system.nim30
-rw-r--r--lib/system/repr.nim14
3 files changed, 23 insertions, 29 deletions
diff --git a/compiler/rodutils.nim b/compiler/rodutils.nim
index 03d5c95eb..1352afadb 100644
--- a/compiler/rodutils.nim
+++ b/compiler/rodutils.nim
@@ -10,7 +10,7 @@
 ## Serialization utilities for the compiler.
 import strutils
 
-proc c_sprintf(buf, frmt: cstring) {.importc: "sprintf", header: "<stdio.h>", nodecl, varargs.}
+proc c_snprintf(s: cstring; n:uint; frmt: cstring): cint {.importc: "snprintf", header: "<stdio.h>", nodecl, varargs.}
 
 proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string =
   if f != f:
@@ -21,9 +21,9 @@ proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string =
     if f > 0.0: result = "INF"
     else: result = "-INF"
   else:
-    var buf: array[0..80, char]
-    c_sprintf(buf, "%#.16e" & literalPostfix, f)
-    result = newString(buf)
+    result = newString(80)
+    let newLen = c_snprintf(result[0].addr, 81, "%#.16e" & literalPostfix, f)
+    result.setLen(newLen)
 
 proc encodeStr*(s: string, result: var string) =
   for i in countup(0, len(s) - 1):
diff --git a/lib/system.nim b/lib/system.nim
index 3dc892c44..b5008129d 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -1872,26 +1872,20 @@ proc `$` *[Enum: enum](x: Enum): string {.magic: "EnumToStr", noSideEffect.}
   ## a ``$`` operator for a concrete enumeration is provided, this is
   ## used instead. (In other words: *Overwriting* is possible.)
 
-proc `$`*[T: ref](arg: T): string =
-  ## The stringify operator to handle the nil value of all ref types.
-  ## Then it forwards to stringify operator of the underlying (value) type.
-  if arg.isNil:
-    "nil"
-  else:
-    "ref " & $arg[]
-
-proc `$`*[T: ptr](arg: T): string =
-  ## The stringify operator to handle the nil value of all ptr types.
-  ## Then it forwards to stringify operator of the underlying (value) type.
-  if arg.isNil:
-    "nil"
-  else:
-    "ptr " & $arg[]
-
-proc newString*[N](data: array[N, char]): string =
+proc newString*[N](data: array[N, char]): string {.noSideEffect.} =
   ## Construct a string from an array of characters. The `data` is
   ## expected to be a null terminated string as it is often used in C.
-  $(cast[cstring](data[0].unsafeAddr))
+  when nimvm:
+    # cannot cast on the vm
+    # not recommended to use this procedure on the vm at all, but at least it doesn't fail.
+    result = ""
+    for c in data:
+      if c == '\0':
+        return
+      else:
+        result.add c
+  else:
+    result = $(cast[cstring](data[0].unsafeAddr))
 
 # undocumented:
 proc getRefcount*[T](x: ref T): int {.importc: "getRefcount", noSideEffect.}
diff --git a/lib/system/repr.nim b/lib/system/repr.nim
index fcf65dd0e..2775b1b3e 100644
--- a/lib/system/repr.nim
+++ b/lib/system/repr.nim
@@ -16,27 +16,27 @@ proc reprInt(x: int64): string {.compilerproc.} = return $x
 proc reprFloat(x: float): string {.compilerproc.} = return $x
 
 proc reprPointer(x: pointer): string {.compilerproc.} =
-  var buf: array[0..59, char]
-  discard c_sprintf(buf, "%p", x)
-  return newString(buf)
+  result = newString(60)
+  let newLen = c_sprintf(result[0].addr, "%p", x)
+  result.setLen newLen
 
 proc `$`(x: uint64): string =
   if x == 0:
     result = "0"
   else:
-    var buf: array[60, char]
+    result = newString(60)
     var i = 0
     var n = x
     while n != 0:
       let nn = n div 10'u64
-      buf[i] = char(n - 10'u64 * nn + ord('0'))
+      result[i] = char(n - 10'u64 * nn + ord('0'))
       inc i
       n = nn
+    result.setLen i
 
     let half = i div 2
     # Reverse
-    for t in 0 .. < half: swap(buf[t], buf[i-t-1])
-    result = newString(buf)
+    for t in 0 .. < half: swap(result[t], result[i-t-1])
 
 proc reprStrAux(result: var string, s: cstring; len: int) =
   if cast[pointer](s) == nil: