mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 14:00:35 +00:00
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:
@@ -27,21 +27,23 @@ proc repr*(x: bool): string {.magic: "BoolToStr", noSideEffect.}
|
||||
## repr for a boolean argument. Returns `x`
|
||||
## 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`
|
||||
## converted to a string.
|
||||
##
|
||||
## .. code-block:: Nim
|
||||
## 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`
|
||||
## 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`
|
||||
## as it is. This operator is useful for generic code, so
|
||||
## that ``$expr`` also works if ``expr`` is already a string.
|
||||
## but quoted.
|
||||
'"' & x & '"'
|
||||
|
||||
proc repr*[Enum: enum](x: Enum): string {.magic: "EnumToStr", noSideEffect.}
|
||||
## repr for an enumeration argument. This works for
|
||||
|
||||
@@ -7,8 +7,8 @@ nil
|
||||
output: '''
|
||||
nil
|
||||
2
|
||||
Obj(member: ref @[hello])
|
||||
ref (member: ref @[hello])
|
||||
Obj(member: ref @["hello"])
|
||||
ref (member: ref @["hello"])
|
||||
'''
|
||||
"""
|
||||
import tables
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
discard """
|
||||
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
|
||||
Dick Laurent is dead'''
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user