diff --git a/lib/system/repr_v2.nim b/lib/system/repr_v2.nim index fcc187a42c4cf..d456f4454f893 100644 --- a/lib/system/repr_v2.nim +++ b/lib/system/repr_v2.nim @@ -29,21 +29,35 @@ proc repr*(x: bool): string {.magic: "BoolToStr", noSideEffect.} proc repr*(x: char): string {.noSideEffect.} = ## repr for a character argument. Returns `x` - ## converted to a string. + ## converted to an escaped string. ## ## .. code-block:: Nim ## assert repr('c') == "'c'" - '\'' & $x & '\'' - -proc repr*(x: cstring): string {.noSideEffect.} = - ## repr for a CString argument. Returns `x` - ## converted to a quoted string. - '"' & $x & '"' + result.add '\'' + # Elides string creations if not needed + if x in {'\\', '\0'..'\31', '\127'..'\255'}: + result.add '\\' + if x in {'\0'..'\31', '\127'..'\255'}: + result.add $x.uint8 + else: + result.add x + result.add '\'' -proc repr*(x: string): string {.noSideEffect.} = +proc repr*(x: string | cstring): string {.noSideEffect.} = ## repr for a string argument. Returns `x` - ## but quoted. - '"' & x & '"' + ## converted to a quoted and escaped string. + result.add '\"' + for i in 0..