Skip to content

Instantly share code, notes, and snippets.

@apg
Created June 1, 2018 23:57
Show Gist options
  • Save apg/6a15545ee7d1d1539dcdb3a10e64f120 to your computer and use it in GitHub Desktop.
Save apg/6a15545ee7d1d1539dcdb3a10e64f120 to your computer and use it in GitHub Desktop.
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