summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-04-09 00:48:27 +0200
committerAraq <rumpf_a@web.de>2014-04-09 00:48:27 +0200
commit0c64849aab957601c71a34f7158eb0e8bd5d4e25 (patch)
tree40e4fe6c3a9319d5cf315c60faae547d67665161
parent4196757de768e5832d740e87164d0597e5f95363 (diff)
parent36869b3aed66b7edd8c116929eb418bb9d6f4554 (diff)
downloadNim-0c64849aab957601c71a34f7158eb0e8bd5d4e25.tar.gz
Merge branch 'devel' of https://github.com/Araq/Nimrod into devel
-rw-r--r--lib/system.nim41
-rw-r--r--lib/system/sysstr.nim6
-rw-r--r--tests/system/toString.nim11
3 files changed, 38 insertions, 20 deletions
diff --git a/lib/system.nim b/lib/system.nim
index 047b998c3..a54a25f2f 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -1697,12 +1697,23 @@ proc `$`*[T: tuple|object](x: T): string =
   ##   $(23, 45) == "(23, 45)"
   ##   $() == "()"
   result = "("
+  var firstElement = true
   for name, value in fieldPairs(x):
-    if result.len > 1: result.add(", ")
+    if not(firstElement): result.add(", ")
     result.add(name)
     result.add(": ")
     result.add($value)
+    firstElement = false
   result.add(")")
+  
+proc collectionToString[T](x: T, b, e: string): string =
+  result = b
+  var firstElement = true
+  for value in items(x):
+    if not(firstElement): result.add(", ")
+    result.add($value)
+    firstElement = false
+  result.add(e)
 
 proc `$`*[T: set](x: T): string = 
   ## generic ``$`` operator for sets that is lifted from the components
@@ -1710,24 +1721,18 @@ proc `$`*[T: set](x: T): string =
   ##
   ## .. code-block:: nimrod
   ##   ${23, 45} == "{23, 45}"
-  result = "{"
-  for value in items(x):
-    if result.len > 1: result.add(", ")
-    result.add($value)
-  result.add("}")
+  collectionToString(x, "{", "}")
 
-when false:
-  proc `$`*[T](a: openArray[T]): string = 
-    ## generic ``$`` operator for open arrays that is lifted from the elements
-    ## of `a`. Example:
-    ##
-    ## .. code-block:: nimrod
-    ##   $[23, 45] == "[23, 45]"
-    result = "["
-    for x in items(a):
-      if result.len > 1: result.add(", ")
-      result.add($x)
-    result.add("]")
+proc `$`*[T: seq](x: T): string = 
+  ## generic ``$`` operator for seqs that is lifted from the components
+  ## of `x`. Example:
+  ##
+  ## .. code-block:: nimrod
+  ##   $(@[23, 45]) == "@[23, 45]"
+  collectionToString(x, "@[", "]")
+
+proc `$`*[T: array](x: T): string = 
+  collectionToString(x, "[", "]")
 
 # ----------------- GC interface ---------------------------------------------
 
diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim
index 4244bae4c..eb9d2000b 100644
--- a/lib/system/sysstr.nim
+++ b/lib/system/sysstr.nim
@@ -252,8 +252,10 @@ proc nimIntToStr(x: int): string {.compilerRtl.} =
 
 proc nimFloatToStr(x: float): string {.compilerproc.} =
   var buf: array [0..59, char]
-  c_sprintf(buf, "%#.16e", x)
-  return $buf
+  c_sprintf(buf, "%#.f", x)
+  result = $buf
+  if result[len(result)-1] == '.':
+    result.add("0")
 
 proc nimInt64ToStr(x: int64): string {.compilerRtl.} =
   result = newString(sizeof(x)*4)
diff --git a/tests/system/toString.nim b/tests/system/toString.nim
new file mode 100644
index 000000000..17dcb3cb4
--- /dev/null
+++ b/tests/system/toString.nim
@@ -0,0 +1,11 @@
+discard """
+  output:'''@[23, 45]
+@[, foo, bar]
+[, foo, bar]
+[23, 45]'''
+"""
+
+echo($(@[23, 45]))
+echo($(@["", "foo", "bar"]))
+echo($(["", "foo", "bar"]))
+echo($([23, 45]))