-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc2.d
117 lines (105 loc) · 2.83 KB
/
wc2.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import std.stdio;
import std.file;
import std.algorithm.sorting;
void main (string[] args)
{
int w_total;
int l_total;
ulong c_total;
size_t[string] dictionary;
writefln(" lines words bytes file");
foreach (arg; args[1 .. $])
{
int w_cnt, l_cnt;
bool inword;
auto c_cnt = std.file.getSize(arg);
if (c_cnt < 10_000_000)
{
size_t wstart;
auto input = readText(arg);
foreach (j, c; input)
{
if (c == '\n')
++l_cnt;
if (c >= '0' && c <= '9')
{
}
else if (c >= 'a' && c <= 'z' ||
c >= 'A' && c <= 'Z')
{
if (!inword)
{
wstart = j;
inword = true;
++w_cnt;
}
}
else if (inword)
{
auto word = input[wstart .. j];
dictionary[word]++;
inword = false;
}
}
if (inword)
{
auto w = input[wstart .. $];
dictionary[w]++;
}
}
else
{
auto f = std.stdio.File(arg);
string buf;
while (!f.eof())
{
char c;
f.readf("%c", &c);
if (c == '\n')
++l_cnt;
if (c >= '0' && c <= '9')
{
if (inword)
buf ~= c;
}
else if (c >= 'a' && c <= 'z' ||
c >= 'A' && c <= 'Z')
{
if (!inword)
{
buf.length = 0;
buf ~= c;
inword = 1;
++w_cnt;
}
else
buf ~= c;
}
else if (inword)
{
if (++dictionary[buf] == 1)
buf = null;
inword = 0;
}
}
if (inword)
{
dictionary[buf]++;
}
}
writefln("%8s%8s%8s %s\n", l_cnt, w_cnt, c_cnt, arg);
l_total += l_cnt;
w_total += w_cnt;
c_total += c_cnt;
}
if (args.length > 2)
{
writefln("--------------------------------------\n%8s%8s%8s total",
l_total, w_total, c_total);
}
writefln("--------------------------------------");
foreach (word1; dictionary.keys.sort())
{
writefln("%3s %s", dictionary[word1], word1);
}
}