Skip to content

Commit

Permalink
Merge pull request #2806 from WalterBright/stdin-ex
Browse files Browse the repository at this point in the history
add example for std.stdio.stdin
  • Loading branch information
dnadlinger committed Dec 21, 2014
2 parents 3c914b2 + 5156308 commit f9c5ff3
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions std/stdio.d
Expand Up @@ -2096,23 +2096,23 @@ $(XREF file,readText)
@property nothrow
ubyte[] front()
{
version(assert)
version(assert)
{
import core.exception : RangeError;
if (empty)
throw new RangeError();
if (empty)
throw new RangeError();
}
return chunk_;
}

/// Ditto
void popFront()
{
version(assert)
version(assert)
{
import core.exception : RangeError;
if (empty)
throw new RangeError();
if (empty)
throw new RangeError();
}
prime();
}
Expand Down Expand Up @@ -2666,11 +2666,11 @@ struct LockingTextReader

@property dchar front()
{
version(assert)
version(assert)
{
import core.exception : RangeError;
if (empty)
throw new RangeError();
if (empty)
throw new RangeError();
}
return _front;
}
Expand Down Expand Up @@ -2728,11 +2728,11 @@ struct LockingTextReader

void popFront()
{
version(assert)
version(assert)
{
import core.exception : RangeError;
if (empty)
throw new RangeError();
if (empty)
throw new RangeError();
}

// Pop the current front.
Expand Down Expand Up @@ -3699,7 +3699,7 @@ class StdioException : Exception
uint errno;

/**
Initialize with a message and an error code.
Initialize with a message and an error code.
*/
this(string message, uint e = .errno)
{
Expand Down Expand Up @@ -3764,7 +3764,26 @@ extern(C) void std_stdio_static_this()
//---------
__gshared
{
File stdin; /// The standard input stream.
/** The standard input stream.
*/
File stdin;
///
unittest
{
// Read stdin, sort lines, write to stdout
import std.stdio, std.array, std.algorithm : sort, copy, map;

void main() {
stdin // read from stdin
.byLine(KeepTerminator.yes) // a line at a time
.map!(a => a.idup) // byLine() reuses its buffer, so copy the lines
.array // convert to array of lines
.sort() // sort the lines
.copy( // copy output of .sort to an OutputRange
stdout.lockingTextWriter()); // the OutputRange
}
}

File stdout; /// The standard output stream.
File stderr; /// The standard error stream.
}
Expand Down Expand Up @@ -3799,7 +3818,7 @@ private size_t readlnImpl(FILE* fps, ref char[] buf, dchar terminator = '\n')
{
import core.memory;
import std.array : appender, uninitializedArray;

FLOCK(fps);
scope(exit) FUNLOCK(fps);

Expand Down

0 comments on commit f9c5ff3

Please sign in to comment.