Merge branch 'devel' of https://github.com/Araq/Nimrod into devel

This commit is contained in:
Araq 2014-04-09 00:48:27 +02:00
commit 0c64849aab
3 changed files with 38 additions and 20 deletions

View file

@ -1697,12 +1697,23 @@ proc `$`*[T: tuple|object](x: T): string =
## $(23, 45) == "(23, 45)" ## $(23, 45) == "(23, 45)"
## $() == "()" ## $() == "()"
result = "(" result = "("
var firstElement = true
for name, value in fieldPairs(x): for name, value in fieldPairs(x):
if result.len > 1: result.add(", ") if not(firstElement): result.add(", ")
result.add(name) result.add(name)
result.add(": ") result.add(": ")
result.add($value) result.add($value)
firstElement = false
result.add(")") 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 = proc `$`*[T: set](x: T): string =
## generic ``$`` operator for sets that is lifted from the components ## generic ``$`` operator for sets that is lifted from the components
@ -1710,24 +1721,18 @@ proc `$`*[T: set](x: T): string =
## ##
## .. code-block:: nimrod ## .. code-block:: nimrod
## ${23, 45} == "{23, 45}" ## ${23, 45} == "{23, 45}"
result = "{" collectionToString(x, "{", "}")
for value in items(x):
if result.len > 1: result.add(", ")
result.add($value)
result.add("}")
when false: proc `$`*[T: seq](x: T): string =
proc `$`*[T](a: openArray[T]): string = ## generic ``$`` operator for seqs that is lifted from the components
## generic ``$`` operator for open arrays that is lifted from the elements ## of `x`. Example:
## of `a`. Example: ##
## ## .. code-block:: nimrod
## .. code-block:: nimrod ## $(@[23, 45]) == "@[23, 45]"
## $[23, 45] == "[23, 45]" collectionToString(x, "@[", "]")
result = "["
for x in items(a): proc `$`*[T: array](x: T): string =
if result.len > 1: result.add(", ") collectionToString(x, "[", "]")
result.add($x)
result.add("]")
# ----------------- GC interface --------------------------------------------- # ----------------- GC interface ---------------------------------------------

View file

@ -252,8 +252,10 @@ proc nimIntToStr(x: int): string {.compilerRtl.} =
proc nimFloatToStr(x: float): string {.compilerproc.} = proc nimFloatToStr(x: float): string {.compilerproc.} =
var buf: array [0..59, char] var buf: array [0..59, char]
c_sprintf(buf, "%#.16e", x) c_sprintf(buf, "%#.f", x)
return $buf result = $buf
if result[len(result)-1] == '.':
result.add("0")
proc nimInt64ToStr(x: int64): string {.compilerRtl.} = proc nimInt64ToStr(x: int64): string {.compilerRtl.} =
result = newString(sizeof(x)*4) result = newString(sizeof(x)*4)

11
tests/system/toString.nim Normal file
View file

@ -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]))