prevent null characters in $ on collections of char
This commit is contained in:
parent
a6e6b05565
commit
11914a23be
2 changed files with 9 additions and 4 deletions
|
|
@ -2425,6 +2425,14 @@ proc collectionToString[T](x: T, prefix, separator, suffix: string): string =
|
||||||
result.add "nil"
|
result.add "nil"
|
||||||
else:
|
else:
|
||||||
result.add($value)
|
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:
|
else:
|
||||||
result.add($value)
|
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
|
## generic ``$`` operator for arrays that is lifted from the components
|
||||||
collectionToString(x, "[", ", ", "]")
|
collectionToString(x, "[", ", ", "]")
|
||||||
|
|
||||||
|
|
||||||
proc quit*(errormsg: string, errorcode = QuitFailure) {.noReturn.} =
|
proc quit*(errormsg: string, errorcode = QuitFailure) {.noReturn.} =
|
||||||
## a shorthand for ``echo(errormsg); quit(errorcode)``.
|
## a shorthand for ``echo(errormsg); quit(errorcode)``.
|
||||||
echo(errormsg)
|
echo(errormsg)
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,5 @@ doAssert dataStr == $data
|
||||||
import strutils
|
import strutils
|
||||||
# array test
|
# array test
|
||||||
let arr = ['H','e','l','l','o',' ','W','o','r','l','d','!','\0']
|
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, !, ]"
|
||||||
# not sure if this is really a good idea
|
|
||||||
doAssert startsWith($arr, "[H, e, l, l, o, , W, o, r, l, d, !,")
|
|
||||||
doAssert $arr.cstring == "Hello World!"
|
doAssert $arr.cstring == "Hello World!"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue