-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcounter.go
154 lines (128 loc) · 3.46 KB
/
counter.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
package wildcat
import (
"unicode/utf8"
)
type calculator interface {
calculate(data []byte) int64
}
// Counter shows
type Counter interface {
IsType(ct CounterType) bool
Type() CounterType
update(data []byte)
Count(ct CounterType) int64
}
// CounterType represents the types of counting.
type CounterType int
const (
// Bytes shows the counter type for counting byte size.
Bytes CounterType = 1
// Characters shows the counter type for counting characters.
Characters = 2
// Words shows the counter type for counting the words.
Words = 4
// Lines shows the counter type for counting the lines.
Lines = 8
// All shows the counter type for counting byte size, characters, words, and lines.
All = Lines | Words | Characters | Bytes
)
// IsType checks the equality between the receiver and the given counter type.
func (ct CounterType) IsType(ct2 CounterType) bool {
return ct&ct2 == ct2
}
// NewCounter generates Counter by CounterTypes.
func NewCounter(counterType CounterType) Counter {
counter := &multipleCounter{ct: counterType, counters: map[CounterType]Counter{}}
generators := []struct {
ct CounterType
generator func() Counter
}{
{Bytes, func() Counter { return &singleCounter{ct: Bytes, number: 0, calculator: &byteCalculator{}} }},
{Characters, func() Counter { return &singleCounter{ct: Characters, number: 0, calculator: &characterCalculator{}} }},
{Words, func() Counter { return &singleCounter{ct: Words, number: 0, calculator: &wordCalculator{}} }},
{Lines, func() Counter { return &singleCounter{ct: Lines, number: 0, calculator: &lineCalculator{}} }},
}
for _, gens := range generators {
if counterType&gens.ct == gens.ct {
counter.counters[gens.ct] = gens.generator()
}
}
return counter
}
type multipleCounter struct {
ct CounterType
counters map[CounterType]Counter
}
func (mc *multipleCounter) IsType(ct CounterType) bool {
return mc.ct&ct == ct
}
func (mc *multipleCounter) Type() CounterType {
return mc.ct
}
func (mc *multipleCounter) update(data []byte) {
for _, v := range mc.counters {
v.update(data)
}
}
func (mc *multipleCounter) Count(ct CounterType) int64 {
counter, ok := mc.counters[ct]
if !ok {
return -1
}
return counter.Count(ct)
}
type singleCounter struct {
ct CounterType
number int64
calculator calculator
}
func (sc *singleCounter) IsType(ct CounterType) bool {
return sc.ct.IsType(ct)
}
func (sc *singleCounter) Type() CounterType {
return sc.ct
}
func (sc *singleCounter) Count(ct CounterType) int64 {
return sc.number
}
func (sc *singleCounter) update(data []byte) {
sc.number = sc.number + sc.calculator.calculate(data)
}
type lineCalculator struct {
}
func (lc *lineCalculator) calculate(data []byte) int64 {
var number int64 = 0
for _, datum := range data {
if datum == '\n' {
number++
}
}
return number
}
type wordCalculator struct {
}
func isWhiteSpace(data byte) bool {
return data == 0 || data == ' ' || data == '\t' || data == '\n' || data == '\r'
}
func (wc *wordCalculator) calculate(data []byte) int64 {
number := int64(0)
if len(data) > 0 && !isWhiteSpace(data[0]) {
number++
}
for i, datum := range data {
if i > 0 && isWhiteSpace(data[i-1]) && !isWhiteSpace(datum) {
number++
}
}
return number
}
type byteCalculator struct {
}
func (bc *byteCalculator) calculate(data []byte) int64 {
return int64(len(data))
}
type characterCalculator struct {
}
func (cc *characterCalculator) calculate(data []byte) int64 {
return int64(utf8.RuneCount(data))
}