Merge branch 'stringify-array' of https://github.com/krux02/Nim into krux02-stringify-array
This commit is contained in:
commit
db0d353281
9 changed files with 89 additions and 71 deletions
|
|
@ -10,7 +10,7 @@
|
||||||
## Serialization utilities for the compiler.
|
## Serialization utilities for the compiler.
|
||||||
import strutils
|
import strutils
|
||||||
|
|
||||||
proc c_sprintf(buf, frmt: cstring) {.importc: "sprintf", header: "<stdio.h>", nodecl, varargs.}
|
proc c_snprintf(s: cstring; n:uint; frmt: cstring): cint {.importc: "snprintf", header: "<stdio.h>", nodecl, varargs.}
|
||||||
|
|
||||||
proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string =
|
proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string =
|
||||||
if f != f:
|
if f != f:
|
||||||
|
|
@ -22,8 +22,8 @@ proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string =
|
||||||
else: result = "-INF"
|
else: result = "-INF"
|
||||||
else:
|
else:
|
||||||
var buf: array[0..80, char]
|
var buf: array[0..80, char]
|
||||||
c_sprintf(buf, "%#.16e" & literalPostfix, f)
|
discard c_snprintf(buf.cstring, buf.len.uint, "%#.16e%s", f, literalPostfix.cstring)
|
||||||
result = $buf
|
result = $buf.cstring
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -500,7 +500,7 @@ proc getLocalAddr*(socket: SocketHandle, domain: Domain): (string, Port) =
|
||||||
if inet_ntop(name.sin6_family.cint,
|
if inet_ntop(name.sin6_family.cint,
|
||||||
addr name.sin6_addr, buf.cstring, sizeof(buf).int32).isNil:
|
addr name.sin6_addr, buf.cstring, sizeof(buf).int32).isNil:
|
||||||
raiseOSError(osLastError())
|
raiseOSError(osLastError())
|
||||||
result = ($buf, Port(nativesockets.ntohs(name.sin6_port)))
|
result = ($buf.cstring, Port(nativesockets.ntohs(name.sin6_port)))
|
||||||
else:
|
else:
|
||||||
raiseOSError(OSErrorCode(-1), "invalid socket family in getLocalAddr")
|
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,
|
if inet_ntop(name.sin6_family.cint,
|
||||||
addr name.sin6_addr, buf.cstring, sizeof(buf).int32).isNil:
|
addr name.sin6_addr, buf.cstring, sizeof(buf).int32).isNil:
|
||||||
raiseOSError(osLastError())
|
raiseOSError(osLastError())
|
||||||
result = ($buf, Port(nativesockets.ntohs(name.sin6_port)))
|
result = ($buf.cstring, Port(nativesockets.ntohs(name.sin6_port)))
|
||||||
else:
|
else:
|
||||||
raiseOSError(OSErrorCode(-1), "invalid socket family in getLocalAddr")
|
raiseOSError(OSErrorCode(-1), "invalid socket family in getLocalAddr")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -790,7 +790,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 = $x.d_name.cstring
|
||||||
if y != "." and y != "..":
|
if y != "." and y != "..":
|
||||||
var s: Stat
|
var s: Stat
|
||||||
if not relative:
|
if not relative:
|
||||||
|
|
|
||||||
|
|
@ -1875,7 +1875,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.
|
||||||
|
|
@ -2437,20 +2437,28 @@ 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:
|
else:
|
||||||
result.add($value)
|
result.add($value)
|
||||||
firstElement = false
|
# prevent temporary string allocation
|
||||||
result.add(e)
|
elif compiles(result.add(value)):
|
||||||
|
result.add(value)
|
||||||
|
else:
|
||||||
|
result.add($value)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
@ -2458,7 +2466,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
|
||||||
|
|
@ -2466,13 +2474,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 ---------------------------------------------
|
||||||
|
|
||||||
|
|
@ -3329,6 +3334,10 @@ 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)
|
||||||
|
|
|
||||||
|
|
@ -16,27 +16,27 @@ proc reprInt(x: int64): string {.compilerproc.} = return $x
|
||||||
proc reprFloat(x: float): string {.compilerproc.} = return $x
|
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[60, char]
|
||||||
discard c_sprintf(buf, "%p", x)
|
discard c_sprintf(buf.cstring, "%p", x)
|
||||||
return $buf
|
result = $buf.cstring
|
||||||
|
|
||||||
proc `$`(x: uint64): string =
|
proc `$`(x: uint64): string =
|
||||||
if x == 0:
|
if x == 0:
|
||||||
result = "0"
|
result = "0"
|
||||||
else:
|
else:
|
||||||
var buf: array[60, char]
|
result = newString(60)
|
||||||
var i = 0
|
var i = 0
|
||||||
var n = x
|
var n = x
|
||||||
while n != 0:
|
while n != 0:
|
||||||
let nn = n div 10'u64
|
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
|
inc i
|
||||||
n = nn
|
n = nn
|
||||||
|
result.setLen i
|
||||||
|
|
||||||
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(result[t], result[i-t-1])
|
||||||
result = $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:
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ proc launchSwarm(name: ptr SockAddr) {.async.} =
|
||||||
16384, cast[ptr SockAddr](addr saddr),
|
16384, cast[ptr SockAddr](addr saddr),
|
||||||
addr slen)
|
addr slen)
|
||||||
size = 0
|
size = 0
|
||||||
var grammString = $buffer
|
var grammString = $buffer.cstring
|
||||||
if grammString == message:
|
if grammString == message:
|
||||||
saveSendingPort(sockport)
|
saveSendingPort(sockport)
|
||||||
inc(recvCount)
|
inc(recvCount)
|
||||||
|
|
@ -84,7 +84,7 @@ proc readMessages(server: AsyncFD) {.async.} =
|
||||||
16384, cast[ptr SockAddr](addr(saddr)),
|
16384, cast[ptr SockAddr](addr(saddr)),
|
||||||
addr(slen))
|
addr(slen))
|
||||||
size = 0
|
size = 0
|
||||||
var grammString = $buffer
|
var grammString = $buffer.cstring
|
||||||
if grammString.startswith("Message ") and
|
if grammString.startswith("Message ") and
|
||||||
saddr.sin_addr.s_addr == 0x100007F:
|
saddr.sin_addr.s_addr == 0x100007F:
|
||||||
await sendTo(server, addr grammString[0], len(grammString),
|
await sendTo(server, addr grammString[0], len(grammString),
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ type
|
||||||
of nkList: sons: seq[PCaseNode]
|
of nkList: sons: seq[PCaseNode]
|
||||||
else: unused: seq[string]
|
else: unused: seq[string]
|
||||||
|
|
||||||
TIdObj* = object of TObject
|
TIdObj* = object of RootObj
|
||||||
id*: int # unique id; use this for comparisons and not the pointers
|
id*: int # unique id; use this for comparisons and not the pointers
|
||||||
|
|
||||||
PIdObj* = ref TIdObj
|
PIdObj* = ref TIdObj
|
||||||
|
|
|
||||||
|
|
@ -6,9 +6,8 @@ when not defined(windows):
|
||||||
var buf: array[0..10, char]
|
var buf: array[0..10, char]
|
||||||
while true:
|
while true:
|
||||||
var r = read(0, addr(buf), sizeof(buf)-1)
|
var r = read(0, addr(buf), sizeof(buf)-1)
|
||||||
add inp, $buf
|
add inp, $buf.cstring
|
||||||
if r != sizeof(buf)-1: break
|
if r != sizeof(buf)-1: break
|
||||||
|
|
||||||
echo inp
|
echo inp
|
||||||
#dafkladskölklödsaf ölksdakölfölksfklwe4iojr389wr 89uweokf sdlkf jweklr jweflksdj fioewjfsdlfsd
|
#dafkladskölklödsaf ölksdakölfölksfklwe4iojr389wr 89uweokf sdlkf jweklr jweflksdj fioewjfsdlfsd
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,42 +1,53 @@
|
||||||
discard """
|
discard """
|
||||||
output:'''@[23, 45]
|
output:""
|
||||||
@[, foo, bar]
|
|
||||||
{a, b, c}
|
|
||||||
2.3242
|
|
||||||
2.982
|
|
||||||
123912.1
|
|
||||||
123912.1823
|
|
||||||
5.0
|
|
||||||
1e+100
|
|
||||||
inf
|
|
||||||
-inf
|
|
||||||
nan
|
|
||||||
nil
|
|
||||||
nil'''
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
echo($(@[23, 45]))
|
doAssert "@[23, 45]" == $(@[23, 45])
|
||||||
echo($(@["", "foo", "bar"]))
|
doAssert "[32, 45]" == $([32, 45])
|
||||||
#echo($(["", "foo", "bar"]))
|
doAssert "@[, foo, bar]" == $(@["", "foo", "bar"])
|
||||||
#echo($([23, 45]))
|
doAssert "[, foo, bar]" == $(["", "foo", "bar"])
|
||||||
|
|
||||||
# bug #2395
|
# bug #2395
|
||||||
|
|
||||||
let alphaSet: set[char] = {'a'..'c'}
|
let alphaSet: set[char] = {'a'..'c'}
|
||||||
echo alphaSet
|
doAssert "{a, b, c}" == $alphaSet
|
||||||
|
doAssert "2.3242" == $(2.3242)
|
||||||
echo($(2.3242))
|
doAssert "2.982" == $(2.982)
|
||||||
echo($(2.982))
|
doAssert "123912.1" == $(123912.1)
|
||||||
echo($(123912.1))
|
doAssert "123912.1823" == $(123912.1823)
|
||||||
echo($(123912.1823))
|
doAssert "5.0" == $(5.0)
|
||||||
echo($(5.0))
|
doAssert "1e+100" == $(1e100)
|
||||||
echo($(1e100))
|
doAssert "inf" == $(1e1000000)
|
||||||
echo($(1e1000000))
|
doAssert "-inf" == $(-1e1000000)
|
||||||
echo($(-1e1000000))
|
doAssert "nan" == $(0.0/0.0)
|
||||||
echo($(0.0/0.0))
|
|
||||||
|
|
||||||
# nil tests
|
# nil tests
|
||||||
|
# maybe a bit inconsistent in types
|
||||||
var x: seq[string]
|
var x: seq[string]
|
||||||
var y: string
|
doAssert "nil" == $(x)
|
||||||
echo(x)
|
|
||||||
echo(y)
|
var y: string = nil
|
||||||
|
doAssert nil == $(y)
|
||||||
|
|
||||||
|
type
|
||||||
|
Foo = object
|
||||||
|
a: int
|
||||||
|
b: string
|
||||||
|
|
||||||
|
var foo1: Foo
|
||||||
|
|
||||||
|
# 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']
|
||||||
|
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 $arr == "[H, e, l, l, o, , W, o, r, l, d, !, \0]"
|
||||||
|
doAssert $arr.cstring == "Hello World!"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue