Merge branch 'stringify-array' of https://github.com/krux02/Nim into krux02-stringify-array

This commit is contained in:
Andreas Rumpf 2017-10-09 23:46:24 +02:00
commit db0d353281
9 changed files with 89 additions and 71 deletions

View file

@ -500,7 +500,7 @@ proc getLocalAddr*(socket: SocketHandle, domain: Domain): (string, Port) =
if inet_ntop(name.sin6_family.cint,
addr name.sin6_addr, 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.sin6_addr, 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")

View file

@ -790,7 +790,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 = $x.d_name.cstring
if y != "." and y != "..":
var s: Stat
if not relative:

View file

@ -1875,7 +1875,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.
@ -2437,20 +2437,28 @@ 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)
# prevent temporary string allocation
elif compiles(result.add(value)):
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
@ -2458,7 +2466,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
@ -2466,13 +2474,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 ---------------------------------------------
@ -3329,6 +3334,10 @@ 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)

View file

@ -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 $buf
var buf: array[60, char]
discard c_sprintf(buf.cstring, "%p", x)
result = $buf.cstring
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 = $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: