diff options
-rw-r--r-- | compiler/rodutils.nim | 6 | ||||
-rw-r--r-- | lib/pure/nativesockets.nim | 4 | ||||
-rw-r--r-- | lib/pure/os.nim | 2 | ||||
-rw-r--r-- | lib/system.nim | 15 | ||||
-rw-r--r-- | lib/system/repr.nim | 6 | ||||
-rw-r--r-- | tests/async/tnewasyncudp.nim | 6 | ||||
-rw-r--r-- | tests/misc/treadx.nim | 3 | ||||
-rw-r--r-- | tests/system/toString.nim | 12 |
8 files changed, 16 insertions, 38 deletions
diff --git a/compiler/rodutils.nim b/compiler/rodutils.nim index 1352afadb..3a90a207c 100644 --- a/compiler/rodutils.nim +++ b/compiler/rodutils.nim @@ -21,9 +21,9 @@ proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string = if f > 0.0: result = "INF" else: result = "-INF" else: - result = newString(80) - let newLen = c_snprintf(result[0].addr, 81, "%#.16e" & literalPostfix, f) - result.setLen(newLen) + var buf: array[0..80, char] + let newLen = c_snprintf(buf.cstring, buf.len.uint, "%#.16e%s", f, literalPostfix.cstring) + result = $buf.cstring proc encodeStr*(s: string, result: var string) = for i in countup(0, len(s) - 1): diff --git a/lib/pure/nativesockets.nim b/lib/pure/nativesockets.nim index 7568408a6..a33836458 100644 --- a/lib/pure/nativesockets.nim +++ b/lib/pure/nativesockets.nim @@ -500,7 +500,7 @@ proc getLocalAddr*(socket: SocketHandle, domain: Domain): (string, Port) = if inet_ntop(name.sin6_family.cint, addr name, 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, 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") diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 5211bc00c..a227e9f32 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -1010,7 +1010,7 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path: while true: var x = readdir(d) if x == nil: break - var y = newString(x.d_name) + var y = $x.d_name.cstring if y != "." and y != "..": var s: Stat if not relative: diff --git a/lib/system.nim b/lib/system.nim index b5008129d..014538098 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -1872,21 +1872,6 @@ proc `$` *[Enum: enum](x: Enum): string {.magic: "EnumToStr", noSideEffect.} ## a ``$`` operator for a concrete enumeration is provided, this is ## 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: proc getRefcount*[T](x: ref T): int {.importc: "getRefcount", noSideEffect.} proc getRefcount*(x: string): int {.importc: "getRefcount", noSideEffect.} diff --git a/lib/system/repr.nim b/lib/system/repr.nim index 2775b1b3e..172b4c08c 100644 --- a/lib/system/repr.nim +++ b/lib/system/repr.nim @@ -16,9 +16,9 @@ proc reprInt(x: int64): string {.compilerproc.} = return $x proc reprFloat(x: float): string {.compilerproc.} = return $x proc reprPointer(x: pointer): string {.compilerproc.} = - result = newString(60) - let newLen = c_sprintf(result[0].addr, "%p", x) - result.setLen newLen + var buf: array[60, char] + discard c_sprintf(result[0].addr, "%p", x) + result = $buf.cstring proc `$`(x: uint64): string = if x == 0: diff --git a/tests/async/tnewasyncudp.nim b/tests/async/tnewasyncudp.nim index bf54c0d06..da731f4b8 100644 --- a/tests/async/tnewasyncudp.nim +++ b/tests/async/tnewasyncudp.nim @@ -54,7 +54,7 @@ proc launchSwarm(name: ptr SockAddr) {.async.} = k = 0 while k < messagesToSend: zeroMem(addr(buffer[0]), 16384) - zeroMem(cast[pointer](addr(saddr)), sizeof(Sockaddr_in)) + zeroMem(cast[pointer](addr(saddr)), sizeof(Sockaddr_in)) var message = "Message " & $(i * messagesToSend + k) await sendTo(sock, addr message[0], len(message), name, sizeof(Sockaddr_in).SockLen) @@ -62,7 +62,7 @@ proc launchSwarm(name: ptr SockAddr) {.async.} = 16384, cast[ptr SockAddr](addr saddr), addr slen) size = 0 - var grammString = $buffer + var grammString = $buffer.cstring if grammString == message: saveSendingPort(sockport) inc(recvCount) @@ -84,7 +84,7 @@ proc readMessages(server: AsyncFD) {.async.} = 16384, cast[ptr SockAddr](addr(saddr)), addr(slen)) size = 0 - var grammString = $buffer + var grammString = $buffer.cstring if grammString.startswith("Message ") and saddr.sin_addr.s_addr == 0x100007F: await sendTo(server, addr grammString[0], len(grammString), diff --git a/tests/misc/treadx.nim b/tests/misc/treadx.nim index 49b6ad691..2e3904cd1 100644 --- a/tests/misc/treadx.nim +++ b/tests/misc/treadx.nim @@ -6,9 +6,8 @@ when not defined(windows): var buf: array[0..10, char] while true: var r = read(0, addr(buf), sizeof(buf)-1) - add inp, $buf + add inp, $buf.cstring if r != sizeof(buf)-1: break echo inp #dafkladskölklödsaf ölksdakölfölksfklwe4iojr389wr 89uweokf sdlkf jweklr jweflksdj fioewjfsdlfsd - diff --git a/tests/system/toString.nim b/tests/system/toString.nim index f9697ec75..cd9a49272 100644 --- a/tests/system/toString.nim +++ b/tests/system/toString.nim @@ -13,9 +13,6 @@ inf nan nil nil -(a: 0, b: nil) -nil -ptr (a: 0, b: nil)''' """ echo($(@[23, 45])) @@ -49,12 +46,8 @@ type b: string var foo1: Foo -var foo2: ref Foo -var foo3: ptr Foo = foo1.addr -echo foo1 -echo foo2 -echo foo3 +doAssert $foo1 == "(a: 0, b: nil)" const data = @['a','b', '\0', 'c','d'] @@ -67,5 +60,6 @@ import strutils # array test 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 newString(arr) == "Hello World!" +doAssert $arr.cstring == "Hello World!" |