From 1f7fc7f279a4a1c5f4854f7c7a39a58bb2cca805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Mon, 29 May 2017 17:53:20 +0200 Subject: [PATCH 1/9] arrays can now be printed --- compiler/rodutils.nim | 3 +- lib/pure/os.nim | 2 +- lib/system.nim | 64 ++++++++++++++++++++++++++++----------- lib/system/repr.nim | 4 +-- tests/system/toString.nim | 33 ++++++++++++++++++-- 5 files changed, 81 insertions(+), 25 deletions(-) diff --git a/compiler/rodutils.nim b/compiler/rodutils.nim index 77f7c844f3..03d5c95eb3 100644 --- a/compiler/rodutils.nim +++ b/compiler/rodutils.nim @@ -23,7 +23,7 @@ proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string = else: var buf: array[0..80, char] c_sprintf(buf, "%#.16e" & literalPostfix, f) - result = $buf + result = newString(buf) proc encodeStr*(s: string, result: var string) = for i in countup(0, len(s) - 1): @@ -133,4 +133,3 @@ iterator decodeStrArray*(s: cstring): string = while s[i] != '\0': yield decodeStr(s, i) if s[i] == ' ': inc i - diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 2195f6327e..5211bc00c8 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -1010,7 +1010,7 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path: while true: var x = readdir(d) if x == nil: break - var y = $x.d_name + var y = newString(x.d_name) if y != "." and y != "..": var s: Stat if not relative: diff --git a/lib/system.nim b/lib/system.nim index d62c0424eb..3dc892c442 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -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) diff --git a/lib/system/repr.nim b/lib/system/repr.nim index ab02c58a2a..fcf65dd0e3 100644 --- a/lib/system/repr.nim +++ b/lib/system/repr.nim @@ -18,7 +18,7 @@ proc reprFloat(x: float): string {.compilerproc.} = return $x proc reprPointer(x: pointer): string {.compilerproc.} = var buf: array[0..59, char] discard c_sprintf(buf, "%p", x) - return $buf + return newString(buf) proc `$`(x: uint64): string = if x == 0: @@ -36,7 +36,7 @@ proc `$`(x: uint64): string = let half = i div 2 # Reverse for t in 0 .. < half: swap(buf[t], buf[i-t-1]) - result = $buf + result = newString(buf) proc reprStrAux(result: var string, s: cstring; len: int) = if cast[pointer](s) == nil: diff --git a/tests/system/toString.nim b/tests/system/toString.nim index a2337f5dd4..f9697ec759 100644 --- a/tests/system/toString.nim +++ b/tests/system/toString.nim @@ -12,14 +12,16 @@ inf -inf nan nil -nil''' +nil +(a: 0, b: nil) +nil +ptr (a: 0, b: nil)''' """ echo($(@[23, 45])) echo($(@["", "foo", "bar"])) #echo($(["", "foo", "bar"])) #echo($([23, 45])) - # bug #2395 let alphaSet: set[char] = {'a'..'c'} @@ -40,3 +42,30 @@ var x: seq[string] var y: string echo(x) echo(y) + +type + Foo = object + a: int + b: string + +var foo1: Foo +var foo2: ref Foo +var foo3: ptr Foo = foo1.addr + +echo foo1 +echo foo2 +echo foo3 + +const + data = @['a','b', '\0', 'c','d'] + dataStr = $data + +# ensure same result when on VM or when at program execution +doAssert dataStr == $data + +import strutils +# array test +let arr = ['H','e','l','l','o',' ','W','o','r','l','d','!','\0'] + +doAssert startsWith($arr, "[H, e, l, l, o, , W, o, r, l, d, !,") +doAssert newString(arr) == "Hello World!" From 0852be2dec72406c8d9c93e094a8907b52d1aa5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Wed, 31 May 2017 14:05:45 +0200 Subject: [PATCH 2/9] remove $ for ptr/ref, prefer using string over array of char --- compiler/rodutils.nim | 8 ++++---- lib/system.nim | 30 ++++++++++++------------------ lib/system/repr.nim | 14 +++++++------- 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/compiler/rodutils.nim b/compiler/rodutils.nim index 03d5c95eb3..1352afadb4 100644 --- a/compiler/rodutils.nim +++ b/compiler/rodutils.nim @@ -10,7 +10,7 @@ ## Serialization utilities for the compiler. import strutils -proc c_sprintf(buf, frmt: cstring) {.importc: "sprintf", header: "", nodecl, varargs.} +proc c_snprintf(s: cstring; n:uint; frmt: cstring): cint {.importc: "snprintf", header: "", nodecl, varargs.} proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string = if f != f: @@ -21,9 +21,9 @@ proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string = if f > 0.0: result = "INF" else: result = "-INF" else: - var buf: array[0..80, char] - c_sprintf(buf, "%#.16e" & literalPostfix, f) - result = newString(buf) + result = newString(80) + let newLen = c_snprintf(result[0].addr, 81, "%#.16e" & literalPostfix, f) + result.setLen(newLen) proc encodeStr*(s: string, result: var string) = for i in countup(0, len(s) - 1): diff --git a/lib/system.nim b/lib/system.nim index 3dc892c442..b5008129d6 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -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.} diff --git a/lib/system/repr.nim b/lib/system/repr.nim index fcf65dd0e3..2775b1b3ed 100644 --- a/lib/system/repr.nim +++ b/lib/system/repr.nim @@ -16,27 +16,27 @@ proc reprInt(x: int64): string {.compilerproc.} = return $x proc reprFloat(x: float): string {.compilerproc.} = return $x proc reprPointer(x: pointer): string {.compilerproc.} = - var buf: array[0..59, char] - discard c_sprintf(buf, "%p", x) - return newString(buf) + result = newString(60) + let newLen = c_sprintf(result[0].addr, "%p", x) + result.setLen newLen proc `$`(x: uint64): string = if x == 0: result = "0" else: - var buf: array[60, char] + result = newString(60) var i = 0 var n = x while n != 0: let nn = n div 10'u64 - buf[i] = char(n - 10'u64 * nn + ord('0')) + result[i] = char(n - 10'u64 * nn + ord('0')) inc i n = nn + result.setLen i let half = i div 2 # Reverse - for t in 0 .. < half: swap(buf[t], buf[i-t-1]) - result = newString(buf) + for t in 0 .. < half: swap(result[t], result[i-t-1]) proc reprStrAux(result: var string, s: cstring; len: int) = if cast[pointer](s) == nil: From ddea990a70606aaa19513510ae4b47ada7e49fe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Tue, 6 Jun 2017 21:19:50 +0200 Subject: [PATCH 3/9] removed newString proc again, reverted some unnecesary changes --- compiler/rodutils.nim | 6 +++--- lib/pure/nativesockets.nim | 4 ++-- lib/pure/os.nim | 2 +- lib/system.nim | 15 --------------- lib/system/repr.nim | 6 +++--- tests/async/tnewasyncudp.nim | 6 +++--- tests/misc/treadx.nim | 3 +-- tests/system/toString.nim | 12 +++--------- 8 files changed, 16 insertions(+), 38 deletions(-) diff --git a/compiler/rodutils.nim b/compiler/rodutils.nim index 1352afadb4..3a90a207c0 100644 --- a/compiler/rodutils.nim +++ b/compiler/rodutils.nim @@ -21,9 +21,9 @@ proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string = if f > 0.0: result = "INF" else: result = "-INF" else: - result = newString(80) - let newLen = c_snprintf(result[0].addr, 81, "%#.16e" & literalPostfix, f) - result.setLen(newLen) + var buf: array[0..80, char] + let newLen = c_snprintf(buf.cstring, buf.len.uint, "%#.16e%s", f, literalPostfix.cstring) + result = $buf.cstring proc encodeStr*(s: string, result: var string) = for i in countup(0, len(s) - 1): diff --git a/lib/pure/nativesockets.nim b/lib/pure/nativesockets.nim index 7568408a6c..a338364584 100644 --- a/lib/pure/nativesockets.nim +++ b/lib/pure/nativesockets.nim @@ -500,7 +500,7 @@ proc getLocalAddr*(socket: SocketHandle, domain: Domain): (string, Port) = if inet_ntop(name.sin6_family.cint, addr name, buf.cstring, sizeof(buf).int32).isNil: raiseOSError(osLastError()) - result = ($buf, Port(nativesockets.ntohs(name.sin6_port))) + result = ($buf.cstring, Port(nativesockets.ntohs(name.sin6_port))) else: raiseOSError(OSErrorCode(-1), "invalid socket family in getLocalAddr") @@ -536,7 +536,7 @@ proc getPeerAddr*(socket: SocketHandle, domain: Domain): (string, Port) = if inet_ntop(name.sin6_family.cint, addr name, buf.cstring, sizeof(buf).int32).isNil: raiseOSError(osLastError()) - result = ($buf, Port(nativesockets.ntohs(name.sin6_port))) + result = ($buf.cstring, Port(nativesockets.ntohs(name.sin6_port))) else: raiseOSError(OSErrorCode(-1), "invalid socket family in getLocalAddr") diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 5211bc00c8..a227e9f324 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -1010,7 +1010,7 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path: while true: var x = readdir(d) if x == nil: break - var y = newString(x.d_name) + var y = $x.d_name.cstring if y != "." and y != "..": var s: Stat if not relative: diff --git a/lib/system.nim b/lib/system.nim index b5008129d6..0145380983 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -1872,21 +1872,6 @@ 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 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. - 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.} proc getRefcount*(x: string): int {.importc: "getRefcount", noSideEffect.} diff --git a/lib/system/repr.nim b/lib/system/repr.nim index 2775b1b3ed..172b4c08c0 100644 --- a/lib/system/repr.nim +++ b/lib/system/repr.nim @@ -16,9 +16,9 @@ proc reprInt(x: int64): string {.compilerproc.} = return $x proc reprFloat(x: float): string {.compilerproc.} = return $x proc reprPointer(x: pointer): string {.compilerproc.} = - result = newString(60) - let newLen = c_sprintf(result[0].addr, "%p", x) - result.setLen newLen + var buf: array[60, char] + discard c_sprintf(result[0].addr, "%p", x) + result = $buf.cstring proc `$`(x: uint64): string = if x == 0: diff --git a/tests/async/tnewasyncudp.nim b/tests/async/tnewasyncudp.nim index bf54c0d06a..da731f4b86 100644 --- a/tests/async/tnewasyncudp.nim +++ b/tests/async/tnewasyncudp.nim @@ -54,7 +54,7 @@ proc launchSwarm(name: ptr SockAddr) {.async.} = k = 0 while k < messagesToSend: zeroMem(addr(buffer[0]), 16384) - zeroMem(cast[pointer](addr(saddr)), sizeof(Sockaddr_in)) + zeroMem(cast[pointer](addr(saddr)), sizeof(Sockaddr_in)) var message = "Message " & $(i * messagesToSend + k) await sendTo(sock, addr message[0], len(message), name, sizeof(Sockaddr_in).SockLen) @@ -62,7 +62,7 @@ proc launchSwarm(name: ptr SockAddr) {.async.} = 16384, cast[ptr SockAddr](addr saddr), addr slen) size = 0 - var grammString = $buffer + var grammString = $buffer.cstring if grammString == message: saveSendingPort(sockport) inc(recvCount) @@ -84,7 +84,7 @@ proc readMessages(server: AsyncFD) {.async.} = 16384, cast[ptr SockAddr](addr(saddr)), addr(slen)) size = 0 - var grammString = $buffer + var grammString = $buffer.cstring if grammString.startswith("Message ") and saddr.sin_addr.s_addr == 0x100007F: await sendTo(server, addr grammString[0], len(grammString), diff --git a/tests/misc/treadx.nim b/tests/misc/treadx.nim index 49b6ad6912..2e3904cd12 100644 --- a/tests/misc/treadx.nim +++ b/tests/misc/treadx.nim @@ -6,9 +6,8 @@ when not defined(windows): var buf: array[0..10, char] while true: var r = read(0, addr(buf), sizeof(buf)-1) - add inp, $buf + add inp, $buf.cstring if r != sizeof(buf)-1: break echo inp #dafkladskölklödsaf ölksdakölfölksfklwe4iojr389wr 89uweokf sdlkf jweklr jweflksdj fioewjfsdlfsd - diff --git a/tests/system/toString.nim b/tests/system/toString.nim index f9697ec759..cd9a492725 100644 --- a/tests/system/toString.nim +++ b/tests/system/toString.nim @@ -13,9 +13,6 @@ inf nan nil nil -(a: 0, b: nil) -nil -ptr (a: 0, b: nil)''' """ echo($(@[23, 45])) @@ -49,12 +46,8 @@ type b: string var foo1: Foo -var foo2: ref Foo -var foo3: ptr Foo = foo1.addr -echo foo1 -echo foo2 -echo foo3 +doAssert $foo1 == "(a: 0, b: nil)" const data = @['a','b', '\0', 'c','d'] @@ -67,5 +60,6 @@ import strutils # array test let arr = ['H','e','l','l','o',' ','W','o','r','l','d','!','\0'] +# not sure if this is really a good idea doAssert startsWith($arr, "[H, e, l, l, o, , W, o, r, l, d, !,") -doAssert newString(arr) == "Hello World!" +doAssert $arr.cstring == "Hello World!" From c9a2acefc1f34801df4e57d8a2462bff4a0666d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Tue, 6 Jun 2017 22:33:52 +0200 Subject: [PATCH 4/9] fix --- lib/system/repr.nim | 2 +- tests/gc/gctest.nim | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/system/repr.nim b/lib/system/repr.nim index 172b4c08c0..58c86b0dbe 100644 --- a/lib/system/repr.nim +++ b/lib/system/repr.nim @@ -17,7 +17,7 @@ proc reprFloat(x: float): string {.compilerproc.} = return $x proc reprPointer(x: pointer): string {.compilerproc.} = var buf: array[60, char] - discard c_sprintf(result[0].addr, "%p", x) + discard c_sprintf(buf.cstring, "%p", x) result = $buf.cstring proc `$`(x: uint64): string = diff --git a/tests/gc/gctest.nim b/tests/gc/gctest.nim index b3b9af6080..f5c81f033a 100644 --- a/tests/gc/gctest.nim +++ b/tests/gc/gctest.nim @@ -31,7 +31,7 @@ type of nkList: sons: seq[PCaseNode] else: unused: seq[string] - TIdObj* = object of TObject + TIdObj* = object of RootObj id*: int # unique id; use this for comparisons and not the pointers PIdObj* = ref TIdObj From a6e6b05565ad717c77d6d66b3c6fd865ad0f615d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Wed, 19 Jul 2017 00:08:00 +0200 Subject: [PATCH 5/9] made toString test assert based --- tests/system/toString.nim | 54 +++++++++++++++------------------------ 1 file changed, 21 insertions(+), 33 deletions(-) diff --git a/tests/system/toString.nim b/tests/system/toString.nim index cd9a492725..d002bb379f 100644 --- a/tests/system/toString.nim +++ b/tests/system/toString.nim @@ -1,44 +1,32 @@ discard """ - output:'''@[23, 45] -@[, foo, bar] -{a, b, c} -2.3242 -2.982 -123912.1 -123912.1823 -5.0 -1e+100 -inf --inf -nan -nil -nil + output:"" """ -echo($(@[23, 45])) -echo($(@["", "foo", "bar"])) -#echo($(["", "foo", "bar"])) -#echo($([23, 45])) +doAssert "@[23, 45]" == $(@[23, 45]) +doAssert "[32, 45]" == $([32, 45]) +doAssert "@[, foo, bar]" == $(@["", "foo", "bar"]) +doAssert "[, foo, bar]" == $(["", "foo", "bar"]) + # bug #2395 - let alphaSet: set[char] = {'a'..'c'} -echo alphaSet - -echo($(2.3242)) -echo($(2.982)) -echo($(123912.1)) -echo($(123912.1823)) -echo($(5.0)) -echo($(1e100)) -echo($(1e1000000)) -echo($(-1e1000000)) -echo($(0.0/0.0)) +doAssert "{a, b, c}" == $alphaSet +doAssert "2.3242" == $(2.3242) +doAssert "2.982" == $(2.982) +doAssert "123912.1" == $(123912.1) +doAssert "123912.1823" == $(123912.1823) +doAssert "5.0" == $(5.0) +doAssert "1e+100" == $(1e100) +doAssert "inf" == $(1e1000000) +doAssert "-inf" == $(-1e1000000) +doAssert "nan" == $(0.0/0.0) # nil tests +# maybe a bit inconsistent in types var x: seq[string] -var y: string -echo(x) -echo(y) +doAssert "nil" == $(x) + +var y: string = nil +doAssert nil == $(y) type Foo = object From 11914a23bebe4499f4161edb9a421e37a3182abe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Mon, 24 Jul 2017 23:55:30 +0200 Subject: [PATCH 6/9] prevent null characters in $ on collections of char --- lib/system.nim | 9 ++++++++- tests/system/toString.nim | 4 +--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/system.nim b/lib/system.nim index 0145380983..0bc0a0dbfb 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2425,6 +2425,14 @@ proc collectionToString[T](x: T, prefix, separator, suffix: string): string = result.add "nil" else: result.add($value) + # prevent temporary string allocation + elif compiles(result.add(value)): + # don't insert '\0' characters into the result string + when value is char: + if value != '\0': + result.add(value) + else: + result.add(value) else: result.add($value) @@ -3307,7 +3315,6 @@ 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) diff --git a/tests/system/toString.nim b/tests/system/toString.nim index d002bb379f..0eed596aee 100644 --- a/tests/system/toString.nim +++ b/tests/system/toString.nim @@ -47,7 +47,5 @@ doAssert dataStr == $data import strutils # array test let arr = ['H','e','l','l','o',' ','W','o','r','l','d','!','\0'] - -# not sure if this is really a good idea -doAssert startsWith($arr, "[H, e, l, l, o, , W, o, r, l, d, !,") +doAssert $arr == "[H, e, l, l, o, , W, o, r, l, d, !, ]" doAssert $arr.cstring == "Hello World!" From 3bf1f019a76c6fee5553a7daf7d984cfd39cfb04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Mon, 7 Aug 2017 17:54:05 +0200 Subject: [PATCH 7/9] improved genEcho --- compiler/ccgexprs.nim | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index de784acf9c..d971c0bc24 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -953,23 +953,40 @@ proc genEcho(p: BProc, n: PNode) = # this unusal way of implementing it ensures that e.g. ``echo("hallo", 45)`` # is threadsafe. internalAssert n.kind == nkBracket - var args: Rope = nil + var args = newSeq[tuple[hasvalue,len,str: string]](0) var a: TLoc for i in countup(0, n.len-1): if n.sons[i].skipConv.kind == nkNilLit: - add(args, ", \"nil\"") + args.add((hasvalue:"0", len: "0", str: "\"\"")) else: initLocExpr(p, n.sons[i], a) - addf(args, ", $1? ($1)->data:\"nil\"", [rdLoc(a)]) + let ptrexpr: string = $rdLoc(a) + let len = format("($1)->Sup.len", ptrexpr) + let str = format("($1)->data", ptrexpr) + args.add((hasvalue: ptrexpr, len: len, str: str)) if platform.targetOS == osGenode: # bypass libc and print directly to the Genode LOG session p.module.includeHeader("") - linefmt(p, cpsStmts, """Genode::log(""$1);$n""", args) + # separated args + var rope: Rope + for arg in args: + rope.add ", " + rope.add arg.str + # TODO this is wrong, it does not properly handle '\0' characters, and locking is not done. + linefmt(p, cpsStmts, """Genode::log(""$1);$n""", rope) + for arg in args: + if arg.hasvalue != "0": # if we know statically that there is no string, it can be skipped + linefmt(p, cpsStmts, "if($1) Genode::log($2);$n", rope(arg.hasvalue), rope(arg.str)) else: p.module.includeHeader("") - linefmt(p, cpsStmts, "printf($1$2);$n", - makeCString(repeat("%s", n.len) & tnl), args) - linefmt(p, cpsStmts, "fflush(stdout);$n") + #linefmt(p, cpsStmts, "printf($1$2);$n", + # makeCString(repeat("%s", n.len) & tnl), args) + linefmt(p, cpsStmts, "flockfile(stdout);$n") + for arg in args: + if arg.hasvalue != "0": # if we know statically that there is no string, it can be skipped + linefmt(p, cpsStmts, "if($1) fwrite($2, 1, $3, stdout);$n", rope(arg.hasvalue), rope(arg.str), rope(arg.len)); + linefmt(p, cpsStmts, "putchar('\\n'); fflush(stdout);$n") + linefmt(p, cpsStmts, "funlockfile(stdout);$n") proc gcUsage(n: PNode) = if gSelectedGC == gcNone: message(n.info, warnGcMem, n.renderTree) From 54808ab12fcbf8cc253129ed03f560fc6dd2648e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Mon, 7 Aug 2017 18:21:21 +0200 Subject: [PATCH 8/9] don't filter '\0' characters in string generation --- compiler/rodutils.nim | 2 +- lib/system.nim | 7 +------ tests/system/toString.nim | 3 ++- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/compiler/rodutils.nim b/compiler/rodutils.nim index 3a90a207c0..6e77e6b8f9 100644 --- a/compiler/rodutils.nim +++ b/compiler/rodutils.nim @@ -22,7 +22,7 @@ proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string = else: result = "-INF" else: var buf: array[0..80, char] - let newLen = c_snprintf(buf.cstring, buf.len.uint, "%#.16e%s", f, literalPostfix.cstring) + discard c_snprintf(buf.cstring, buf.len.uint, "%#.16e%s", f, literalPostfix.cstring) result = $buf.cstring proc encodeStr*(s: string, result: var string) = diff --git a/lib/system.nim b/lib/system.nim index 0bc0a0dbfb..d2bdeae0e5 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2427,12 +2427,7 @@ proc collectionToString[T](x: T, prefix, separator, suffix: string): string = result.add($value) # prevent temporary string allocation elif compiles(result.add(value)): - # don't insert '\0' characters into the result string - when value is char: - if value != '\0': - result.add(value) - else: - result.add(value) + result.add(value) else: result.add($value) diff --git a/tests/system/toString.nim b/tests/system/toString.nim index 0eed596aee..377336c90c 100644 --- a/tests/system/toString.nim +++ b/tests/system/toString.nim @@ -46,6 +46,7 @@ doAssert dataStr == $data import strutils # array test + let arr = ['H','e','l','l','o',' ','W','o','r','l','d','!','\0'] -doAssert $arr == "[H, e, l, l, o, , W, o, r, l, d, !, ]" +doAssert $arr == "[H, e, l, l, o, , W, o, r, l, d, !, \0]" doAssert $arr.cstring == "Hello World!" From 608cc18178ad8618767e50552fe51d1d179a6aae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Tue, 8 Aug 2017 13:58:50 +0200 Subject: [PATCH 9/9] reverted genEcho --- compiler/ccgexprs.nim | 31 +++++++------------------------ tests/system/toString.nim | 3 ++- 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index d971c0bc24..de784acf9c 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -953,40 +953,23 @@ proc genEcho(p: BProc, n: PNode) = # this unusal way of implementing it ensures that e.g. ``echo("hallo", 45)`` # is threadsafe. internalAssert n.kind == nkBracket - var args = newSeq[tuple[hasvalue,len,str: string]](0) + var args: Rope = nil var a: TLoc for i in countup(0, n.len-1): if n.sons[i].skipConv.kind == nkNilLit: - args.add((hasvalue:"0", len: "0", str: "\"\"")) + add(args, ", \"nil\"") else: initLocExpr(p, n.sons[i], a) - let ptrexpr: string = $rdLoc(a) - let len = format("($1)->Sup.len", ptrexpr) - let str = format("($1)->data", ptrexpr) - args.add((hasvalue: ptrexpr, len: len, str: str)) + addf(args, ", $1? ($1)->data:\"nil\"", [rdLoc(a)]) if platform.targetOS == osGenode: # bypass libc and print directly to the Genode LOG session p.module.includeHeader("") - # separated args - var rope: Rope - for arg in args: - rope.add ", " - rope.add arg.str - # TODO this is wrong, it does not properly handle '\0' characters, and locking is not done. - linefmt(p, cpsStmts, """Genode::log(""$1);$n""", rope) - for arg in args: - if arg.hasvalue != "0": # if we know statically that there is no string, it can be skipped - linefmt(p, cpsStmts, "if($1) Genode::log($2);$n", rope(arg.hasvalue), rope(arg.str)) + linefmt(p, cpsStmts, """Genode::log(""$1);$n""", args) else: p.module.includeHeader("") - #linefmt(p, cpsStmts, "printf($1$2);$n", - # makeCString(repeat("%s", n.len) & tnl), args) - linefmt(p, cpsStmts, "flockfile(stdout);$n") - for arg in args: - if arg.hasvalue != "0": # if we know statically that there is no string, it can be skipped - linefmt(p, cpsStmts, "if($1) fwrite($2, 1, $3, stdout);$n", rope(arg.hasvalue), rope(arg.str), rope(arg.len)); - linefmt(p, cpsStmts, "putchar('\\n'); fflush(stdout);$n") - linefmt(p, cpsStmts, "funlockfile(stdout);$n") + linefmt(p, cpsStmts, "printf($1$2);$n", + makeCString(repeat("%s", n.len) & tnl), args) + linefmt(p, cpsStmts, "fflush(stdout);$n") proc gcUsage(n: PNode) = if gSelectedGC == gcNone: message(n.info, warnGcMem, n.renderTree) diff --git a/tests/system/toString.nim b/tests/system/toString.nim index 377336c90c..bc8720b554 100644 --- a/tests/system/toString.nim +++ b/tests/system/toString.nim @@ -35,7 +35,8 @@ type var foo1: Foo -doAssert $foo1 == "(a: 0, b: nil)" +# nil string should be an some point in time equal to the empty string +doAssert(($foo1)[0..9] == "(a: 0, b: ") const data = @['a','b', '\0', 'c','d']