arrays can now be printed

This commit is contained in:
Arne Döring 2017-05-29 17:53:20 +02:00
commit 1f7fc7f279
5 changed files with 81 additions and 25 deletions

View file

@ -1852,7 +1852,7 @@ proc `$` *(x: float): string {.magic: "FloatToStr", noSideEffect.}
proc `$` *(x: bool): string {.magic: "BoolToStr", noSideEffect.}
## The stringify operator for a boolean argument. Returns `x`
## converted to the string "false" or "true".
#
proc `$` *(x: char): string {.magic: "CharToStr", noSideEffect.}
## The stringify operator for a character argument. Returns `x`
## converted to a string.
@ -1872,6 +1872,27 @@ 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 =
## 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))
# undocumented:
proc getRefcount*[T](x: ref T): int {.importc: "getRefcount", noSideEffect.}
proc getRefcount*(x: string): int {.importc: "getRefcount", noSideEffect.}
@ -2410,20 +2431,25 @@ proc `$`*[T: tuple|object](x: T): string =
result.add("...")
result.add(")")
proc collectionToString[T: set | seq](x: T, b, e: string): string =
when x is seq:
if x.isNil: return "nil"
result = b
proc collectionToString[T](x: T, prefix, separator, suffix: string): string =
result = prefix
var firstElement = true
for value in items(x):
if not firstElement: result.add(", ")
if firstElement:
firstElement = false
else:
result.add(separator)
when compiles(value.isNil):
if value.isNil: result.add "nil"
else: result.add($value)
# this branch should not be necessary
if value.isNil:
result.add "nil"
else:
result.add($value)
else:
result.add($value)
firstElement = false
result.add(e)
result.add(suffix)
proc `$`*[T](x: set[T]): string =
## generic ``$`` operator for sets that is lifted from the components
@ -2431,7 +2457,7 @@ proc `$`*[T](x: set[T]): string =
##
## .. code-block:: nim
## ${23, 45} == "{23, 45}"
collectionToString(x, "{", "}")
collectionToString(x, "{", ", ", "}")
proc `$`*[T](x: seq[T]): string =
## generic ``$`` operator for seqs that is lifted from the components
@ -2439,13 +2465,10 @@ proc `$`*[T](x: seq[T]): string =
##
## .. code-block:: nim
## $(@[23, 45]) == "@[23, 45]"
collectionToString(x, "@[", "]")
when false:
# causes bootstrapping to fail as we use array of chars and cstring should
# match better ...
proc `$`*[T, IDX](x: array[IDX, T]): string =
collectionToString(x, "[", "]")
if x.isNil:
"nil"
else:
collectionToString(x, "@[", ", ", "]")
# ----------------- GC interface ---------------------------------------------
@ -3301,6 +3324,11 @@ elif defined(JS):
include "system/sysio"
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)