removed newString proc again, reverted some unnecesary changes

This commit is contained in:
Arne Döring
2017-06-06 21:19:50 +02:00
parent 0852be2dec
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"
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):

View File

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

View File

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

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
## 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.}

View File

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

View File

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

View File

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

View File

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