File: main.go

package info (click to toggle)
golang-github-gobwas-glob 0.2.3%2Bgit20180208.19c076c-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, bullseye-backports
  • size: 364 kB
  • sloc: sh: 20; makefile: 3
file content (82 lines) | stat: -rw-r--r-- 1,805 bytes parent folder | download | duplicates (4)
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
package main

import (
	"flag"
	"fmt"
	"github.com/gobwas/glob"
	"os"
	"strings"
	"testing"
	"unicode/utf8"
)

func benchString(r testing.BenchmarkResult) string {
	nsop := r.NsPerOp()
	ns := fmt.Sprintf("%10d ns/op", nsop)
	allocs := "0"
	if r.N > 0 {
		if nsop < 100 {
			// The format specifiers here make sure that
			// the ones digits line up for all three possible formats.
			if nsop < 10 {
				ns = fmt.Sprintf("%13.2f ns/op", float64(r.T.Nanoseconds())/float64(r.N))
			} else {
				ns = fmt.Sprintf("%12.1f ns/op", float64(r.T.Nanoseconds())/float64(r.N))
			}
		}

		allocs = fmt.Sprintf("%d", r.MemAllocs/uint64(r.N))
	}

	return fmt.Sprintf("%8d\t%s\t%s allocs", r.N, ns, allocs)
}

func main() {
	pattern := flag.String("p", "", "pattern to draw")
	sep := flag.String("s", "", "comma separated list of separators")
	fixture := flag.String("f", "", "fixture")
	verbose := flag.Bool("v", false, "verbose")
	flag.Parse()

	if *pattern == "" {
		flag.Usage()
		os.Exit(1)
	}

	var separators []rune
	for _, c := range strings.Split(*sep, ",") {
		if r, w := utf8.DecodeRuneInString(c); len(c) > w {
			fmt.Println("only single charactered separators are allowed")
			os.Exit(1)
		} else {
			separators = append(separators, r)
		}
	}

	g, err := glob.Compile(*pattern, separators...)
	if err != nil {
		fmt.Println("could not compile pattern:", err)
		os.Exit(1)
	}

	if !*verbose {
		fmt.Println(g.Match(*fixture))
		return
	}

	fmt.Printf("result: %t\n", g.Match(*fixture))

	cb := testing.Benchmark(func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			glob.Compile(*pattern, separators...)
		}
	})
	fmt.Println("compile:", benchString(cb))

	mb := testing.Benchmark(func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			g.Match(*fixture)
		}
	})
	fmt.Println("match:    ", benchString(mb))
}