diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim
index d971c0bc2..de784acf9 100644
--- a/compiler/ccgexprs.nim
+++ b/compiler/ccgexprs.nim
@@ -953,40 +953,23 @@ proc genEcho(p: BProc, n: PNode) =
# this unusal way of implementing it ensures that e.g. ``echo("hallo", 45)``
# is threadsafe.
internalAssert n.kind == nkBracket
- var args = newSeq[tuple[hasvalue,len,str: string]](0)
+ var args: Rope = nil
var a: TLoc
for i in countup(0, n.len-1):
if n.sons[i].skipConv.kind == nkNilLit:
- args.add((hasvalue:"0", len: "0", str: "\"\""))
+ add(args, ", \"nil\"")
else:
initLocExpr(p, n.sons[i], a)
- let ptrexpr: string = $rdLoc(a)
- let len = format("($1)->Sup.len", ptrexpr)
- let str = format("($1)->data", ptrexpr)
- args.add((hasvalue: ptrexpr, len: len, str: str))
+ addf(args, ", $1? ($1)->data:\"nil\"", [rdLoc(a)])
if platform.targetOS == osGenode:
# bypass libc and print directly to the Genode LOG session
p.module.includeHeader("")
- # separated args
- var rope: Rope
- for arg in args:
- rope.add ", "
- rope.add arg.str
- # TODO this is wrong, it does not properly handle '\0' characters, and locking is not done.
- linefmt(p, cpsStmts, """Genode::log(""$1);$n""", rope)
- for arg in args:
- if arg.hasvalue != "0": # if we know statically that there is no string, it can be skipped
- linefmt(p, cpsStmts, "if($1) Genode::log($2);$n", rope(arg.hasvalue), rope(arg.str))
+ linefmt(p, cpsStmts, """Genode::log(""$1);$n""", args)
else:
p.module.includeHeader("")
- #linefmt(p, cpsStmts, "printf($1$2);$n",
- # makeCString(repeat("%s", n.len) & tnl), args)
- linefmt(p, cpsStmts, "flockfile(stdout);$n")
- for arg in args:
- if arg.hasvalue != "0": # if we know statically that there is no string, it can be skipped
- linefmt(p, cpsStmts, "if($1) fwrite($2, 1, $3, stdout);$n", rope(arg.hasvalue), rope(arg.str), rope(arg.len));
- linefmt(p, cpsStmts, "putchar('\\n'); fflush(stdout);$n")
- linefmt(p, cpsStmts, "funlockfile(stdout);$n")
+ linefmt(p, cpsStmts, "printf($1$2);$n",
+ makeCString(repeat("%s", n.len) & tnl), args)
+ linefmt(p, cpsStmts, "fflush(stdout);$n")
proc gcUsage(n: PNode) =
if gSelectedGC == gcNone: message(n.info, warnGcMem, n.renderTree)
diff --git a/tests/system/toString.nim b/tests/system/toString.nim
index 377336c90..bc8720b55 100644
--- a/tests/system/toString.nim
+++ b/tests/system/toString.nim
@@ -35,7 +35,8 @@ type
var foo1: Foo
-doAssert $foo1 == "(a: 0, b: nil)"
+# nil string should be an some point in time equal to the empty string
+doAssert(($foo1)[0..9] == "(a: 0, b: ")
const
data = @['a','b', '\0', 'c','d']