-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwc.go
222 lines (203 loc) · 5.26 KB
/
wc.go
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
bytesp "bytes"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"strings"
"unicode/utf8"
)
var (
line = flag.Bool("l", false, "The number of lines in each input file is written to the standard output.")
words = flag.Bool("w", false, "The number of words in each input file is written to the standard output.")
bytes = flag.Bool("c", false, "The number of bytes in each input file is written to the standard output. This will cancel out any prior usage of the -m option.")
characters = flag.Bool("m", false, "The number of characters in each input file is written to the standard output. If the current locale does not support multibyte characters, this is equivalent to the -c option. This will cancel out any prior usage of the -c option.")
)
var wcMap map[int]*fileStat
type fileStat struct {
Name string
LineCount int64
WordCount int64
ByteCount int64
CharacterCount int64
Error error
}
func calculateOptions() int {
totalOptions := 1
if *line {
totalOptions++
}
if *words {
totalOptions++
}
if *bytes {
totalOptions++
}
if *characters {
totalOptions++
}
if totalOptions == 1 {
*line, *words, *bytes = true, true, true
}
return totalOptions
}
func setReader(options []string) [][]byte {
var fileBytes [][]byte
if len(options) == 0 {
stdinBytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
f := &fileStat{Name: "", Error: errors.New("wc: " + err.Error())}
wcMap[0] = f
}
if wcMap[0] == nil {
wcMap[0] = &fileStat{}
}
fileBytes = append(fileBytes, stdinBytes)
}
for i, v := range options {
fileByte, err := ioutil.ReadFile(v)
if err != nil {
f := &fileStat{Name: v, Error: errors.New("wc: " + err.Error())}
wcMap[i] = f
} else {
wcMap[i] = &fileStat{Name: v}
}
fileBytes = append(fileBytes, fileByte)
}
return fileBytes
}
func countByNewLineDelim(i int, b []byte) {
var count int
buff := bytesp.NewBuffer(b)
for {
_, err := buff.ReadBytes(byte('\n'))
switch {
case err == io.EOF:
wcMap[i].LineCount = int64(count)
return
case err != nil:
f := &fileStat{Name: "", Error: err}
wcMap[i] = f
return
}
count++
}
}
func countLines(wcBytes [][]byte) {
for i, wcByte := range wcBytes {
countByNewLineDelim(i, wcByte)
}
}
func countBytes(wcBytes [][]byte) {
for i, wcByte := range wcBytes {
wcMap[i].ByteCount = int64(len(wcByte))
}
}
func countWords(wcBytes [][]byte) {
for i, wcByte := range wcBytes {
wcMap[i].WordCount = int64(len(strings.Fields(string(wcByte))))
}
}
func countCharacters(wcBytes [][]byte) {
for i, wcByte := range wcBytes {
wcMap[i].CharacterCount = int64(utf8.RuneCount(wcByte))
}
}
func calculateMetrics() (lineSum, wordSum, characterSum, byteSum int64) {
var keys []int
for k := range wcMap {
keys = append(keys, k)
}
sort.Ints(keys)
for _, keyIndex := range keys {
if wcMap[keyIndex].Error != nil {
io.Copy(os.Stdout, strings.NewReader(fmt.Sprintf("%s\n", wcMap[keyIndex].Error.Error())))
} else {
var message string
if *line {
message = fmt.Sprintf("\t%d", wcMap[keyIndex].LineCount)
lineSum = lineSum + wcMap[keyIndex].LineCount
}
if *words {
if message != "" {
message = fmt.Sprintf("\t%s\t%d", message, wcMap[keyIndex].WordCount)
} else {
message = fmt.Sprintf("\t%d", wcMap[keyIndex].WordCount)
}
wordSum = wordSum + wcMap[keyIndex].WordCount
}
if *bytes {
if message != "" {
message = fmt.Sprintf("\t%s\t%d", message, wcMap[keyIndex].ByteCount)
} else {
message = fmt.Sprintf("\t%d", wcMap[keyIndex].ByteCount)
}
byteSum = byteSum + wcMap[keyIndex].ByteCount
}
if *characters {
if message != "" {
message = fmt.Sprintf("\t%s\t%d", message, wcMap[keyIndex].CharacterCount)
} else {
message = fmt.Sprintf("\t%d", wcMap[keyIndex].CharacterCount)
}
characterSum = characterSum + wcMap[keyIndex].CharacterCount
}
io.Copy(os.Stdout, strings.NewReader(fmt.Sprintf("%s\t%s\n", message, wcMap[keyIndex].Name)))
}
}
return lineSum, wordSum, characterSum, byteSum
}
func printAggregateMetrics(lineSum, wordSum, characterSum, byteSum int64) {
var totalMessage string
if *line {
totalMessage = fmt.Sprintf("\t%d", lineSum)
}
if *words {
if totalMessage != "" {
totalMessage = fmt.Sprintf("\t%s\t%d", totalMessage, wordSum)
} else {
totalMessage = fmt.Sprintf("\t%d", wordSum)
}
}
if *bytes {
if totalMessage != "" {
totalMessage = fmt.Sprintf("\t%s\t%d", totalMessage, byteSum)
} else {
totalMessage = fmt.Sprintf("\t%d", byteSum)
}
}
if *characters {
if totalMessage != "" {
totalMessage = fmt.Sprintf("\t%s\t%d", totalMessage, characterSum)
} else {
totalMessage = fmt.Sprintf("\t%d", characterSum)
}
}
io.Copy(os.Stdout, strings.NewReader(fmt.Sprintf("%s\ttotal\n", totalMessage)))
}
func main() {
flag.Parse()
wcMap = make(map[int]*fileStat)
totalOptions := calculateOptions()
wcBytes := setReader(os.Args[totalOptions:])
if *line {
countLines(wcBytes)
}
if *words {
countWords(wcBytes)
}
if *bytes {
countBytes(wcBytes)
}
if *characters {
countCharacters(wcBytes)
}
lineSum, wordSum, characterSum, byteSum := calculateMetrics()
if len(os.Args[totalOptions:]) > 1 {
printAggregateMetrics(lineSum, wordSum, characterSum, byteSum)
}
}