-
-
Save apg/6a15545ee7d1d1539dcdb3a10e64f120 to your computer and use it in GitHub Desktop.
Simple wc(1) in D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $ 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