Small improvements for string and char repr with gc:arc (#14400)

* Small improvements for string and char repr with gc:arc

* Fix test
This commit is contained in:
Clyybber 2020-05-20 12:54:04 +02:00 • committed by GitHub
commit 7fe4c66f35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 9 deletions

View file

@ -27,21 +27,23 @@ proc repr*(x: bool): string {.magic: "BoolToStr", noSideEffect.}
## repr for a boolean argument. Returns `x` ## repr for a boolean argument. Returns `x`
## converted to the string "false" or "true". ## converted to the string "false" or "true".
proc repr*(x: char): string {.magic: "CharToStr", noSideEffect.} proc repr*(x: char): string {.noSideEffect.} =
## repr for a character argument. Returns `x` ## repr for a character argument. Returns `x`
## converted to a string. ## converted to a string.
## ##
## .. code-block:: Nim ## .. code-block:: Nim
## assert repr('c') == "c" ## assert repr('c') == "c"
'\'' & $x & '\''
proc repr*(x: cstring): string {.magic: "CStrToStr", noSideEffect.} proc repr*(x: cstring): string {.noSideEffect.} =
## repr for a CString argument. Returns `x` ## repr for a CString argument. Returns `x`
## converted to a string. ## converted to a quoted string.
'"' & $x & '"'
proc repr*(x: string): string {.magic: "StrToStr", noSideEffect.} proc repr*(x: string): string {.noSideEffect.} =
## repr for a string argument. Returns `x` ## repr for a string argument. Returns `x`
## as it is. This operator is useful for generic code, so ## but quoted.
## that ``$expr`` also works if ``expr`` is already a string. '"' & x & '"'
proc repr*[Enum: enum](x: Enum): string {.magic: "EnumToStr", noSideEffect.} proc repr*[Enum: enum](x: Enum): string {.magic: "EnumToStr", noSideEffect.}
## repr for an enumeration argument. This works for ## repr for an enumeration argument. This works for

View file

@ -7,8 +7,8 @@ nil
output: ''' output: '''
nil nil
2 2
Obj(member: ref @[hello]) Obj(member: ref @["hello"])
ref (member: ref @[hello]) ref (member: ref @["hello"])
''' '''
""" """
import tables import tables

View file

@ -1,6 +1,6 @@
discard """ discard """
cmd: "nim c --gc:arc $file" cmd: "nim c --gc:arc $file"
output: '''Foo(field: Dick Laurent, k: ka, x: 0.0) output: '''Foo(field: "Dick Laurent", k: ka, x: 0.0)
Nobody is dead Nobody is dead
Dick Laurent is dead''' Dick Laurent is dead'''
""" """