1 // RUNNABLE_PHOBOS_TEST
2 // PERMUTE_ARGS:
3 // EXECUTE_ARGS: runnable/extra-files/alice30.txt
4 // EXTRA_FILES: extra-files/alice30.txt
5 
6 import std.stdio;
7 import std.file;
8 
main(string[]args)9 int main (string[] args)
10 {
11     int w_total;
12     int l_total;
13     int c_total;
14     int[string] dictionary;
15 
16     writefln("   lines   words   bytes file");
17     foreach (arg; args[1 .. args.length])
18     {
19         int w_cnt, l_cnt, c_cnt;
20         size_t wstart;
21         bool inword;
22 
23         auto input = cast(string)std.file.read(arg);
24 
25         foreach (j, c; input)
26         {
27             if (c == '\n')
28                 ++l_cnt;
29             if (c >= '0' && c <= '9')
30             {
31             }
32             else if (c >= 'a' && c <= 'z' ||
33                      c >= 'A' && c <= 'Z')
34             {
35                 if (!inword)
36                 {
37                     wstart = j;
38                     inword = true;
39                     ++w_cnt;
40                 }
41             }
42             else if (inword)
43             {   auto word = input[wstart .. j];
44 
45                 dictionary[word]++;
46                 inword = false;
47             }
48             ++c_cnt;
49         }
50         if (inword)
51         {   auto w = input[wstart .. input.length];
52             dictionary[w]++;
53         }
54         writefln("%8s%8s%8s %s", l_cnt, w_cnt, c_cnt, arg);
55         l_total += l_cnt;
56         w_total += w_cnt;
57         c_total += c_cnt;
58     }
59 
60     if (args.length > 2)
61     {
62         writefln("--------------------------------------\n%8s%8s%8s total",
63             l_total, w_total, c_total);
64     }
65 
66     writefln("--------------------------------------");
67 
68     foreach (word1; dictionary.keys)
69     {
70         writefln("%3s %s", dictionary[word1], word1);
71     }
72     return 0;
73 }
74