-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (118 loc) · 2.79 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
package main
import (
"flag"
"fmt"
"io"
"os"
"strings"
)
func countLines(bytes []byte) int {
var lineCount int
for _, byte := range bytes {
// The decimal representation of a UTF-8 newline is 10.
if byte == 10 {
lineCount++
}
}
return lineCount
}
func countBytes(bytes []byte) int {
byteCount := len(bytes)
return byteCount
}
func countChars(bytes []byte) int {
var charCount int
fileContent := string(bytes)
lines := strings.Split(fileContent, "\n")
for _, line := range lines {
arrLine := strings.Split(line, "")
for i := 0; i < len(arrLine); i++ {
charCount++
}
// New line counts so add it
charCount++
}
// Last line will not contain a newline so remove
charCount--
return charCount
}
func fileToBytes(fileName string) []byte {
bytes, err := os.ReadFile(fileName)
if err != nil {
panic(err)
}
return bytes
}
func stripTabAndSpaceFromLine(text string) []string {
var flatWords []string
words := strings.Split(text, " ")
for _, word := range words {
tabInWord := strings.Contains(word, " ")
if tabInWord {
splitWords := strings.Split(word, " ")
for _, splitWord := range splitWords {
flatWords = append(flatWords, splitWord)
}
} else {
flatWords = append(flatWords, word)
}
}
return flatWords
}
func countWords(bytes []byte) int {
var wordCount int
fileContent := string(bytes)
text := strings.Split(fileContent, "\n")
for _, line := range text {
line = strings.TrimSpace(line)
if line != "" {
words := stripTabAndSpaceFromLine(line)
if words[0] != "" {
wordCount += len(words)
}
}
}
return wordCount
}
func main() {
var fileBytes []byte
var fileName string
bytesFlag := flag.Bool("c", false, "Count the bytes in a file/stdin")
linesFlag := flag.Bool("l", false, "Count the number of lines in a file/stdin")
wordsFlag := flag.Bool("w", false, "Count the number of words in a file/stdin")
charsFlag := flag.Bool("m", false, "Count the number of characters in a file/stdin")
flag.Parse()
args := flag.Args()
if len(args) > 0 {
fileName = args[0]
fileBytes = fileToBytes(fileName)
} else {
stdin, err := io.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
fileBytes = stdin
}
if *bytesFlag {
fileByteTotal := countBytes(fileBytes)
fmt.Println(fileByteTotal, fileName)
}
if *linesFlag {
fileLines := countLines(fileBytes)
fmt.Println(fileLines, fileName)
}
if *wordsFlag {
fileWords := countWords(fileBytes)
fmt.Println(fileWords, fileName)
}
if *charsFlag {
fileChars := countChars(fileBytes)
fmt.Println(fileChars, fileName)
}
if !*bytesFlag && !*linesFlag && !*wordsFlag && !*charsFlag {
fileByteCount := countBytes(fileBytes)
fileLines := countLines(fileBytes)
fileWords := countWords(fileBytes)
fmt.Println(fileLines, fileWords, fileByteCount, fileName)
}
}