From 25e64d346826ca3378a16a8bbbdcbc458b4809b1 Mon Sep 17 00:00:00 2001 From: Andrei Alexandrescu Date: Thu, 4 Feb 2016 10:28:12 -0500 Subject: [PATCH 1/2] Add print --- std/stdio.d | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/std/stdio.d b/std/stdio.d index f406882000c..a7c5b3683a1 100644 --- a/std/stdio.d +++ b/std/stdio.d @@ -1368,6 +1368,49 @@ Throws: $(D Exception) if the file is not opened. w.put('\n'); } + /** + + Writes its arguments in text format to the file separated pairwise by a + space (or custom `separator`), followed by a newline (or custom `eol`). + + Throws: $(D Exception) if the file is not opened. + $(D ErrnoException) on an error writing to the file. + */ + void print(string separator = " ", string eol = "\n", S...)(S args) + { + import std.conv : to; + enum string s = () + { + string result = "write("; + foreach (i, _; S) + { + if (i > 0) + { + if (separator.length == 0) result ~= ", "; + else if (separator.length == 1) result ~= ", separator[0], "; + else result ~= ", separator, "; + } + result ~= "args[" ~ to!string(i) ~ "]"; + } + if (eol.length == 0) result ~= ");"; + else if (eol.length == 1) result ~= ", eol[0]);"; + else result ~= ", eol);"; + return result; + }(); + // to debug: pragma(msg, s); + mixin(s); + } + + /// + unittest + { + void cubes(uint limit) + { + foreach (i; 0 .. limit) + stdout.print(i, i * i * i); + } + } + /** Read line from the file handle and return it as a specified type. @@ -3314,6 +3357,23 @@ unittest // assert(read == "Hello, nice world number 42!\n1\n1\n1\n", "["~read~"]"); } +/** +Forwards to `stdout.print`. +*/ +void print(string separator = " ", string eol = "\n", S...)(S args) +{ + trustedStdout.print!(separator, eol)(args); +} + +/// +unittest +{ + void printCSVRow(Data...)(Data data) + { + print!", "(data); + } +} + /** * Read data from $(D stdin) according to the specified * $(LINK2 std_format.html#format-string, format specifier) using From 2d57cfdfcee6d3d8fc75afe091e347a956d1e50e Mon Sep 17 00:00:00 2001 From: Andrei Alexandrescu Date: Fri, 12 Feb 2016 10:07:39 -0500 Subject: [PATCH 2/2] Simplify implementation --- std/stdio.d | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/std/stdio.d b/std/stdio.d index a7c5b3683a1..77978668381 100644 --- a/std/stdio.d +++ b/std/stdio.d @@ -1376,29 +1376,12 @@ Throws: $(D Exception) if the file is not opened. Throws: $(D Exception) if the file is not opened. $(D ErrnoException) on an error writing to the file. */ - void print(string separator = " ", string eol = "\n", S...)(S args) + void print(string separator = " ", string eol = "\n", S...)(auto ref S args) { - import std.conv : to; - enum string s = () - { - string result = "write("; - foreach (i, _; S) - { - if (i > 0) - { - if (separator.length == 0) result ~= ", "; - else if (separator.length == 1) result ~= ", separator[0], "; - else result ~= ", separator, "; - } - result ~= "args[" ~ to!string(i) ~ "]"; - } - if (eol.length == 0) result ~= ");"; - else if (eol.length == 1) result ~= ", eol[0]);"; - else result ~= ", eol);"; - return result; - }(); - // to debug: pragma(msg, s); - mixin(s); + import std.range : repeat; + import std.string : join; + static immutable fmt = repeat("%s", S.length).join(separator) ~ eol; + writefln(fmt, args); } ///