-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
124 lines (108 loc) · 3.71 KB
/
main.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
package main
import (
"bytes"
"errors"
"fmt"
"os"
"strconv"
"strings"
"github.com/karust/regexcmp/engines"
"github.com/spf13/cobra"
)
const version = "1.0.0"
type Config struct {
IsDisplayOutput bool
IsGroupRun bool
IsPrintGroupRegexp bool
GenerateNonMatching bool
RegexFilePath string
ScanFilePath string
NumberMatching int
NumberNonMatching int
repeatScanTimes int
testCases map[string]RegexpEngine
execOrder []string
}
var config Config
var RootCmd = &cobra.Command{
Use: "regexcmp",
Short: "Golang regex libraries comparison",
Version: version,
SilenceUsage: true,
PreRun: func(cmd *cobra.Command, args []string) {
config.testCases = map[string]RegexpEngine{
"rure": &engines.Rure{},
"pcre": &engines.Pcre{},
"default": &engines.Default{},
"re2": &engines.Re2{},
"hyper": &engines.Hyper{},
"yara": &engines.Yara{},
"regexp2": &engines.Regexp2{},
}
config.execOrder = []string{"rure", "pcre", "re2", "hyper", "yara", "regexp2", "default"}
initRegexps()
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) == 0 {
args = append(args, "1")
}
config.repeatScanTimes, err = strconv.Atoi(args[0])
if err != nil {
return errors.New("Provide numeric value: " + err.Error())
}
if len(args) >= 2 {
testEngine := strings.ToLower(args[1])
if _, ok := config.testCases[testEngine]; args[1] != "" && ok {
config.execOrder = []string{testEngine}
} else {
fmt.Printf("No `%v` engine is available. Use one of %v\n", args[1], config.execOrder)
return
}
}
fmt.Println("Generate data...")
var data = bytes.Repeat([]byte("[email protected] nümbr=+71112223334 SSN:123-45-6789 http://1.1.1.1 3FZbgi29cpjq2GjdwV8eyHuJJnkLtktZc5 Й"), config.repeatScanTimes)
var locData *[]byte
locData = &data
fmt.Printf("Test data size: %.2fMB\n", float64(len(data))/1000/1000)
if config.IsPrintGroupRegexp {
groupRe := ""
for name, regexpr := range allRegexps {
regexpr = fmt.Sprintf("(?P<%v>%s)|", name, regexpr)
groupRe += regexpr
}
fmt.Println("Group regexp: " + groupRe[:len(groupRe)-1])
}
for _, name := range config.execOrder {
engine := config.testCases[name]
fmt.Printf("\nRun %v:\n", strings.ToUpper(name))
//fmt.Printf("Free memory: %dMB\n", memory.FreeMemory()/1024/1024)
if config.IsGroupRun {
err := runGroup(engine, locData)
if err != nil {
fmt.Printf("Error during test: %v\n", err)
}
} else {
err := runSingle(engine, locData)
if err != nil {
fmt.Printf("Error during test: %v\n", err)
}
}
}
return nil
},
}
func init() {
RootCmd.PersistentFlags().BoolVarP(&config.IsDisplayOutput, "display", "d", false, "Display matched results")
RootCmd.PersistentFlags().BoolVarP(&config.IsGroupRun, "group", "g", false, "Run grouped regexps (not individually)")
RootCmd.PersistentFlags().BoolVarP(&config.IsPrintGroupRegexp, "print", "p", false, "Print constructed group regexp")
RootCmd.PersistentFlags().BoolVarP(&config.GenerateNonMatching, "nm", "", false, "Generate non-matching regexps from existing")
// RootCmd.PersistentFlags().StringVarP(&config.RegexFilePath, "rfile", "r", "./test/regexps.txt", "File with newline separated and named regexps")
// RootCmd.PersistentFlags().StringVarP(&config.ScanFilePath, "sfile", "s", "./test/scanfile.txt", "File with text to scan")
RootCmd.PersistentFlags().IntVarP(&config.NumberMatching, "matching", "m", 0, "Number of additional matching regexps")
RootCmd.PersistentFlags().IntVarP(&config.NumberNonMatching, "nonmatching", "n", 0, "Number of additional non-matching regexps")
}
func main() {
if err := RootCmd.Execute(); err != nil {
os.Exit(1)
}
}