diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2017-10-10 13:18:32 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2017-10-10 13:18:32 +0200 |
commit | 13f974336eff453fe4384ef2566f069b6fde75a1 (patch) | |
tree | b0849d565dafa26ae4d062e4bd18981c00714241 /compiler/rodutils.nim | |
parent | 8bd9c7a4e6cb789cd0e37d30c4c59080744cea39 (diff) | |
parent | 91981c07bd9335dbd32afb636bd1437a588f39eb (diff) | |
download | Nim-13f974336eff453fe4384ef2566f069b6fde75a1.tar.gz |
Merge branch 'araq-stringify-array' into devel
Diffstat (limited to 'compiler/rodutils.nim')
-rw-r--r-- | compiler/rodutils.nim | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/compiler/rodutils.nim b/compiler/rodutils.nim index 77f7c844f..0456e9349 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: "<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 = if f != f: @@ -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] - c_sprintf(buf, "%#.16e" & literalPostfix, f) - result = $buf + 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): @@ -133,4 +138,3 @@ iterator decodeStrArray*(s: cstring): string = while s[i] != '\0': yield decodeStr(s, i) if s[i] == ' ': inc i - |