Skip to content

Commit cba3519

Browse files
authored
Add funcSize filter (#23)
1 parent e85c045 commit cba3519

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

funcSize.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@ import (
88

99
type funcSizeRunner struct {
1010
messageRE *regexp.Regexp
11+
filterRE *regexp.Regexp
1112
}
1213

1314
func (r *funcSizeRunner) Init() {
1415
r.messageRE = regexp.MustCompile(`(.*) STEXT.* size=(\d+)`)
16+
if filter != "" {
17+
r.filterRE = regexp.MustCompile(filter)
18+
}
1519
}
1620

1721
func (r *funcSizeRunner) Run(pkg string) error {
1822
cmd := exec.Command("go", r.getCmd(pkg)...)
23+
1924
out, err := cmd.CombinedOutput()
2025
if err != nil {
2126
return fmt.Errorf("%v: %s", err, out)
@@ -27,13 +32,15 @@ func (r *funcSizeRunner) Run(pkg string) error {
2732
}
2833
results := []resultRow{}
2934

30-
// TODO: add a CLI flag for the function name filtering?
31-
// Having to use a grep in 99% of use cases is not very convenient.
3235
for _, submatches := range r.messageRE.FindAllStringSubmatch(string(out), -1) {
33-
results = append(results, resultRow{
34-
Fn: submatches[1],
35-
Size: submatches[2],
36-
})
36+
fn, size := submatches[1], submatches[2]
37+
38+
if r.passesFilter(fn) {
39+
results = append(results, resultRow{
40+
Fn: fn,
41+
Size: size,
42+
})
43+
}
3744
}
3845

3946
if asJSON {
@@ -47,6 +54,10 @@ func (r *funcSizeRunner) Run(pkg string) error {
4754
return nil
4855
}
4956

57+
func (r *funcSizeRunner) passesFilter(fn string) bool {
58+
return r.filterRE == nil || r.filterRE.MatchString(fn)
59+
}
60+
5061
func (r *funcSizeRunner) getCmd(pkg string) []string {
5162
return goArgs(pkg, goArgsBuild, goArgsGcFlags("-S"))
5263
}

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const version = "v0.0.0"
1414
var (
1515
flagMod string
1616
asJSON bool
17+
filter string
1718
)
1819

1920
func main() {
@@ -99,6 +100,7 @@ func parseArgs(cmd string, args []string) error {
99100
fset := flag.NewFlagSet(cmd, flag.ContinueOnError)
100101
fset.StringVar(&flagMod, "mod", "", `-mod compiler flag(readonly|vendor)`)
101102
fset.BoolVar(&asJSON, "json", false, `return result as JSON`)
103+
fset.StringVar(&filter, "filter", "", "regex to filter the results")
102104
return fset.Parse(args)
103105
}
104106

0 commit comments

Comments
 (0)