Skip to content

Commit ac30d44

Browse files
author
Jonathan Gautheron
committed
Add -min and -max to trim down -numbers matches
1 parent b1fecdb commit ac30d44

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ Flags:
2727
-min-occurrences report from how many occurrences (default: 2)
2828
-match-constant look for existing constants matching the strings
2929
-numbers search also for duplicated numbers
30+
-min minimum value, only works with -numbers
31+
-max maximum value, only works with -numbers
3032
-output output formatting (text or json)
3133
3234
Examples:
3335
3436
goconst ./...
3537
goconst -ignore "yacc|\.pb\." $GOPATH/src/github.com/cockroachdb/cockroach/...
3638
goconst -min-occurrences 3 -output json $GOPATH/src/github.com/cockroachdb/cockroach
39+
goconst -numbers -min 60 -max 512 .
3740
```
3841

3942
### Other static analysis tools

cmd/goconst/main.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"log"
88
"os"
9+
"strconv"
910
"strings"
1011

1112
"github.com/jgautheron/goconst"
@@ -24,13 +25,16 @@ Flags:
2425
-min-occurrences report from how many occurrences (default: 2)
2526
-match-constant look for existing constants matching the strings
2627
-numbers search also for duplicated numbers
28+
-min minimum value, only works with -numbers
29+
-max maximum value, only works with -numbers
2730
-output output formatting (text or json)
2831
2932
Examples:
3033
3134
goconst ./...
3235
goconst -ignore "yacc|\.pb\." $GOPATH/src/github.com/cockroachdb/cockroach/...
3336
goconst -min-occurrences 3 -output json $GOPATH/src/github.com/cockroachdb/cockroach
37+
goconst -numbers -min 60 -max 512 .
3438
`
3539

3640
var (
@@ -39,6 +43,8 @@ var (
3943
flagMinOccurrences = flag.Int("min-occurrences", 2, "report from how many occurrences")
4044
flagMatchConstant = flag.Bool("match-constant", false, "look for existing constants matching the strings")
4145
flagNumbers = flag.Bool("numbers", false, "search also for duplicated numbers")
46+
flagMin = flag.Int("min", 0, "minimum value, only works with -numbers")
47+
flagMax = flag.Int("max", 0, "maximum value, only works with -numbers")
4248
flagOutput = flag.String("output", "text", "output formatting")
4349
)
4450

@@ -68,20 +74,30 @@ func main() {
6874
os.Exit(1)
6975
}
7076

71-
printOutput(strs, consts, *flagOutput, *flagMinOccurrences)
77+
printOutput(strs, consts, *flagOutput, *flagMinOccurrences, *flagMin, *flagMax)
7278
}
7379

7480
func usage() {
7581
fmt.Fprintf(os.Stderr, usageDoc)
7682
os.Exit(1)
7783
}
7884

79-
func printOutput(strs goconst.Strings, consts goconst.Constants, output string, minOccurrences int) {
80-
// Filter out items whose occurrences don't match the min value
85+
func printOutput(strs goconst.Strings, consts goconst.Constants, output string, minOccurrences, min, max int) {
8186
for str, item := range strs {
87+
// Filter out items whose occurrences don't match the min value
8288
if len(item) < minOccurrences {
8389
delete(strs, str)
8490
}
91+
92+
// If the value is a number
93+
if i, err := strconv.Atoi(str); err == nil {
94+
if min != 0 && i < min {
95+
delete(strs, str)
96+
}
97+
if max != 0 && i > max {
98+
delete(strs, str)
99+
}
100+
}
85101
}
86102

87103
switch output {

0 commit comments

Comments
 (0)