Skip to content

Instantly share code, notes, and snippets.

@apg

apg/wc.d

Created Jun 1, 2018
Embed
What would you like to do?
Simple wc(1) in D
// $ dmd wc.d
// $ cat /usr/share/dict/words | ./wc
// 235886 235886 2493109
import std.stdio;
void main()
{
int lines = 0;
int words = 0;
int chars = 0;
bool wasSpace = true;
auto buffer = new char[4096];
while (!stdin.eof()) {
auto inp = stdin.rawRead(buffer);
foreach (i; 0 .. inp.length) {
switch (inp[i]) {
case '\t':
case ' ':
wasSpace = true;
chars++;
break;
case '\r':
case '\n':
wasSpace = true;
lines++;
chars++;
break;
default:
chars++;
if (wasSpace) {
words++;
}
wasSpace = false;
}
}
}
writeln(lines, "\t", words, "\t", chars);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment