removed newString proc again, reverted some unnecesary changes

This commit is contained in:
Arne Döring 2017-06-06 21:19:50 +02:00
commit ddea990a70
8 changed files with 16 additions and 38 deletions

View file

@ -21,9 +21,9 @@ proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string =
if f > 0.0: result = "INF" if f > 0.0: result = "INF"
else: result = "-INF" else: result = "-INF"
else: else:
result = newString(80) var buf: array[0..80, char]
let newLen = c_snprintf(result[0].addr, 81, "%#.16e" & literalPostfix, f) let newLen = c_snprintf(buf.cstring, buf.len.uint, "%#.16e%s", f, literalPostfix.cstring)
result.setLen(newLen) 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):

View file

@ -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, buf.cstring, sizeof(buf).int32).isNil: addr name, 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, buf.cstring, sizeof(buf).int32).isNil: addr name, 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")

View file

@ -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 = newString(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:

View file

@ -1872,21 +1872,6 @@ 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 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: # 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.}

View file

@ -16,9 +16,9 @@ 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.} =
result = newString(60) var buf: array[60, char]
let newLen = c_sprintf(result[0].addr, "%p", x) discard c_sprintf(result[0].addr, "%p", x)
result.setLen newLen result = $buf.cstring
proc `$`(x: uint64): string = proc `$`(x: uint64): string =
if x == 0: if x == 0:

View file

@ -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),

View file

@ -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

View file

@ -13,9 +13,6 @@ inf
nan nan
nil nil
nil nil
(a: 0, b: nil)
nil
ptr (a: 0, b: nil)'''
""" """
echo($(@[23, 45])) echo($(@[23, 45]))
@ -49,12 +46,8 @@ type
b: string b: string
var foo1: Foo var foo1: Foo
var foo2: ref Foo
var foo3: ptr Foo = foo1.addr
echo foo1 doAssert $foo1 == "(a: 0, b: nil)"
echo foo2
echo foo3
const const
data = @['a','b', '\0', 'c','d'] data = @['a','b', '\0', 'c','d']
@ -67,5 +60,6 @@ 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']
# not sure if this is really a good idea
doAssert startsWith($arr, "[H, e, l, l, o, , W, o, r, l, d, !,") doAssert startsWith($arr, "[H, e, l, l, o, , W, o, r, l, d, !,")
doAssert newString(arr) == "Hello World!" doAssert $arr.cstring == "Hello World!"