-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcount.go
More file actions
167 lines (130 loc) · 2.45 KB
/
count.go
File metadata and controls
167 lines (130 loc) · 2.45 KB
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
package main
import (
"bufio"
"fmt"
"io"
"unicode"
)
type Counts struct {
Bytes int
Words int
Lines int
}
func (c *Counts) Add(other Counts) {
c.Bytes += other.Bytes
c.Words += other.Words
c.Lines += other.Lines
}
func (c Counts) Println(w io.Writer, options DisplayOptions, name string) {
if options.PrintLines() {
fmt.Fprint(w, c.Lines, "\t")
}
if options.PrintWords() {
fmt.Fprint(w, c.Words, "\t")
}
if options.PrintBytes() {
fmt.Fprint(w, c.Bytes, "\t")
}
if options.Filename {
fmt.Fprint(w, name, "\t")
}
fmt.Fprintln(w, "")
}
func CountWords(r io.Reader) int {
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanWords)
wordCount := 0
for scanner.Scan() {
wordCount++
}
return wordCount
}
func CountLines(r io.Reader) int {
linesCount := 0
reader := bufio.NewReader(r)
for {
rune, _, err := reader.ReadRune()
if err != nil {
break
}
if rune == '\n' {
linesCount++
}
}
return linesCount
}
func CountBytes(r io.Reader) int {
bytes, _ := io.Copy(io.Discard, r)
return int(bytes)
}
func GetCounts(r io.Reader) Counts {
bytesReader, bytesWriter := io.Pipe()
wordsReader, wordsWriter := io.Pipe()
linesReader, linesWriter := io.Pipe()
w := io.MultiWriter(bytesWriter, wordsWriter, linesWriter)
chBytes := make(chan int)
chWords := make(chan int)
chLines := make(chan int)
go func() {
defer close(chBytes)
chBytes <- CountBytes(bytesReader)
}()
go func() {
defer close(chWords)
chWords <- CountWords(wordsReader)
}()
go func() {
defer close(chLines)
chLines <- CountLines(linesReader)
}()
io.Copy(w, r)
bytesWriter.Close()
wordsWriter.Close()
linesWriter.Close()
byteCount := <-chBytes
wordsCount := <-chWords
linesCount := <-chLines
return Counts{
Bytes: byteCount,
Words: wordsCount,
Lines: linesCount,
}
}
func GetCountsSinglePass(r io.Reader) Counts {
linesCount := 0
bytesCount := 0
wordsCount := 0
isInsideWord := false
reader := bufio.NewReader(r)
for {
r, size, err := reader.ReadRune()
if err != nil {
break
}
bytesCount += size
if r == '\n' {
linesCount++
}
if unicode.IsSpace(r) {
isInsideWord = false
continue
}
if isInsideWord {
continue
}
wordsCount++
isInsideWord = true
}
return Counts{
Lines: linesCount,
Bytes: bytesCount,
Words: wordsCount,
}
}
func AddCounts(a Counts, b Counts) Counts {
return Counts{
Bytes: a.Bytes + b.Bytes,
Words: a.Words + b.Words,
Lines: a.Lines + b.Lines,
}
}