remove $ for ptr/ref, prefer using string over array of char

This commit is contained in:
Arne Döring 2017-05-31 14:05:45 +02:00
commit 0852be2dec
3 changed files with 23 additions and 29 deletions

View file

@ -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.}