From 1f7fc7f279a4a1c5f4854f7c7a39a58bb2cca805 Mon Sep 17 00:00:00 2001 From: Arne Döring Date: Mon, 29 May 2017 17:53:20 +0200 Subject: arrays can now be printed --- compiler/rodutils.nim | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'compiler/rodutils.nim') diff --git a/compiler/rodutils.nim b/compiler/rodutils.nim index 77f7c844f..03d5c95eb 100644 --- a/compiler/rodutils.nim +++ b/compiler/rodutils.nim @@ -23,7 +23,7 @@ proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string = else: var buf: array[0..80, char] c_sprintf(buf, "%#.16e" & literalPostfix, f) - result = $buf + result = newString(buf) proc encodeStr*(s: string, result: var string) = for i in countup(0, len(s) - 1): @@ -133,4 +133,3 @@ iterator decodeStrArray*(s: cstring): string = while s[i] != '\0': yield decodeStr(s, i) if s[i] == ' ': inc i - -- cgit 1.4.1-2-gfad0 From 0852be2dec72406c8d9c93e094a8907b52d1aa5a Mon Sep 17 00:00:00 2001 From: Arne Döring Date: Wed, 31 May 2017 14:05:45 +0200 Subject: remove $ for ptr/ref, prefer using string over array of char --- compiler/rodutils.nim | 8 ++++---- lib/system.nim | 30 ++++++++++++------------------ lib/system/repr.nim | 14 +++++++------- 3 files changed, 23 insertions(+), 29 deletions(-) (limited to 'compiler/rodutils.nim') diff --git a/compiler/rodutils.nim b/compiler/rodutils.nim index 03d5c95eb..1352afadb 100644 --- a/compiler/rodutils.nim +++ b/compiler/rodutils.nim @@ -10,7 +10,7 @@ ## Serialization utilities for the compiler. import strutils -proc c_sprintf(buf, frmt: cstring) {.importc: "sprintf", header: "", nodecl, varargs.} +proc c_snprintf(s: cstring; n:uint; frmt: cstring): cint {.importc: "snprintf", header: "", nodecl, varargs.} proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string = if f != f: @@ -21,9 +21,9 @@ proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string = if f > 0.0: result = "INF" else: result = "-INF" else: - var buf: array[0..80, char] - c_sprintf(buf, "%#.16e" & literalPostfix, f) - result = newString(buf) + result = newString(80) + let newLen = c_snprintf(result[0].addr, 81, "%#.16e" & literalPostfix, f) + result.setLen(newLen) proc encodeStr*(s: string, result: var string) = for i in countup(0, len(s) - 1): diff --git a/lib/system.nim b/lib/system.nim index 3dc892c44..b5008129d 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -1872,26 +1872,20 @@ 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 `$`*[T: ref](arg: T): string = - ## The stringify operator to handle the nil value of all ref types. - ## Then it forwards to stringify operator of the underlying (value) type. - if arg.isNil: - "nil" - else: - "ref " & $arg[] - -proc `$`*[T: ptr](arg: T): string = - ## The stringify operator to handle the nil value of all ptr types. - ## Then it forwards to stringify operator of the underlying (value) type. - if arg.isNil: - "nil" - else: - "ptr " & $arg[] - -proc newString*[N](data: array[N, char]): string = +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. - $(cast[cstring](data[0].unsafeAddr)) + 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.} diff --git a/lib/system/repr.nim b/lib/system/repr.nim index fcf65dd0e..2775b1b3e 100644 --- a/lib/system/repr.nim +++ b/lib/system/repr.nim @@ -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 newString(buf) + result = newString(60) + let newLen = c_sprintf(result[0].addr, "%p", x) + result.setLen newLen 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 = newString(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: -- cgit 1.4.1-2-gfad0 From ddea990a70606aaa19513510ae4b47ada7e49fe9 Mon Sep 17 00:00:00 2001 From: Arne Döring Date: Tue, 6 Jun 2017 21:19:50 +0200 Subject: removed newString proc again, reverted some unnecesary changes --- compiler/rodutils.nim | 6 +++--- lib/pure/nativesockets.nim | 4 ++-- lib/pure/os.nim | 2 +- lib/system.nim | 15 --------------- lib/system/repr.nim | 6 +++--- tests/async/tnewasyncudp.nim | 6 +++--- tests/misc/treadx.nim | 3 +-- tests/system/toString.nim | 12 +++--------- 8 files changed, 16 insertions(+), 38 deletions(-) (limited to 'compiler/rodutils.nim') 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!" -- cgit 1.4.1-2-gfad0 From 54808ab12fcbf8cc253129ed03f560fc6dd2648e Mon Sep 17 00:00:00 2001 From: Arne Döring Date: Mon, 7 Aug 2017 18:21:21 +0200 Subject: don't filter '\0' characters in string generation --- compiler/rodutils.nim | 2 +- lib/system.nim | 7 +------ tests/system/toString.nim | 3 ++- 3 files changed, 4 insertions(+), 8 deletions(-) (limited to 'compiler/rodutils.nim') diff --git a/compiler/rodutils.nim b/compiler/rodutils.nim index 3a90a207c..6e77e6b8f 100644 --- a/compiler/rodutils.nim +++ b/compiler/rodutils.nim @@ -22,7 +22,7 @@ proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string = else: result = "-INF" else: var buf: array[0..80, char] - let newLen = c_snprintf(buf.cstring, buf.len.uint, "%#.16e%s", f, literalPostfix.cstring) + discard c_snprintf(buf.cstring, buf.len.uint, "%#.16e%s", f, literalPostfix.cstring) result = $buf.cstring proc encodeStr*(s: string, result: var string) = diff --git a/lib/system.nim b/lib/system.nim index 0bc0a0dbf..d2bdeae0e 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2427,12 +2427,7 @@ proc collectionToString[T](x: T, prefix, separator, suffix: string): string = result.add($value) # prevent temporary string allocation elif compiles(result.add(value)): - # don't insert '\0' characters into the result string - when value is char: - if value != '\0': - result.add(value) - else: - result.add(value) + result.add(value) else: result.add($value) diff --git a/tests/system/toString.nim b/tests/system/toString.nim index 0eed596ae..377336c90 100644 --- a/tests/system/toString.nim +++ b/tests/system/toString.nim @@ -46,6 +46,7 @@ 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, !, ]" +doAssert $arr == "[H, e, l, l, o, , W, o, r, l, d, !, \0]" doAssert $arr.cstring == "Hello World!" -- cgit 1.4.1-2-gfad0 From 3308d265816b342a842e7cabef492a54ea722e74 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Tue, 10 Oct 2017 00:17:20 +0200 Subject: breaking change: arrays of char do not convert to cstring; ptr to array of char does --- changelog.md | 3 +++ compiler/condsyms.nim | 1 + compiler/rodutils.nim | 11 ++++++++--- compiler/sigmatch.nim | 13 +++++++------ compiler/types.nim | 8 -------- lib/pure/nativesockets.nim | 14 ++++++++------ lib/pure/os.nim | 5 ++++- lib/pure/strutils.nim | 10 ++++++++-- lib/system/dyncalls.nim | 5 ++++- lib/system/excpt.nim | 8 ++++++-- lib/system/repr.nim | 11 ++++++++--- lib/system/sysstr.nim | 20 ++++++++++++++++---- 12 files changed, 73 insertions(+), 36 deletions(-) (limited to 'compiler/rodutils.nim') diff --git a/changelog.md b/changelog.md index 9f6a83a37..22d763f53 100644 --- a/changelog.md +++ b/changelog.md @@ -5,3 +5,6 @@ - Removed basic2d/basic3d out of the stdlib and into Nimble packages. These packages deprecated however, use the ``glm``, ``arraymancer``, ``neo`` or another package. +- Arrays of char cannot be converted to ``cstring`` anymore, pointers to + arrays of char can! This means ``$`` for arrays can finally exist + in ``system.nim`` and do the right thing. diff --git a/compiler/condsyms.nim b/compiler/condsyms.nim index 9863e90bb..02c31163a 100644 --- a/compiler/condsyms.nim +++ b/compiler/condsyms.nim @@ -108,3 +108,4 @@ proc initDefines*() = defineSymbol("nimHasCppDefine") defineSymbol("nimGenericInOutFlags") when false: defineSymbol("nimHasOpt") + defineSymbol("nimNoArrayToCstringConversion") diff --git a/compiler/rodutils.nim b/compiler/rodutils.nim index 6e77e6b8f..0456e9349 100644 --- a/compiler/rodutils.nim +++ b/compiler/rodutils.nim @@ -21,9 +21,14 @@ proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string = if f > 0.0: result = "INF" else: result = "-INF" else: - var buf: array[0..80, char] - discard c_snprintf(buf.cstring, buf.len.uint, "%#.16e%s", f, literalPostfix.cstring) - result = $buf.cstring + when defined(nimNoArrayToCstringConversion): + result = newString(81) + let n = c_snprintf(result.cstring, result.len.uint, "%#.16e%s", f, literalPostfix.cstring) + setLen(result, n) + else: + var buf: array[0..80, char] + discard 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/compiler/sigmatch.nim b/compiler/sigmatch.nim index f2bc24399..50d4178b6 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -1288,12 +1288,13 @@ proc typeRelImpl(c: var TCandidate, f, aOrig: PType, of tyString: result = isConvertible of tyPtr: # ptr[Tag, char] is not convertible to 'cstring' for now: - if a.len == 1 and a.sons[0].kind == tyChar: result = isConvertible - of tyArray: - if (firstOrd(a.sons[0]) == 0) and - (skipTypes(a.sons[0], {tyRange}).kind in {tyInt..tyInt64}) and - (a.sons[1].kind == tyChar): - result = isConvertible + if a.len == 1: + let pointsTo = a.sons[0].skipTypes(abstractInst) + if pointsTo.kind == tyChar: result = isConvertible + elif pointsTo.kind == tyArray and firstOrd(pointsTo.sons[0]) == 0 and + skipTypes(pointsTo.sons[0], {tyRange}).kind in {tyInt..tyInt64} and + pointsTo.sons[1].kind == tyChar: + result = isConvertible else: discard of tyEmpty, tyVoid: diff --git a/compiler/types.nim b/compiler/types.nim index 3dbf6fd18..f32b0da18 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -667,14 +667,6 @@ proc lengthOrd*(t: PType): BiggestInt = else: result = lastOrd(t) - firstOrd(t) + 1 -proc isCompatibleToCString*(a: PType): bool = - if a.kind == tyArray: - if (firstOrd(a.sons[0]) == 0) and - (skipTypes(a.sons[0], {tyRange, tyGenericInst, tyAlias}).kind in - {tyInt..tyInt64, tyUInt..tyUInt64}) and - (a.sons[1].kind == tyChar): - result = true - # -------------- type equality ----------------------------------------------- type diff --git a/lib/pure/nativesockets.nim b/lib/pure/nativesockets.nim index c8fb041d8..6c8701843 100644 --- a/lib/pure/nativesockets.nim +++ b/lib/pure/nativesockets.nim @@ -496,11 +496,12 @@ proc getLocalAddr*(socket: SocketHandle, domain: Domain): (string, Port) = addr(namelen)) == -1'i32: raiseOSError(osLastError()) # Cannot use INET6_ADDRSTRLEN here, because it's a C define. - var buf: array[64, char] + result[0] = newString(64) if inet_ntop(name.sin6_family.cint, - addr name.sin6_addr, buf.cstring, sizeof(buf).int32).isNil: + addr name.sin6_addr, addr result[0][0], (result[0].len+1).int32).isNil: raiseOSError(osLastError()) - result = ($buf.cstring, Port(nativesockets.ntohs(name.sin6_port))) + setLen(result[0], result[0].cstring.len) + result[1] = Port(nativesockets.ntohs(name.sin6_port)) else: raiseOSError(OSErrorCode(-1), "invalid socket family in getLocalAddr") @@ -532,11 +533,12 @@ proc getPeerAddr*(socket: SocketHandle, domain: Domain): (string, Port) = addr(namelen)) == -1'i32: raiseOSError(osLastError()) # Cannot use INET6_ADDRSTRLEN here, because it's a C define. - var buf: array[64, char] + result[0] = newString(64) if inet_ntop(name.sin6_family.cint, - addr name.sin6_addr, buf.cstring, sizeof(buf).int32).isNil: + addr name.sin6_addr, addr result[0][0], (result[0].len+1).int32).isNil: raiseOSError(osLastError()) - result = ($buf.cstring, Port(nativesockets.ntohs(name.sin6_port))) + setLen(result[0], result[0].cstring.len) + result[1] = 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 e6ef96ba3..a1ae4e250 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -790,7 +790,10 @@ 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.cstring + when defined(nimNoArrayToCstringConversion): + var y = $cstring(addr x.d_name) + else: + var y = $x.d_name.cstring if y != "." and y != "..": var s: Stat if not relative: diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 910d09afd..cc0f474f4 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -1890,11 +1890,17 @@ proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault, frmtstr[3] = '*' frmtstr[4] = floatFormatToChar[format] frmtstr[5] = '\0' - L = c_sprintf(buf, frmtstr, precision, f) + when defined(nimNoArrayToCstringConversion): + L = c_sprintf(addr buf, addr frmtstr, precision, f) + else: + L = c_sprintf(buf, frmtstr, precision, f) else: frmtstr[1] = floatFormatToChar[format] frmtstr[2] = '\0' - L = c_sprintf(buf, frmtstr, f) + when defined(nimNoArrayToCstringConversion): + L = c_sprintf(addr buf, addr frmtstr, f) + else: + L = c_sprintf(buf, frmtstr, f) result = newString(L) for i in 0 ..< L: # Depending on the locale either dot or comma is produced, diff --git a/lib/system/dyncalls.nim b/lib/system/dyncalls.nim index 2b86ddf25..c8e251d1e 100644 --- a/lib/system/dyncalls.nim +++ b/lib/system/dyncalls.nim @@ -142,7 +142,10 @@ elif defined(windows) or defined(dos): dec(m) k = k div 10 if k == 0: break - result = getProcAddress(cast[THINSTANCE](lib), decorated) + when defined(nimNoArrayToCstringConversion): + result = getProcAddress(cast[THINSTANCE](lib), addr decorated) + else: + result = getProcAddress(cast[THINSTANCE](lib), decorated) if result != nil: return procAddrError(name) diff --git a/lib/system/excpt.nim b/lib/system/excpt.nim index cee4e33a5..950981227 100644 --- a/lib/system/excpt.nim +++ b/lib/system/excpt.nim @@ -289,8 +289,12 @@ proc raiseExceptionAux(e: ref Exception) = add(buf, " [") xadd(buf, e.name, e.name.len) add(buf, "]\n") - unhandled(buf): - showErrorMessage(buf) + when defined(nimNoArrayToCstringConversion): + template tbuf(): untyped = addr buf + else: + template tbuf(): untyped = buf + unhandled(tbuf()): + showErrorMessage(tbuf()) quitOrDebug() proc raiseException(e: ref Exception, ename: cstring) {.compilerRtl.} = diff --git a/lib/system/repr.nim b/lib/system/repr.nim index 58c86b0db..19fa564fb 100644 --- a/lib/system/repr.nim +++ b/lib/system/repr.nim @@ -16,9 +16,14 @@ 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[60, char] - discard c_sprintf(buf.cstring, "%p", x) - result = $buf.cstring + when defined(nimNoArrayToCstringConversion): + result = newString(60) + let n = c_sprintf(addr result[0], "%p", x) + setLen(result, n) + else: + var buf: array[0..59, char] + discard c_sprintf(buf, "%p", x) + return $buf proc `$`(x: uint64): string = if x == 0: diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim index 90201202c..43b5a0292 100644 --- a/lib/system/sysstr.nim +++ b/lib/system/sysstr.nim @@ -24,7 +24,10 @@ proc cmpStrings(a, b: NimString): int {.inline, compilerProc.} = if a == b: return 0 if a == nil: return -1 if b == nil: return 1 - return c_strcmp(a.data, b.data) + when defined(nimNoArrayToCstringConversion): + return c_strcmp(addr a.data, addr b.data) + else: + return c_strcmp(a.data, b.data) proc eqStrings(a, b: NimString): bool {.inline, compilerProc.} = if a == b: return true @@ -320,7 +323,10 @@ proc nimIntToStr(x: int): string {.compilerRtl.} = proc add*(result: var string; x: float) = var buf: array[0..64, char] - var n: int = c_sprintf(buf, "%.16g", x) + when defined(nimNoArrayToCstringConversion): + var n: int = c_sprintf(addr buf, "%.16g", x) + else: + var n: int = c_sprintf(buf, "%.16g", x) var hasDot = false for i in 0..n-1: if buf[i] == ',': @@ -342,7 +348,10 @@ proc add*(result: var string; x: float) = else: result.add "inf" else: - result.add buf + var i = 0 + while buf[i] != '\0': + result.add buf[i] + inc i proc nimFloatToStr(f: float): string {.compilerproc.} = result = newStringOfCap(8) @@ -507,7 +516,10 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat, t[ti-2] = ('0'.ord + abs_exponent mod 10).char; abs_exponent = abs_exponent div 10 t[ti-3] = ('0'.ord + abs_exponent mod 10).char - number = c_strtod(t, nil) + when defined(nimNoArrayToCstringConversion): + number = c_strtod(addr t, nil) + else: + number = c_strtod(t, nil) proc nimInt64ToStr(x: int64): string {.compilerRtl.} = result = newStringOfCap(sizeof(x)*4) -- cgit 1.4.1-2-gfad0