thornik

Bench 'Word count' for D

Dec 5th, 2017
598
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×