arrays can now be printed
This commit is contained in:
parent
52ff244d5d
commit
1f7fc7f279
5 changed files with 81 additions and 25 deletions
|
|
@ -23,7 +23,7 @@ proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string =
|
||||||
else:
|
else:
|
||||||
var buf: array[0..80, char]
|
var buf: array[0..80, char]
|
||||||
c_sprintf(buf, "%#.16e" & literalPostfix, f)
|
c_sprintf(buf, "%#.16e" & literalPostfix, f)
|
||||||
result = $buf
|
result = newString(buf)
|
||||||
|
|
||||||
proc encodeStr*(s: string, result: var string) =
|
proc encodeStr*(s: string, result: var string) =
|
||||||
for i in countup(0, len(s) - 1):
|
for i in countup(0, len(s) - 1):
|
||||||
|
|
@ -133,4 +133,3 @@ iterator decodeStrArray*(s: cstring): string =
|
||||||
while s[i] != '\0':
|
while s[i] != '\0':
|
||||||
yield decodeStr(s, i)
|
yield decodeStr(s, i)
|
||||||
if s[i] == ' ': inc i
|
if s[i] == ' ': inc i
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1010,7 +1010,7 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path:
|
||||||
while true:
|
while true:
|
||||||
var x = readdir(d)
|
var x = readdir(d)
|
||||||
if x == nil: break
|
if x == nil: break
|
||||||
var y = $x.d_name
|
var y = newString(x.d_name)
|
||||||
if y != "." and y != "..":
|
if y != "." and y != "..":
|
||||||
var s: Stat
|
var s: Stat
|
||||||
if not relative:
|
if not relative:
|
||||||
|
|
|
||||||
|
|
@ -1852,7 +1852,7 @@ proc `$` *(x: float): string {.magic: "FloatToStr", noSideEffect.}
|
||||||
proc `$` *(x: bool): string {.magic: "BoolToStr", noSideEffect.}
|
proc `$` *(x: bool): string {.magic: "BoolToStr", noSideEffect.}
|
||||||
## The stringify operator for a boolean argument. Returns `x`
|
## The stringify operator for a boolean argument. Returns `x`
|
||||||
## converted to the string "false" or "true".
|
## converted to the string "false" or "true".
|
||||||
|
#
|
||||||
proc `$` *(x: char): string {.magic: "CharToStr", noSideEffect.}
|
proc `$` *(x: char): string {.magic: "CharToStr", noSideEffect.}
|
||||||
## The stringify operator for a character argument. Returns `x`
|
## The stringify operator for a character argument. Returns `x`
|
||||||
## converted to a string.
|
## 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
|
## a ``$`` operator for a concrete enumeration is provided, this is
|
||||||
## used instead. (In other words: *Overwriting* is possible.)
|
## 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:
|
# undocumented:
|
||||||
proc getRefcount*[T](x: ref T): int {.importc: "getRefcount", noSideEffect.}
|
proc getRefcount*[T](x: ref T): int {.importc: "getRefcount", noSideEffect.}
|
||||||
proc getRefcount*(x: string): 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("...")
|
||||||
result.add(")")
|
result.add(")")
|
||||||
|
|
||||||
proc collectionToString[T: set | seq](x: T, b, e: string): string =
|
proc collectionToString[T](x: T, prefix, separator, suffix: string): string =
|
||||||
when x is seq:
|
result = prefix
|
||||||
if x.isNil: return "nil"
|
|
||||||
result = b
|
|
||||||
var firstElement = true
|
var firstElement = true
|
||||||
for value in items(x):
|
for value in items(x):
|
||||||
if not firstElement: result.add(", ")
|
if firstElement:
|
||||||
|
firstElement = false
|
||||||
|
else:
|
||||||
|
result.add(separator)
|
||||||
|
|
||||||
when compiles(value.isNil):
|
when compiles(value.isNil):
|
||||||
if value.isNil: result.add "nil"
|
# this branch should not be necessary
|
||||||
else: result.add($value)
|
if value.isNil:
|
||||||
|
result.add "nil"
|
||||||
|
else:
|
||||||
|
result.add($value)
|
||||||
else:
|
else:
|
||||||
result.add($value)
|
result.add($value)
|
||||||
firstElement = false
|
|
||||||
result.add(e)
|
result.add(suffix)
|
||||||
|
|
||||||
proc `$`*[T](x: set[T]): string =
|
proc `$`*[T](x: set[T]): string =
|
||||||
## generic ``$`` operator for sets that is lifted from the components
|
## generic ``$`` operator for sets that is lifted from the components
|
||||||
|
|
@ -2431,7 +2457,7 @@ proc `$`*[T](x: set[T]): string =
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
## ${23, 45} == "{23, 45}"
|
## ${23, 45} == "{23, 45}"
|
||||||
collectionToString(x, "{", "}")
|
collectionToString(x, "{", ", ", "}")
|
||||||
|
|
||||||
proc `$`*[T](x: seq[T]): string =
|
proc `$`*[T](x: seq[T]): string =
|
||||||
## generic ``$`` operator for seqs that is lifted from the components
|
## generic ``$`` operator for seqs that is lifted from the components
|
||||||
|
|
@ -2439,13 +2465,10 @@ proc `$`*[T](x: seq[T]): string =
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
## $(@[23, 45]) == "@[23, 45]"
|
## $(@[23, 45]) == "@[23, 45]"
|
||||||
collectionToString(x, "@[", "]")
|
if x.isNil:
|
||||||
|
"nil"
|
||||||
when false:
|
else:
|
||||||
# causes bootstrapping to fail as we use array of chars and cstring should
|
collectionToString(x, "@[", ", ", "]")
|
||||||
# match better ...
|
|
||||||
proc `$`*[T, IDX](x: array[IDX, T]): string =
|
|
||||||
collectionToString(x, "[", "]")
|
|
||||||
|
|
||||||
# ----------------- GC interface ---------------------------------------------
|
# ----------------- GC interface ---------------------------------------------
|
||||||
|
|
||||||
|
|
@ -3301,6 +3324,11 @@ elif defined(JS):
|
||||||
include "system/sysio"
|
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.} =
|
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)
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ proc reprFloat(x: float): string {.compilerproc.} = return $x
|
||||||
proc reprPointer(x: pointer): string {.compilerproc.} =
|
proc reprPointer(x: pointer): string {.compilerproc.} =
|
||||||
var buf: array[0..59, char]
|
var buf: array[0..59, char]
|
||||||
discard c_sprintf(buf, "%p", x)
|
discard c_sprintf(buf, "%p", x)
|
||||||
return $buf
|
return newString(buf)
|
||||||
|
|
||||||
proc `$`(x: uint64): string =
|
proc `$`(x: uint64): string =
|
||||||
if x == 0:
|
if x == 0:
|
||||||
|
|
@ -36,7 +36,7 @@ proc `$`(x: uint64): string =
|
||||||
let half = i div 2
|
let half = i div 2
|
||||||
# Reverse
|
# Reverse
|
||||||
for t in 0 .. < half: swap(buf[t], buf[i-t-1])
|
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) =
|
proc reprStrAux(result: var string, s: cstring; len: int) =
|
||||||
if cast[pointer](s) == nil:
|
if cast[pointer](s) == nil:
|
||||||
|
|
|
||||||
|
|
@ -12,14 +12,16 @@ inf
|
||||||
-inf
|
-inf
|
||||||
nan
|
nan
|
||||||
nil
|
nil
|
||||||
nil'''
|
nil
|
||||||
|
(a: 0, b: nil)
|
||||||
|
nil
|
||||||
|
ptr (a: 0, b: nil)'''
|
||||||
"""
|
"""
|
||||||
|
|
||||||
echo($(@[23, 45]))
|
echo($(@[23, 45]))
|
||||||
echo($(@["", "foo", "bar"]))
|
echo($(@["", "foo", "bar"]))
|
||||||
#echo($(["", "foo", "bar"]))
|
#echo($(["", "foo", "bar"]))
|
||||||
#echo($([23, 45]))
|
#echo($([23, 45]))
|
||||||
|
|
||||||
# bug #2395
|
# bug #2395
|
||||||
|
|
||||||
let alphaSet: set[char] = {'a'..'c'}
|
let alphaSet: set[char] = {'a'..'c'}
|
||||||
|
|
@ -40,3 +42,30 @@ var x: seq[string]
|
||||||
var y: string
|
var y: string
|
||||||
echo(x)
|
echo(x)
|
||||||
echo(y)
|
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!"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue