-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
213 lines (195 loc) · 4.4 KB
/
main.go
File metadata and controls
213 lines (195 loc) · 4.4 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
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
// cut – cut out selected portions of each line of a file.
package main
import (
"solod.dev/so/bufio"
"solod.dev/so/flag"
"solod.dev/so/fmt"
"solod.dev/so/io"
"solod.dev/so/mem"
"solod.dev/so/os"
"solod.dev/so/strconv"
"solod.dev/so/strings"
)
const illegalListValue = "cut: [-f] list: illegal list value"
// Range represents a field selection range (1-based).
// hi == 0 means "to end of line".
type Range struct {
lo int
hi int
}
var fields string
var delim string
var useWhitespace bool
var ranges [128]Range
var nranges int
func main() {
parseFlags()
if fields == "" {
fatal("must specify field list (-f)")
}
if useWhitespace && delim != "\t" {
fatal("-w and -d may not be used together")
}
parseFields(fields)
outDelim := delim
if useWhitespace {
outDelim = "\t"
}
args := flag.Args()
if len(args) == 0 {
cut(os.Stdin, outDelim)
os.Exit(0)
}
for _, fname := range args {
if fname == "-" {
cut(os.Stdin, outDelim)
} else {
f, err := os.Open(fname)
if err != nil {
fmt.Printf("cut: %s: No such file or directory\n", fname)
os.Exit(1)
}
cut(&f, outDelim)
f.Close()
}
}
}
// parseFlags parses command-line flags.
func parseFlags() {
flag.StringVar(&fields, "f", "", "field list")
flag.StringVar(&delim, "d", "\t", "field delimiter")
flag.BoolVar(&useWhitespace, "w", false, "use whitespace as delimiter")
flag.Parse()
}
// parseFields parses the field list specified by the -f flag.
func parseFields(list string) {
nranges = 0
i := 0
n := len(list)
for i < n {
// Skip separators (comma, space, tab).
for i < n && (list[i] == ',' || list[i] == ' ' || list[i] == '\t') {
i++
}
if i >= n {
break
}
start := i
for i < n && list[i] != ',' && list[i] != ' ' && list[i] != '\t' {
i++
}
parseToken(list[start:i])
}
if nranges == 0 {
fatal(illegalListValue)
}
}
// parseToken parses a single token from the field list,
// which can be one of the following forms:
// - N: a single number, meaning field N
// - -N: a leading dash, meaning fields 1 through N
// - N-: a trailing dash, meaning fields N through end of line
// - N-M: a range, meaning fields N through M
//
// After parsing, the token is added to the ranges array.
func parseToken(token string) {
if len(token) == 0 {
return
}
dashIdx := strings.IndexByte(token, '-')
if dashIdx < 0 {
// Single number: N.
n, err := strconv.Atoi(token)
if err != nil || n <= 0 {
fatal(illegalListValue)
}
ranges[nranges] = Range{n, n}
nranges++
return
}
if dashIdx == 0 {
// Leading dash: -N means fields 1 through N.
if len(token) < 2 {
fatal(illegalListValue)
}
n, err := strconv.Atoi(token[1:])
if err != nil || n <= 0 {
fatal(illegalListValue)
}
ranges[nranges] = Range{1, n}
nranges++
return
}
if dashIdx == len(token)-1 {
// Trailing dash: N- means fields N through end.
n, err := strconv.Atoi(token[:dashIdx])
if err != nil || n <= 0 {
fatal(illegalListValue)
}
ranges[nranges] = Range{n, 0}
nranges++
return
}
// Range: N-M.
lo, err := strconv.Atoi(token[:dashIdx])
if err != nil || lo <= 0 {
fatal(illegalListValue)
}
hi, err := strconv.Atoi(token[dashIdx+1:])
if err != nil || hi <= 0 {
fatal(illegalListValue)
}
ranges[nranges] = Range{lo, hi}
nranges++
}
// cut reads lines from r, extracts the selected fields,
// and writes them to standard output.
func cut(r io.Reader, outDelim string) {
scanner := bufio.NewScanner(mem.System, r)
for scanner.Scan() {
cutLine(scanner.Text(), outDelim)
}
scanner.Free()
}
// cutLine extracts the selected fields from a single line
// and writes them to standard output.
func cutLine(line string, outDelim string) {
var fields []string
if useWhitespace {
fields = strings.Fields(mem.System, line)
} else {
fields = strings.Split(mem.System, line, delim)
}
nfields := len(fields)
first := true
for i := 1; i <= nfields; i++ {
if isSelected(i, nfields) {
if !first {
fmt.Print(outDelim)
}
fmt.Print(fields[i-1])
first = false
}
}
fmt.Println("")
mem.FreeSlice(mem.System, fields)
}
// isSelected returns true if the given field number
// is selected by the field list.
func isSelected(field int, nfields int) bool {
for i := 0; i < nranges; i++ {
lo := ranges[i].lo
hi := ranges[i].hi
if hi == 0 {
hi = nfields
}
if field >= lo && field <= hi {
return true
}
}
return false
}
func fatal(msg string) {
fmt.Printf("cut: %s\n", msg)
os.Exit(1)
}