Skip to content

Commit

Permalink
Disallow out-of-order field lists.
Browse files Browse the repository at this point in the history
  • Loading branch information
timbray committed May 25, 2021
1 parent f6cf869 commit 4eba337
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ default value is 10.
`-f fieldlist, --fields fieldlist` Specifies which fields should be extracted from incoming records
and used in computing occurrence counts. The fieldlist must be a
comma‐separated list of integers identifying field numbers,
which start at one, for example 3 and 2,5,6.
which start at one, for example 3 and 2,5,6. The fields
must be provided in order, so 3,1,7 is an error.

If no fieldlist is provided, **tf** treats the whole input record as a single field.

Expand Down
2 changes: 1 addition & 1 deletion doc/tf.1
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ value is 10.
Specifies which fields should be extracted from incoming records and used in
computing occurrence counts. The fieldlist must be a comma-separated list of
integers identifying field numbers, which start at one, for example \fB3\fR and
\fB2,5,6\fR.
\fB2,5,6\fR. The fields must be provided in order, so \fB3,1,7\fR is an error.

If no fieldlist is provided, \fBtf\fR treats the whole input record as a single
field.
Expand Down
16 changes: 14 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"os"
"runtime/pprof"
Expand All @@ -23,7 +24,8 @@ Usage: tf
-h, -help, --help
(filename) [optional, stdin if omitted]
Field list is comma-separated integers, e.g. -f 3 or --fields 1,3,7
Field list is comma-separated integers, e.g. -f 3 or --fields 1,3,7. The fields
must be provided in order, so 3,1,7 is an error.
The regexp-valued fields work as follows:
-g/--grep discardsrecords that don't match the regexp (g for grep)
Expand Down Expand Up @@ -132,7 +134,11 @@ func main() {
return
}
// The generated trace can be analyzed with: go tool trace <tracefile>
trace.Start(f)
err = trace.Start(f)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "can't start tracing: %s", err.Error())
return
}
defer trace.Stop()
}
var kf = topfew.NewKeyFinder(fields)
Expand Down Expand Up @@ -169,11 +175,17 @@ func main() {
func parseFields(spec string) ([]uint, error) {
parts := strings.Split(spec, ",")
var fields []uint
lastNum := -1
for _, part := range parts {
num, err := strconv.Atoi(part)
if err != nil {
return nil, fmt.Errorf("Illegal field spec: %v", err)
}
if num <= lastNum {
return nil, errors.New(fmt.Sprintf( "field-number list must be in order; problem at \"%d\":", num))
} else {
lastNum = num
}
fields = append(fields, uint(num))
}
return fields, nil
Expand Down

0 comments on commit 4eba337

Please sign in to comment.