-
-
Notifications
You must be signed in to change notification settings - Fork 754
Add print #3971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add print #3971
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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`). | ||
|
|
||
| Throws: $(D Exception) if the file is not opened. | ||
| $(D ErrnoException) on an error writing to the file. | ||
| */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is over engineering. A static foreach loop for |
||
| writefln(fmt, args); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| /// | ||
| unittest | ||
| { | ||
| void cubes(uint limit) | ||
| { | ||
| foreach (i; 0 .. limit) | ||
| stdout.print(i, i * i * i); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the expected result is unclear
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Params:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example(s):There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.