Advertisement
thornik

Bench 'Word count' for D

Dec 5th, 2017
3,423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.75 KB | None | 0 0
  1. // ldc2.exe -release -m64 -O wc.d
  2.  
  3. import std.stdio;
  4. import std.uni;
  5.  
  6. void main()
  7. {
  8.     int words, lines, chars, state;
  9.    
  10.     foreach(line; stdin.byLine()) {
  11.         lines++;
  12.         state = 0;
  13.         foreach(dchar c; line) {
  14.             final switch(state) {
  15.             case 0: // wait for start of word
  16.                 if (isAlpha(c)) { chars++; state = 1; }
  17.                 break;
  18.             case 1: // we're reading word - wait for end
  19.                 if (isAlpha(c))
  20.                     chars++;
  21.                 else {
  22.                     words++;
  23.                     state = 0;
  24.                 }
  25.             }
  26.         }
  27.         if (state == 1) words++;
  28.     }
  29.     writefln(`Lines=%s words=%s chars=%s`, lines, words, chars);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement