1 // RUNNABLE_PHOBOS_TEST
2 // PERMUTE_ARGS:
3 // EXECUTE_ARGS: runnable/wc2.d
4 
5 import std.file;
6 
7 extern(C) int printf(const char*, ...);
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     printf("   lines   words   bytes file\n");
17     foreach (string arg; args[1 .. args.length])
18     {
19         string input;
20         int w_cnt, l_cnt, c_cnt;
21         int inword;
22         int wstart;
23 
24         input = cast(string)std.file.read(arg);
25 
26         for (int j = 0; j < input.length; j++)
27         {   char c;
28 
29             c = input[j];
30             if (c == '\n')
31                 ++l_cnt;
32             if (c >= '0' && c <= '9')
33             {
34             }
35             else if (c >= 'a' && c <= 'z' ||
36                 c >= 'A' && c <= 'Z')
37             {
38                 if (!inword)
39                 {
40                     wstart = j;
41                     inword = 1;
42                     ++w_cnt;
43                 }
44             }
45             else if (inword)
46             {   string word = input[wstart .. j];
47 
48                 dictionary[word]++;
49                 inword = 0;
50             }
51             ++c_cnt;
52         }
53         if (inword)
54         {   string w = input[wstart .. input.length];
55             dictionary[w]++;
56         }
57         printf("%8lu%8lu%8lu %.*s\n", l_cnt, w_cnt, c_cnt, arg.length, arg.ptr);
58         l_total += l_cnt;
59         w_total += w_cnt;
60         c_total += c_cnt;
61     }
62 
63     if (args.length > 2)
64     {
65         printf("--------------------------------------\n%8lu%8lu%8lu total",
66             l_total, w_total, c_total);
67     }
68 
69     printf("--------------------------------------\n");
70     foreach (string word1; dictionary.keys)
71     {
72         printf("%3d %.*s\n", dictionary[word1], word1.length, word1.ptr);
73     }
74     return 0;
75 }
76