summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--lib/system.nim9
-rw-r--r--tests/system/toString.nim4
2 files changed, 9 insertions, 4 deletions
diff --git a/lib/system.nim b/lib/system.nim
index 014538098..0bc0a0dbf 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -2425,6 +2425,14 @@ proc collectionToString[T](x: T, prefix, separator, suffix: string): string =
         result.add "nil"
       else:
         result.add($value)
+    # prevent temporary string allocation
+    elif compiles(result.add(value)):
+      # don't insert '\0' characters into the result string
+      when value is char:
+        if value != '\0':
+          result.add(value)
+      else:
+        result.add(value)
     else:
       result.add($value)
 
@@ -3307,7 +3315,6 @@ proc `$`*[T, IDX](x: array[IDX, T]): string =
   ## generic ``$`` operator for arrays that is lifted from the components
   collectionToString(x, "[", ", ", "]")
 
-
 proc quit*(errormsg: string, errorcode = QuitFailure) {.noReturn.} =
   ## a shorthand for ``echo(errormsg); quit(errorcode)``.
   echo(errormsg)
diff --git a/tests/system/toString.nim b/tests/system/toString.nim
index d002bb379..0eed596ae 100644
--- a/tests/system/toString.nim
+++ b/tests/system/toString.nim
@@ -47,7 +47,5 @@ doAssert dataStr == $data
 import strutils
 # array test
 let arr = ['H','e','l','l','o',' ','W','o','r','l','d','!','\0']
-
-# not sure if this is really a good idea
-doAssert startsWith($arr, "[H, e, l, l, o,  , W, o, r, l, d, !,")
+doAssert $arr == "[H, e, l, l, o,  , W, o, r, l, d, !, ]"
 doAssert $arr.cstring == "Hello World!"