| 1 |
package experimental |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"reflect" |
| 6 |
"sort" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/betterleaks/go-re2" |
| 11 |
) |
| 12 |
|
| 13 |
func TestCompileLatin1(t *testing.T) { |
| 14 |
tests := []struct { |
| 15 |
pattern string |
| 16 |
input string |
| 17 |
want bool |
| 18 |
}{ |
| 19 |
{ |
| 20 |
pattern: `\xac\xed\x00\x05`, |
| 21 |
input: "\xac\xed\x00\x05t\x00\x04test", |
| 22 |
want: true, |
| 23 |
}, |
| 24 |
{ |
| 25 |
pattern: `\xac\xed\x00\x05`, |
| 26 |
input: "\xac\xed\x00t\x00\x04test", |
| 27 |
want: false, |
| 28 |
}, |
| 29 |
|
| 30 |
{ |
| 31 |
pattern: `(?sm)\xac\xed\x00\x05`, |
| 32 |
input: "\xac\xed\x00\x05t\x00\x04test", |
| 33 |
want: true, |
| 34 |
}, |
| 35 |
{ |
| 36 |
pattern: `(?sm)\xac\xed\x00\x05`, |
| 37 |
input: "\xac\xed\x00t\x00\x04test", |
| 38 |
want: false, |
| 39 |
}, |
| 40 |
|
| 41 |
{ |
| 42 |
pattern: "ハロー", |
| 43 |
input: "ハローワールド", |
| 44 |
want: true, |
| 45 |
}, |
| 46 |
{ |
| 47 |
pattern: "ハロー", |
| 48 |
input: "グッバイワールド", |
| 49 |
want: false, |
| 50 |
}, |
| 51 |
} |
| 52 |
|
| 53 |
for _, tc := range tests { |
| 54 |
tt := tc |
| 55 |
t.Run(fmt.Sprintf("%s/%s", tt.pattern, tt.input), func(t *testing.T) { |
| 56 |
re := MustCompileLatin1(tt.pattern) |
| 57 |
if re.MatchString(tt.input) != tt.want { |
| 58 |
t.Errorf("MatchString(%q) = %v, want %v", tt.input, !tt.want, tt.want) |
| 59 |
} |
| 60 |
}) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
var goodRe = []string{ |
| 65 |
``, |
| 66 |
`.`, |
| 67 |
`^.$`, |
| 68 |
`a`, |
| 69 |
`a*`, |
| 70 |
`a+`, |
| 71 |
`a?`, |
| 72 |
`a|b`, |
| 73 |
`a*|b*`, |
| 74 |
`(a*|b)(c*|d)`, |
| 75 |
`[a-z]`, |
| 76 |
`[a-abc-c\-\]\[]`, |
| 77 |
`[a-z]+`, |
| 78 |
`[abc]`, |
| 79 |
`[^1234]`, |
| 80 |
`[^\n]`, |
| 81 |
`\!\\`, |
| 82 |
} |
| 83 |
|
| 84 |
type stringError struct { |
| 85 |
re string |
| 86 |
err string |
| 87 |
} |
| 88 |
|
| 89 |
var badSet = []stringError{ |
| 90 |
{`*`, "error parsing regexp: no argument for repetition operator: *"}, |
| 91 |
{`+`, "error parsing regexp: no argument for repetition operator: +"}, |
| 92 |
{`?`, "error parsing regexp: no argument for repetition operator: ?"}, |
| 93 |
{`(abc`, "error parsing regexp: missing ): (abc"}, |
| 94 |
{`abc)`, "error parsing regexp: unexpected ): abc)"}, |
| 95 |
{`x[a-z`, "error parsing regexp: missing ]: [a-z"}, |
| 96 |
{`[z-a]`, "error parsing regexp: invalid character class range: z-a"}, |
| 97 |
{`abc\`, "error parsing regexp: trailing \\"}, |
| 98 |
{`a**`, "error parsing regexp: bad repetition operator: **"}, |
| 99 |
{`a*+`, "error parsing regexp: bad repetition operator: *+"}, |
| 100 |
{`\x`, "error parsing regexp: invalid escape sequence: \\x"}, |
| 101 |
{strings.Repeat(`)\pL`, 27000), "error parsing regexp: unexpected ): " + strings.Repeat(`)\pL`, 27000)}, |
| 102 |
} |
| 103 |
|
| 104 |
func compileSetTest(t *testing.T, exprs []string, expErr string) *Set { |
| 105 |
t.Helper() |
| 106 |
set, err := CompileSet(exprs) |
| 107 |
if expErr == "" && err != nil { |
| 108 |
t.Error("compiling `", exprs, "`; unexpected error: ", err.Error()) |
| 109 |
} |
| 110 |
if expErr != "" && err == nil { |
| 111 |
t.Error("compiling `", exprs, "`; missing error") |
| 112 |
} else if expErr != "" && !strings.Contains(err.Error(), expErr) { |
| 113 |
t.Error("compiling `", exprs, "`; wrong error: ", err.Error(), "; want ", expErr) |
| 114 |
} |
| 115 |
return set |
| 116 |
} |
| 117 |
|
| 118 |
func TestGoodSetCompile(t *testing.T) { |
| 119 |
compileSetTest(t, goodRe, "") |
| 120 |
} |
| 121 |
|
| 122 |
func TestBadCompileSet(t *testing.T) { |
| 123 |
for i := range badSet { |
| 124 |
compileSetTest(t, []string{badSet[i].re}, badSet[i].err) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
type SetTest struct { |
| 129 |
exprs []string |
| 130 |
matches string |
| 131 |
matched [4][]int |
| 132 |
} |
| 133 |
|
| 134 |
var setTests = []SetTest{ |
| 135 |
{ |
| 136 |
exprs: []string{`(d)(e){0}(f)`, `[a-c]+`, `abc`, `\d+`}, |
| 137 |
matches: "x", |
| 138 |
matched: [4][]int{ |
| 139 |
nil, nil, nil, nil, |
| 140 |
}, |
| 141 |
}, |
| 142 |
{ |
| 143 |
exprs: []string{`(d)(e){0}(f)`, `[a-c]+`, `abc`, `\d+`}, |
| 144 |
matches: "123", |
| 145 |
matched: [4][]int{ |
| 146 |
nil, {3}, {3}, {3}, |
| 147 |
}, |
| 148 |
}, |
| 149 |
{ |
| 150 |
exprs: []string{`(d)(e){0}(f)`, `[a-c]+`, `abc`, `\d+`}, |
| 151 |
matches: "df123abc", |
| 152 |
matched: [4][]int{ |
| 153 |
nil, {0}, {0, 3}, {0, 1, 2, 3}, |
| 154 |
}, |
| 155 |
}, |
| 156 |
{ |
| 157 |
exprs: []string{`(d)(e){0}(f)`, `[a-c]+`, `abc`, `\d+`, `d{4}-\d{2}-\d{2}$`, `[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}`, `1[3-9]\d{9}`, `\.[a-zA-Z0-9]+$`, `<!--[\s\S]*?-->`}, |
| 158 |
matches: "abcdef123</html><!-- test -->[email protected]", |
| 159 |
matched: [4][]int{ |
| 160 |
nil, {1}, {1, 2}, {1, 2, 3, 5, 6, 7, 8}, |
| 161 |
}, |
| 162 |
}, |
| 163 |
{ |
| 164 |
exprs: []string{`(d)(e){0}(f)`, `[a-c]+`, `abc`, `\d+`, `d{4}-\d{2}-\d{2}$`, `[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}`, `1[3-9]\d{9}`, `\.[a-zA-Z0-9]+$`, `<!--[\s\S]*?-->`}, |
| 165 |
matches: "[email protected]", |
| 166 |
matched: [4][]int{ |
| 167 |
nil, {0}, {0, 3}, {0, 1, 3, 5, 6, 7}, |
| 168 |
}, |
| 169 |
}, |
| 170 |
} |
| 171 |
|
| 172 |
func setFindAllTest(t *testing.T, set *Set, matchStr string, matchNum int, matchedIDs []int) { |
| 173 |
t.Helper() |
| 174 |
m := set.FindAll([]byte(matchStr), matchNum) |
| 175 |
sort.Ints(m) |
| 176 |
if !reflect.DeepEqual(m, matchedIDs) { |
| 177 |
t.Errorf("Match failure on %s: %v should be %v", matchStr, m, matchedIDs) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func setFindAllStringTest(t *testing.T, set *Set, matchStr string, matchNum int, matchedIDs []int) { |
| 182 |
t.Helper() |
| 183 |
m := set.FindAllString(matchStr, matchNum) |
| 184 |
sort.Ints(m) |
| 185 |
if !reflect.DeepEqual(m, matchedIDs) { |
| 186 |
t.Errorf("Match failure on %s: %v should be %v", matchStr, m, matchedIDs) |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func TestSetFindAll(t *testing.T) { |
| 191 |
for _, test := range setTests { |
| 192 |
set := compileSetTest(t, test.exprs, "") |
| 193 |
if set == nil { |
| 194 |
return |
| 195 |
} |
| 196 |
setFindAllTest(t, set, test.matches, 0, test.matched[0]) |
| 197 |
setFindAllTest(t, set, test.matches, 1, test.matched[1]) |
| 198 |
setFindAllTest(t, set, test.matches, 2, test.matched[2]) |
| 199 |
setFindAllTest(t, set, test.matches, 7, test.matched[3]) |
| 200 |
setFindAllTest(t, set, test.matches, 20, test.matched[3]) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
func TestSetFindAllString(t *testing.T) { |
| 205 |
for _, test := range setTests { |
| 206 |
set := compileSetTest(t, test.exprs, "") |
| 207 |
if set == nil { |
| 208 |
return |
| 209 |
} |
| 210 |
setFindAllStringTest(t, set, test.matches, 0, test.matched[0]) |
| 211 |
setFindAllStringTest(t, set, test.matches, 1, test.matched[1]) |
| 212 |
setFindAllStringTest(t, set, test.matches, 2, test.matched[2]) |
| 213 |
setFindAllStringTest(t, set, test.matches, 7, test.matched[3]) |
| 214 |
setFindAllStringTest(t, set, test.matches, 20, test.matched[3]) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
func BenchmarkSet(b *testing.B) { |
| 219 |
b.Run("findAll", func(b *testing.B) { |
| 220 |
set, err := CompileSet(goodRe) |
| 221 |
if err != nil { |
| 222 |
panic(err) |
| 223 |
} |
| 224 |
for range b.N { |
| 225 |
set.FindAll([]byte("abcdef123</html><!-- test -->[email protected]"), 20) |
| 226 |
} |
| 227 |
}) |
| 228 |
} |
| 229 |
|
| 230 |
func BenchmarkSetMatchWithFindSubmatch(b *testing.B) { |
| 231 |
b.Run("set match", func(b *testing.B) { |
| 232 |
set, err := CompileSet(goodRe) |
| 233 |
if err != nil { |
| 234 |
panic(err) |
| 235 |
} |
| 236 |
for range b.N { |
| 237 |
set.FindAll([]byte("abcd123"), 20) |
| 238 |
} |
| 239 |
}) |
| 240 |
b.Run("findSubmatch", func(b *testing.B) { |
| 241 |
re, err := re2.Compile("(" + strings.Join(goodRe, ")|(") + ")") |
| 242 |
if err != nil { |
| 243 |
panic(err) |
| 244 |
} |
| 245 |
for range b.N { |
| 246 |
re.FindAllStringSubmatchIndex("abcd123", 20) |
| 247 |
} |
| 248 |
}) |
| 249 |
} |
| 250 |
|
| 251 |
func ExampleCompileSet() { |
| 252 |
exprs := []string{"abc", "\\d+"} |
| 253 |
set, err := CompileSet(exprs) |
| 254 |
if err != nil { |
| 255 |
panic(err) |
| 256 |
} |
| 257 |
fmt.Println(set.FindAll([]byte("abcd"), len(exprs))) |
| 258 |
fmt.Println(set.FindAll([]byte("123"), len(exprs))) |
| 259 |
fmt.Println(set.FindAll([]byte("abc123"), len(exprs))) |
| 260 |
fmt.Println(set.FindAll([]byte("def"), len(exprs))) |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
} |