root/trunk/docsrc/wc.dd

Revision 1244, 2.2 kB (checked in by walter, 3 years ago)

fix errors

  • Property svn:eol-style set to native
Line 
1 Ddoc
2
3 $(D_S Example: wc,
4
5     $(P This program is the D version of the classic wc (wordcount) C
6     program.
7     It serves to demonstrate how to read files, slice arrays,
8     and simple symbol table management with associative arrays.
9     It has two main code paths, one for smaller files that will fit
10     in memory, and one using buffered file I/O for files that are larger.
11     )
12
13 ------------
14 import std.stdio;
15 import std.stream;
16
17 int main (string[] args)
18 {
19     int w_total;
20     int l_total;
21     ulong c_total;
22     int[string] dictionary;
23
24     writefln("   lines   words   bytes file");
25     foreach (arg; args[1 .. args.length])
26     {
27     int w_cnt, l_cnt;
28     bool inword;
29
30     auto c_cnt = std.file.getSize(arg);
31     if (c_cnt < 10_000_000)
32     {
33         size_t wstart;
34         auto input = cast(string)std.file.read(arg);
35
36         foreach (j, c; input)
37         {
38         if (c == '\n')
39             ++l_cnt;
40         if (c >= '0' && c <= '9')
41         {
42         }
43         else if (c >= 'a' && c <= 'z' ||
44              c >= 'A' && c <= 'Z')
45         {
46             if (!inword)
47             {
48             wstart = j;
49             inword = true;
50             ++w_cnt;
51             }
52         }
53         else if (inword)
54         {   auto word = input[wstart .. j];
55
56             dictionary[word]++;
57             inword = false;
58         }
59         }
60         if (inword)
61         {   auto w = input[wstart .. input.length];
62         dictionary[w]++;
63         }
64     }
65     else
66     {
67         auto f = new BufferedFile(arg);
68         string buf;
69
70         while (!f.eof())
71         {   char c;
72
73         f.read(c);
74         if (c == '\n')
75             ++l_cnt;
76         if (c >= '0' && c <= '9')
77         {
78             if (inword)
79             buf ~= c;
80         }
81         else if (c >= 'a' && c <= 'z' ||
82                  c >= 'A' && c <= 'Z')
83         {
84             if (!inword)
85             {
86             buf.length = 0;
87             buf ~= c;
88             inword = 1;
89             ++w_cnt;
90             }
91             else
92             buf ~= c;
93         }
94         else if (inword)
95         {
96             if (++dictionary[buf] == 1)
97             buf = null;
98             inword = 0;
99         }
100         }
101         if (inword)
102         {
103         dictionary[buf]++;
104         }
105     }
106     writefln("%8s%8s%8s %s\n", l_cnt, w_cnt, c_cnt, arg);
107     l_total += l_cnt;
108     w_total += w_cnt;
109     c_total += c_cnt;
110     }
111
112     if (args.length > 2)
113     {
114     writefln("--------------------------------------\n%8s%8s%8s total",
115         l_total, w_total, c_total);
116     }
117
118     writefln("--------------------------------------");
119
120     foreach (word1; dictionary.keys.sort)
121     {
122     writefln("%3s %s", dictionary[word1], word1);
123     }
124     return 0;
125 }
126 -------------
127 )
128
129 Macros:
130     TITLE=Word Count
131     WIKI=WC
Note: See TracBrowser for help on using the browser.