-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
430 lines (342 loc) · 9.02 KB
/
commands.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package main
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
)
// Command represents a single stage in the pipeline of commands. It processes
// `data` between `start` and `end` and if it finds a match calls `match` with the
// start and end of the match.
type Command interface {
Do(data io.ReaderAt, start, end int64, match func(start, end int64)) error
}
type Doner interface {
Done() error
}
type RegexpCommand struct {
regexp *regexp.Regexp
secRdr *io.SectionReader
rdr *bufio.Reader
start, _offset int64
}
// NewRegexpCommand returns a new Command that uses the specified Regexp.
// The `label` chooses which Command to build; i.e. 'x' creates an XCommand.
func NewRegexpCommand(label rune, re *regexp.Regexp) Command {
switch label {
case 'x':
return &XCommand{RegexpCommand{regexp: re}}
case 'g':
return &GCommand{RegexpCommand{regexp: re}}
case 'y':
return &YCommand{RegexpCommand{regexp: re}}
case 'v':
return &VCommand{RegexpCommand{regexp: re}}
case 'z':
return &ZCommand{RegexpCommand{regexp: re}, -1}
default:
panic(fmt.Sprintf("NewRegexpCommand: called with invalid command rune %c", label))
}
return nil
}
func (r *RegexpCommand) reader(data io.ReaderAt, start, end int64) io.RuneReader {
r.secRdr = io.NewSectionReader(data, start, end-start)
r.rdr = bufio.NewReader(r.secRdr)
r.start = start
r._offset = start
return r.rdr
}
func (r *RegexpCommand) offset() int64 {
return r._offset
}
func (r *RegexpCommand) updateOffset(o int64) {
r._offset = o
r.secRdr.Seek(r._offset-r.start, io.SeekStart)
r.rdr.Reset(r.secRdr)
}
// XCommand is like the sam editor's x command: loop over matches of this regexp
type XCommand struct {
RegexpCommand
}
func (c XCommand) Do(data io.ReaderAt, start, end int64, match func(start, end int64)) error {
if emptyRange(start, end) {
return nil
}
rdr := c.reader(data, start, end)
dbg("XCommand.Do: section reader from %d len %d\n", start, end-start)
for {
locs := c.RegexpCommand.regexp.FindReaderSubmatchIndex(rdr)
if locs == nil {
break
}
dbg("XCommand.Do: match at %d-%d\n", locs[0], locs[1])
match(c.offset()+int64(locs[0]), c.offset()+int64(locs[1]))
delta := int64(locs[1])
if delta == 0 {
dbg("XCommand.Do: exiting because match did not make progress forward\n")
break
}
c.updateOffset(c.offset() + delta)
}
return nil
}
// YCommand is like the sam editor's y command: loop over strings before, between, and after matches of this regexp
type YCommand struct {
RegexpCommand
}
func (c YCommand) Do(data io.ReaderAt, start, end int64, match func(start, end int64)) error {
if emptyRange(start, end) {
return nil
}
rdr := c.reader(data, start, end)
dbg("YCommand.Do: section reader from %d len %d\n", start, end-start)
for {
locs := c.RegexpCommand.regexp.FindReaderSubmatchIndex(rdr)
if locs == nil {
break
}
dbg("YCommand.Do: re match at %d-%d\n", locs[0], locs[1])
dbg("YCommand.Do: sending match %d-%d\n", c.offset(), c.offset()+int64(locs[0]))
match(c.offset(), c.offset()+int64(locs[0]))
delta := int64(locs[1])
if delta == 0 {
dbg("YCommand.Do: exiting because match did not make progress forward\n")
break
}
c.updateOffset(c.offset() + delta)
}
if c.offset() != end {
match(c.offset(), end)
}
return nil
}
// YCommand is like the sam editor's y command, but instead of omitting the matching part, it is included
// as part of the following match.
type ZCommand struct {
RegexpCommand
matchStart int64
}
func (c ZCommand) Do(data io.ReaderAt, start, end int64, match func(start, end int64)) error {
if emptyRange(start, end) {
return nil
}
c.matchStart = -1
rdr := c.reader(data, start, end)
dbg("ZCommand.Do: section reader from %d len %d\n", start, end-start)
for {
locs := c.RegexpCommand.regexp.FindReaderSubmatchIndex(rdr)
if locs == nil {
break
}
dbg("ZCommand.Do: match starting at %d\n", locs[0])
if c.matchStart >= 0 {
dbg("ZCommand.Do: match at %d-%d. offset=%d\n", c.matchStart, c.offset()+int64(locs[0]), c.offset())
match(c.matchStart, c.offset()+int64(locs[0]))
c.matchStart = int64(locs[0])
}
c.matchStart = c.offset() + int64(locs[0])
delta := int64(locs[1])
if delta == 0 {
dbg("ZCommand.Do: exiting because match did not make progress forward\n")
break
}
c.updateOffset(c.offset() + delta)
}
if c.matchStart >= 0 && c.offset() != end {
match(c.matchStart, end)
}
return nil
}
// GCommand is like the sam editor's g command: if the regexp matches the range, output the range, otherwise output no range.
type GCommand struct {
RegexpCommand
}
func (c GCommand) Do(data io.ReaderAt, start, end int64, match func(start, end int64)) error {
if emptyRange(start, end) {
return nil
}
rdr := c.reader(data, start, end)
dbg("GCommand.Do: section reader from %d len %d\n", start, end-start)
if c.RegexpCommand.regexp.MatchReader(rdr) {
dbg("GCommand.Do: match\n")
match(start, end)
return nil
}
return nil
}
// VCommand is like the sam editor's y command: if the regexp doesn't match the range, output the range, otherwise output no range.
type VCommand struct {
RegexpCommand
}
func (c VCommand) Do(data io.ReaderAt, start, end int64, match func(start, end int64)) error {
if emptyRange(start, end) {
return nil
}
rdr := c.reader(data, start, end)
dbg("GCommand.Do: section reader from %d len %d\n", start, end-start)
if c.RegexpCommand.regexp.MatchReader(rdr) {
dbg("GCommand.Do: match\n")
return nil
}
match(start, end)
return nil
}
// PrintCommand is like the sam editor's p command.
type PrintCommand struct {
out io.Writer
sep []byte
printSep bool
}
func (p *PrintCommand) Do(data io.ReaderAt, start, end int64, match func(start, end int64)) error {
buf, err := readRange(data, start, end)
dbg("PrintCommand.Do(%s)\n", string(buf))
if err != nil {
return err
}
if p.out == nil {
p.out = os.Stdout
}
if p.printSep && len(p.sep) > 0 {
p.out.Write(p.sep)
}
p.out.Write(buf)
p.printSep = true
return nil
}
// NewPrintCommand returns a new PrintCommand that writes to `out` and prints the separator `sep` between each match.
func NewPrintCommand(out io.Writer, sep string) *PrintCommand {
return &PrintCommand{out: out, sep: []byte(sep)}
}
// PrintLineCommand is like the sam editor's = command.
type PrintLineCommand struct {
fname string
out io.Writer
}
func NewPrintLineCommand(fname string, out io.Writer) *PrintLineCommand {
return &PrintLineCommand{fname: fname, out: out}
}
func (p *PrintLineCommand) Do(data io.ReaderAt, start, end int64, match func(start, end int64)) error {
dbg("PrintLineCommand.Do for %d-%d\n", start, end)
nl := 1
var (
err error
r rune
)
sr := io.NewSectionReader(data, 0, start)
rdr := bufio.NewReader(sr)
readAndCount := func() {
for {
r, _, err = rdr.ReadRune()
if err != nil {
break
}
if r == '\n' {
nl++
}
}
}
// Re-read data and count the number of lines
readAndCount()
if err != io.EOF {
return err
}
p.out.Write([]byte(fmt.Sprintf("%s:%d", p.fname, nl)))
scnt := nl
sr = io.NewSectionReader(data, start, end-start)
rdr.Reset(sr)
readAndCount()
if err != io.EOF {
return err
}
if nl != scnt {
p.out.Write([]byte(fmt.Sprintf(",%d", nl)))
}
p.out.Write([]byte("\n"))
return nil
}
// NCommand only allows ranges in the range [first,last] to pass. Ranges
// are counted starting from 0.
// Syntax:
// 5 sixth range
// 5:6 sixth and seventh ranges
// 5: sixth range to last
// 0:-2 sixth range to second-last
// -1 last
type NCommand struct {
// end == -1 means end is the last possible range.
// end == -2 means the second last range
start, end int
ranges []Range
match func(start, end int64)
}
func NewNCommand(s string) (*NCommand, error) {
cmd := &NCommand{}
var err error
parts := strings.Split(s, ":")
cmd.start, err = strconv.Atoi(parts[0])
if err != nil {
return nil, err
}
if len(parts) == 1 {
// a single number
cmd.end = cmd.start
} else {
if len(parts[1]) == 0 {
cmd.end = -1
} else {
cmd.end, err = strconv.Atoi(parts[1])
if err != nil {
return nil, err
}
}
}
return cmd, err
}
func MustNCommand(s string) *NCommand {
c, err := NewNCommand(s)
if err != nil {
panic(err)
}
return c
}
func (p *NCommand) Do(data io.ReaderAt, start, end int64, match func(start, end int64)) error {
p.saveRange(start, end)
p.match = match
return nil
}
func (p *NCommand) saveRange(start, end int64) {
if p.ranges == nil {
p.ranges = make([]Range, 0, 20)
}
p.ranges = append(p.ranges, Range{start, end})
}
func (p *NCommand) Done() error {
p.computeActualStart()
p.computeActualEnd()
if p.start > p.end {
// Treat this as the empty set
return nil
}
if p.start < 0 || p.end > len(p.ranges) {
return nil
}
for _, r := range p.ranges[p.start:p.end] {
p.match(r.Start, r.End)
}
return nil
}
func (p *NCommand) computeActualStart() {
if p.start < 0 {
p.start = len(p.ranges) + p.start
}
}
func (p *NCommand) computeActualEnd() {
if p.end >= 0 {
p.end += 1
} else {
p.end = len(p.ranges) + p.end + 1
}
}