Merge dmd upstream e2fe2687b
[official-gcc.git] / gcc / testsuite / gdc.test / runnable / wc2.d
blob6229627843f950e99bf54e1afc5a73f52d77acaf
1 // PERMUTE_ARGS:
2 // EXECUTE_ARGS: runnable/wc2.d
4 import std.file;
6 extern(C) int printf(const char*, ...);
8 int main (string[] args)
10 int w_total;
11 int l_total;
12 int c_total;
13 int[string] dictionary;
15 printf(" lines words bytes file\n");
16 foreach (string arg; args[1 .. args.length])
18 string input;
19 int w_cnt, l_cnt, c_cnt;
20 int inword;
21 int wstart;
23 input = cast(string)std.file.read(arg);
25 for (int j = 0; j < input.length; j++)
26 { char c;
28 c = input[j];
29 if (c == '\n')
30 ++l_cnt;
31 if (c >= '0' && c <= '9')
34 else if (c >= 'a' && c <= 'z' ||
35 c >= 'A' && c <= 'Z')
37 if (!inword)
39 wstart = j;
40 inword = 1;
41 ++w_cnt;
44 else if (inword)
45 { string word = input[wstart .. j];
47 dictionary[word]++;
48 inword = 0;
50 ++c_cnt;
52 if (inword)
53 { string w = input[wstart .. input.length];
54 dictionary[w]++;
56 printf("%8lu%8lu%8lu %.*s\n", l_cnt, w_cnt, c_cnt, arg.length, arg.ptr);
57 l_total += l_cnt;
58 w_total += w_cnt;
59 c_total += c_cnt;
62 if (args.length > 2)
64 printf("--------------------------------------\n%8lu%8lu%8lu total",
65 l_total, w_total, c_total);
68 printf("--------------------------------------\n");
69 foreach (string word1; dictionary.keys)
71 printf("%3d %.*s\n", dictionary[word1], word1.length, word1.ptr);
73 return 0;