Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions std/stdio.d
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,32 @@ 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`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Params:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example(s):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The examples should be taken care of by the ddoc'd unittest(s).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I always forget that feature when looking at code.


Throws: $(D Exception) if the file is not opened.
$(D ErrnoException) on an error writing to the file.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writeln and print should refer to each other via See_Also, to reduce confusion.

void print(string separator = " ", string eol = "\n", S...)(auto ref S args)
{
import std.range : repeat;
import std.string : join;
static immutable fmt = repeat("%s", S.length).join(separator) ~ eol;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is over engineering. A static foreach loop for args and default FormatSpec!char along with formatValue would be faster to compile and faster to execute, and looks clear.

writefln(fmt, args);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writefln already adds a newline, which is redundant with eol, thus this should use writef.

}

///
unittest
{
void cubes(uint limit)
{
foreach (i; 0 .. limit)
stdout.print(i, i * i * i);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the expected result is unclear

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this should be a non-tested example, since there is no testing going on, and I don't think it makes sense to output anything during an actual unit test.

}
}

/**
Read line from the file handle and return it as a specified type.

Expand Down Expand Up @@ -3314,6 +3340,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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wouldn't actually be legal csv.... but it might be if the print function could also take a per-item transformation function to quote it properly. csv technically shouldn't have a space after the comma either.

The transformation function might just be interesting though, but it might also complicate it more than is worth doing. Some kinda of dg(item.asString) that defaults to identity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Echoing my statement above, the unit test should not print to stdout. The unit tests should either

  1. Replace stdout with a temp file (temporarily)
  2. Use a temp file directly.

You could also have a unit test that just verified the call could compile. Wouldn't make a good example though.

}
}

/**
* Read data from $(D stdin) according to the specified
* $(LINK2 std_format.html#format-string, format specifier) using
Expand Down