forked from benbjohnson/unistat
/
main.go
184 lines (167 loc) · 3.86 KB
/
main.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
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"unicode"
)
func main() {
m := NewMain()
if err := m.Run(os.Args[1:]...); err != nil {
fmt.Fprintln(m.Stderr, err)
os.Exit(1)
}
}
// Main represents the main program execution.
type Main struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}
// NewMain returns a new instance of Main.
func NewMain() *Main {
return &Main{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
}
// Run executes the program.
func (m *Main) Run(args ...string) error {
// Parse command line flags.
fs := flag.NewFlagSet("unistat", flag.ContinueOnError)
fs.SetOutput(m.Stderr)
if err := fs.Parse(args); err != nil {
return err
}
// Validate flags.
if fs.NArg() > 1 {
return errors.New("too many files specified")
}
// Determine if stdin or a CLI arg should be used as input.
r, err := m.Reader(fs.Arg(0))
if err != nil {
return err
}
defer r.Close()
// Read each rune and calculate stats.
stats, err := m.Stat(bufio.NewReader(r))
if err != nil {
return err
}
// Print stats to terminal.
fmt.Fprintf(m.Stdout, "%-10s %d\n", "Control:", stats.ControlN)
fmt.Fprintf(m.Stdout, "%-10s %d\n", "Digit:", stats.DigitN)
fmt.Fprintf(m.Stdout, "%-10s %d\n", "Graphic:", stats.GraphicN)
fmt.Fprintf(m.Stdout, "%-10s %d\n", "Letter:", stats.LetterN)
fmt.Fprintf(m.Stdout, "%-10s %d\n", "Lower:", stats.LowerN)
fmt.Fprintf(m.Stdout, "%-10s %d\n", "Mark:", stats.MarkN)
fmt.Fprintf(m.Stdout, "%-10s %d\n", "Number:", stats.NumberN)
fmt.Fprintf(m.Stdout, "%-10s %d\n", "Print:", stats.PrintN)
fmt.Fprintf(m.Stdout, "%-10s %d\n", "Punct:", stats.PunctN)
fmt.Fprintf(m.Stdout, "%-10s %d\n", "Space:", stats.SpaceN)
fmt.Fprintf(m.Stdout, "%-10s %d\n", "Symbol:", stats.SymbolN)
fmt.Fprintf(m.Stdout, "%-10s %d\n", "Title:", stats.TitleN)
fmt.Fprintf(m.Stdout, "%-10s %d\n", "Upper:", stats.UpperN)
fmt.Fprintln(m.Stdout, "")
fmt.Fprintf(m.Stdout, "%-10s %d\n", "Multibyte:", stats.MultiByteN)
fmt.Fprintln(m.Stdout, "")
fmt.Fprintf(m.Stdout, "%-10s %d\n", "Total:", stats.TotalN)
fmt.Fprintln(m.Stdout, "")
return nil
}
// Reader returns a file handle if filename is specified. Otherwise returns stdin.
// The reader must be closed by the caller.
func (m *Main) Reader(filename string) (io.ReadCloser, error) {
// Use stdin if there's no file specified.
if filename == "" {
fmt.Fprintln(m.Stderr, "reading from stdin\n")
return ioutil.NopCloser(m.Stdin), nil
}
// Otherwise open the file.
f, err := os.Open(filename)
if err != nil {
return nil, err
}
return f, nil
}
// Stat calculates statistics for all runes read from r.
func (m *Main) Stat(r io.RuneReader) (Stats, error) {
var stats Stats
for {
// Read next character.
ch, sz, err := r.ReadRune()
if err == io.EOF {
break
} else if err != nil {
return stats, err
}
// Calculate stats.
stats.TotalN++
if unicode.IsControl(ch) {
stats.ControlN++
}
if unicode.IsDigit(ch) {
stats.DigitN++
}
if unicode.IsGraphic(ch) {
stats.GraphicN++
}
if unicode.IsLetter(ch) {
stats.LetterN++
}
if unicode.IsLower(ch) {
stats.LowerN++
}
if unicode.IsMark(ch) {
stats.MarkN++
}
if unicode.IsNumber(ch) {
stats.NumberN++
}
if unicode.IsPrint(ch) {
stats.PrintN++
}
if unicode.IsPunct(ch) {
stats.PunctN++
}
if unicode.IsSpace(ch) {
stats.SpaceN++
}
if unicode.IsSymbol(ch) {
stats.SymbolN++
}
if unicode.IsTitle(ch) {
stats.TitleN++
}
if unicode.IsUpper(ch) {
stats.UpperN++
}
if sz > 1 {
stats.MultiByteN++
}
}
return stats, nil
}
// Stats represents unicode stats for a set of runes.
type Stats struct {
TotalN int
ControlN int
DigitN int
GraphicN int
LetterN int
LowerN int
MarkN int
NumberN int
PrintN int
PunctN int
SpaceN int
SymbolN int
TitleN int
UpperN int
MultiByteN int
}