| 1 |
package re2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"regexp/syntax" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
var goodRe = []string{ |
| 11 |
``, |
| 12 |
`.`, |
| 13 |
`^.$`, |
| 14 |
`a`, |
| 15 |
`a*`, |
| 16 |
`a+`, |
| 17 |
`a?`, |
| 18 |
`a|b`, |
| 19 |
`a*|b*`, |
| 20 |
`(a*|b)(c*|d)`, |
| 21 |
`[a-z]`, |
| 22 |
`[a-abc-c\-\]\[]`, |
| 23 |
`[a-z]+`, |
| 24 |
`[abc]`, |
| 25 |
`[^1234]`, |
| 26 |
`[^\n]`, |
| 27 |
`\!\\`, |
| 28 |
} |
| 29 |
|
| 30 |
type stringError struct { |
| 31 |
re string |
| 32 |
err string |
| 33 |
} |
| 34 |
|
| 35 |
var badRe = []stringError{ |
| 36 |
{`*`, "missing argument to repetition operator: `*`"}, |
| 37 |
{`+`, "missing argument to repetition operator: `+`"}, |
| 38 |
{`?`, "missing argument to repetition operator: `?`"}, |
| 39 |
{`(abc`, "missing closing ): `(abc`"}, |
| 40 |
{`abc)`, "unexpected ): `abc)`"}, |
| 41 |
{`x[a-z`, "missing closing ]: `[a-z`"}, |
| 42 |
{`[z-a]`, "invalid character class range: `z-a`"}, |
| 43 |
{`abc\`, "trailing backslash at end of expression"}, |
| 44 |
{`a**`, "invalid nested repetition operator: `**`"}, |
| 45 |
{`a*+`, "invalid nested repetition operator: `*+`"}, |
| 46 |
{`\x`, "invalid escape sequence: `\\x`"}, |
| 47 |
{strings.Repeat(`\pL`, 27000), "expression too large"}, |
| 48 |
} |
| 49 |
|
| 50 |
func compileTest(t *testing.T, expr string, error string) *Regexp { |
| 51 |
re, err := Compile(expr) |
| 52 |
if error == "" && err != nil { |
| 53 |
t.Error("compiling `", expr, "`; unexpected error: ", err.Error()) |
| 54 |
} |
| 55 |
if error != "" && err == nil { |
| 56 |
t.Error("compiling `", expr, "`; missing error") |
| 57 |
} else if error != "" && !strings.Contains(err.Error(), error) { |
| 58 |
t.Error("compiling `", expr, "`; wrong error: ", err.Error(), "; want ", error) |
| 59 |
} |
| 60 |
return re |
| 61 |
} |
| 62 |
|
| 63 |
func TestGoodCompile(t *testing.T) { |
| 64 |
for i := range goodRe { |
| 65 |
compileTest(t, goodRe[i], "") |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func TestBadCompile(t *testing.T) { |
| 70 |
for i := range badRe { |
| 71 |
compileTest(t, badRe[i].re, badRe[i].err) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func matchTest(t *testing.T, test *FindTest) { |
| 76 |
re := compileTest(t, test.pat, "") |
| 77 |
if re == nil { |
| 78 |
return |
| 79 |
} |
| 80 |
m := re.MatchString(test.text) |
| 81 |
if m != (len(test.matches) > 0) { |
| 82 |
t.Errorf("MatchString failure on %s: %t should be %t", test, m, len(test.matches) > 0) |
| 83 |
} |
| 84 |
|
| 85 |
m = re.Match([]byte(test.text)) |
| 86 |
if m != (len(test.matches) > 0) { |
| 87 |
t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func TestMatch(t *testing.T) { |
| 92 |
for _, test := range findTests { |
| 93 |
matchTest(t, &test) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func matchFunctionTest(t *testing.T, test *FindTest) { |
| 98 |
m, err := MatchString(test.pat, test.text) |
| 99 |
if err == nil { |
| 100 |
return |
| 101 |
} |
| 102 |
if m != (len(test.matches) > 0) { |
| 103 |
t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func TestMatchFunction(t *testing.T) { |
| 108 |
for _, test := range findTests { |
| 109 |
matchFunctionTest(t, &test) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func copyMatchTest(t *testing.T, test *FindTest) { |
| 114 |
re := compileTest(t, test.pat, "") |
| 115 |
if re == nil { |
| 116 |
return |
| 117 |
} |
| 118 |
m1 := re.MatchString(test.text) |
| 119 |
m2 := re.Copy().MatchString(test.text) |
| 120 |
if m1 != m2 { |
| 121 |
t.Errorf("Copied Regexp match failure on %s: original gave %t; copy gave %t; should be %t", |
| 122 |
test, m1, m2, len(test.matches) > 0) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func TestCopyMatch(t *testing.T) { |
| 127 |
for _, test := range findTests { |
| 128 |
copyMatchTest(t, &test) |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
type ReplaceTest struct { |
| 133 |
pattern, replacement, input, output string |
| 134 |
} |
| 135 |
|
| 136 |
var replaceTests = []ReplaceTest{ |
| 137 |
|
| 138 |
{"", "", "", ""}, |
| 139 |
{"", "x", "", "x"}, |
| 140 |
{"", "", "abc", "abc"}, |
| 141 |
{"", "x", "abc", "xaxbxcx"}, |
| 142 |
|
| 143 |
|
| 144 |
{"b", "", "", ""}, |
| 145 |
{"b", "x", "", ""}, |
| 146 |
{"b", "", "abc", "ac"}, |
| 147 |
{"b", "x", "abc", "axc"}, |
| 148 |
{"y", "", "", ""}, |
| 149 |
{"y", "x", "", ""}, |
| 150 |
{"y", "", "abc", "abc"}, |
| 151 |
{"y", "x", "abc", "abc"}, |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
{"[a-c]*", "x", "\u65e5", "x\u65e5x"}, |
| 156 |
{"[^\u65e5]", "x", "abc\u65e5def", "xxx\u65e5xxx"}, |
| 157 |
|
| 158 |
|
| 159 |
{"^[a-c]*", "x", "abcdabc", "xdabc"}, |
| 160 |
{"[a-c]*$", "x", "abcdabc", "abcdx"}, |
| 161 |
{"^[a-c]*$", "x", "abcdabc", "abcdabc"}, |
| 162 |
{"^[a-c]*", "x", "abc", "x"}, |
| 163 |
{"[a-c]*$", "x", "abc", "x"}, |
| 164 |
{"^[a-c]*$", "x", "abc", "x"}, |
| 165 |
{"^[a-c]*", "x", "dabce", "xdabce"}, |
| 166 |
{"[a-c]*$", "x", "dabce", "dabcex"}, |
| 167 |
{"^[a-c]*$", "x", "dabce", "dabce"}, |
| 168 |
{"^[a-c]*", "x", "", "x"}, |
| 169 |
{"[a-c]*$", "x", "", "x"}, |
| 170 |
{"^[a-c]*$", "x", "", "x"}, |
| 171 |
|
| 172 |
{"^[a-c]+", "x", "abcdabc", "xdabc"}, |
| 173 |
{"[a-c]+$", "x", "abcdabc", "abcdx"}, |
| 174 |
{"^[a-c]+$", "x", "abcdabc", "abcdabc"}, |
| 175 |
{"^[a-c]+", "x", "abc", "x"}, |
| 176 |
{"[a-c]+$", "x", "abc", "x"}, |
| 177 |
{"^[a-c]+$", "x", "abc", "x"}, |
| 178 |
{"^[a-c]+", "x", "dabce", "dabce"}, |
| 179 |
{"[a-c]+$", "x", "dabce", "dabce"}, |
| 180 |
{"^[a-c]+$", "x", "dabce", "dabce"}, |
| 181 |
{"^[a-c]+", "x", "", ""}, |
| 182 |
{"[a-c]+$", "x", "", ""}, |
| 183 |
{"^[a-c]+$", "x", "", ""}, |
| 184 |
|
| 185 |
|
| 186 |
{"abc", "def", "abcdefg", "defdefg"}, |
| 187 |
{"bc", "BC", "abcbcdcdedef", "aBCBCdcdedef"}, |
| 188 |
{"abc", "", "abcdabc", "d"}, |
| 189 |
{"x", "xXx", "xxxXxxx", "xXxxXxxXxXxXxxXxxXx"}, |
| 190 |
{"abc", "d", "", ""}, |
| 191 |
{"abc", "d", "abc", "d"}, |
| 192 |
{".+", "x", "abc", "x"}, |
| 193 |
{"[a-c]*", "x", "def", "xdxexfx"}, |
| 194 |
{"[a-c]+", "x", "abcbcdcdedef", "xdxdedef"}, |
| 195 |
{"[a-c]*", "x", "abcbcdcdedef", "xdxdxexdxexfx"}, |
| 196 |
|
| 197 |
|
| 198 |
{"a+", "($0)", "banana", "b(a)n(a)n(a)"}, |
| 199 |
{"a+", "(${0})", "banana", "b(a)n(a)n(a)"}, |
| 200 |
{"a+", "(${0})$0", "banana", "b(a)an(a)an(a)a"}, |
| 201 |
{"a+", "(${0})$0", "banana", "b(a)an(a)an(a)a"}, |
| 202 |
{"hello, (.+)", "goodbye, ${1}", "hello, world", "goodbye, world"}, |
| 203 |
{"hello, (.+)", "goodbye, $1x", "hello, world", "goodbye, "}, |
| 204 |
{"hello, (.+)", "goodbye, ${1}x", "hello, world", "goodbye, worldx"}, |
| 205 |
{"hello, (.+)", "<$0><$1><$2><$3>", "hello, world", "<hello, world><world><><>"}, |
| 206 |
{"hello, (?P<noun>.+)", "goodbye, $noun!", "hello, world", "goodbye, world!"}, |
| 207 |
{"hello, (?P<noun>.+)", "goodbye, ${noun}", "hello, world", "goodbye, world"}, |
| 208 |
{"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "hi", "hihihi"}, |
| 209 |
{"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "bye", "byebyebye"}, |
| 210 |
{"(?P<x>hi)|(?P<x>bye)", "$xyz", "hi", ""}, |
| 211 |
{"(?P<x>hi)|(?P<x>bye)", "${x}yz", "hi", "hiyz"}, |
| 212 |
{"(?P<x>hi)|(?P<x>bye)", "hello $$x", "hi", "hello $x"}, |
| 213 |
{"a+", "${oops", "aaa", "${oops"}, |
| 214 |
{"a+", "$$", "aaa", "$"}, |
| 215 |
{"a+", "$", "aaa", "$"}, |
| 216 |
|
| 217 |
|
| 218 |
{"(x)?", "$1", "123", "123"}, |
| 219 |
{"abc", "$1", "123", "123"}, |
| 220 |
|
| 221 |
|
| 222 |
{"(a)(b){0}(c)", ".$1|$3.", "xacxacx", "x.a|c.x.a|c.x"}, |
| 223 |
{"(a)(((b))){0}c", ".$1.", "xacxacx", "x.a.x.a.x"}, |
| 224 |
{"((a(b){0}){3}){5}(h)", "y caramb$2", "say aaaaaaaaaaaaaaaah", "say ay caramba"}, |
| 225 |
{"((a(b){0}){3}){5}h", "y caramb$2", "say aaaaaaaaaaaaaaaah", "say ay caramba"}, |
| 226 |
} |
| 227 |
|
| 228 |
var replaceLiteralTests = []ReplaceTest{ |
| 229 |
|
| 230 |
{"a+", "($0)", "banana", "b($0)n($0)n($0)"}, |
| 231 |
{"a+", "(${0})", "banana", "b(${0})n(${0})n(${0})"}, |
| 232 |
{"a+", "(${0})$0", "banana", "b(${0})$0n(${0})$0n(${0})$0"}, |
| 233 |
{"a+", "(${0})$0", "banana", "b(${0})$0n(${0})$0n(${0})$0"}, |
| 234 |
{"hello, (.+)", "goodbye, ${1}", "hello, world", "goodbye, ${1}"}, |
| 235 |
{"hello, (?P<noun>.+)", "goodbye, $noun!", "hello, world", "goodbye, $noun!"}, |
| 236 |
{"hello, (?P<noun>.+)", "goodbye, ${noun}", "hello, world", "goodbye, ${noun}"}, |
| 237 |
{"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "hi", "$x$x$x"}, |
| 238 |
{"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "bye", "$x$x$x"}, |
| 239 |
{"(?P<x>hi)|(?P<x>bye)", "$xyz", "hi", "$xyz"}, |
| 240 |
{"(?P<x>hi)|(?P<x>bye)", "${x}yz", "hi", "${x}yz"}, |
| 241 |
{"(?P<x>hi)|(?P<x>bye)", "hello $$x", "hi", "hello $$x"}, |
| 242 |
{"a+", "${oops", "aaa", "${oops"}, |
| 243 |
{"a+", "$$", "aaa", "$$"}, |
| 244 |
{"a+", "$", "aaa", "$"}, |
| 245 |
} |
| 246 |
|
| 247 |
type ReplaceFuncTest struct { |
| 248 |
pattern string |
| 249 |
replacement func(string) string |
| 250 |
input, output string |
| 251 |
} |
| 252 |
|
| 253 |
var replaceFuncTests = []ReplaceFuncTest{ |
| 254 |
{"[a-c]", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxayxbyxcydef"}, |
| 255 |
{"[a-c]+", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxabcydef"}, |
| 256 |
{"[a-c]*", func(s string) string { return "x" + s + "y" }, "defabcdef", "xydxyexyfxabcydxyexyfxy"}, |
| 257 |
} |
| 258 |
|
| 259 |
func TestReplaceAll(t *testing.T) { |
| 260 |
for _, tc := range replaceTests { |
| 261 |
re, err := Compile(tc.pattern) |
| 262 |
if err != nil { |
| 263 |
t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err) |
| 264 |
continue |
| 265 |
} |
| 266 |
actual := re.ReplaceAllString(tc.input, tc.replacement) |
| 267 |
if actual != tc.output { |
| 268 |
t.Errorf("%q.ReplaceAllString(%q,%q) = %q; want %q", |
| 269 |
tc.pattern, tc.input, tc.replacement, actual, tc.output) |
| 270 |
} |
| 271 |
|
| 272 |
actual = string(re.ReplaceAll([]byte(tc.input), []byte(tc.replacement))) |
| 273 |
if actual != tc.output { |
| 274 |
t.Errorf("%q.ReplaceAll(%q,%q) = %q; want %q", |
| 275 |
tc.pattern, tc.input, tc.replacement, actual, tc.output) |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
func TestReplaceAllLiteral(t *testing.T) { |
| 281 |
|
| 282 |
for _, tc := range replaceTests { |
| 283 |
if strings.Contains(tc.replacement, "$") { |
| 284 |
continue |
| 285 |
} |
| 286 |
re, err := Compile(tc.pattern) |
| 287 |
if err != nil { |
| 288 |
t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err) |
| 289 |
continue |
| 290 |
} |
| 291 |
actual := re.ReplaceAllLiteralString(tc.input, tc.replacement) |
| 292 |
if actual != tc.output { |
| 293 |
t.Errorf("%q.ReplaceAllLiteralString(%q,%q) = %q; want %q", |
| 294 |
tc.pattern, tc.input, tc.replacement, actual, tc.output) |
| 295 |
} |
| 296 |
|
| 297 |
actual = string(re.ReplaceAllLiteral([]byte(tc.input), []byte(tc.replacement))) |
| 298 |
if actual != tc.output { |
| 299 |
t.Errorf("%q.ReplaceAllLiteral(%q,%q) = %q; want %q", |
| 300 |
tc.pattern, tc.input, tc.replacement, actual, tc.output) |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
for _, tc := range replaceLiteralTests { |
| 306 |
re, err := Compile(tc.pattern) |
| 307 |
if err != nil { |
| 308 |
t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err) |
| 309 |
continue |
| 310 |
} |
| 311 |
actual := re.ReplaceAllLiteralString(tc.input, tc.replacement) |
| 312 |
if actual != tc.output { |
| 313 |
t.Errorf("%q.ReplaceAllLiteralString(%q,%q) = %q; want %q", |
| 314 |
tc.pattern, tc.input, tc.replacement, actual, tc.output) |
| 315 |
} |
| 316 |
|
| 317 |
actual = string(re.ReplaceAllLiteral([]byte(tc.input), []byte(tc.replacement))) |
| 318 |
if actual != tc.output { |
| 319 |
t.Errorf("%q.ReplaceAllLiteral(%q,%q) = %q; want %q", |
| 320 |
tc.pattern, tc.input, tc.replacement, actual, tc.output) |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
func TestReplaceAllFunc(t *testing.T) { |
| 326 |
for _, tc := range replaceFuncTests { |
| 327 |
re, err := Compile(tc.pattern) |
| 328 |
if err != nil { |
| 329 |
t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err) |
| 330 |
continue |
| 331 |
} |
| 332 |
actual := re.ReplaceAllStringFunc(tc.input, tc.replacement) |
| 333 |
if actual != tc.output { |
| 334 |
t.Errorf("%q.ReplaceFunc(%q,fn) = %q; want %q", |
| 335 |
tc.pattern, tc.input, actual, tc.output) |
| 336 |
} |
| 337 |
|
| 338 |
actual = string(re.ReplaceAllFunc([]byte(tc.input), func(s []byte) []byte { return []byte(tc.replacement(string(s))) })) |
| 339 |
if actual != tc.output { |
| 340 |
t.Errorf("%q.ReplaceFunc(%q,fn) = %q; want %q", |
| 341 |
tc.pattern, tc.input, actual, tc.output) |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
type MetaTest struct { |
| 347 |
pattern, output, literal string |
| 348 |
isLiteral bool |
| 349 |
} |
| 350 |
|
| 351 |
var metaTests = []MetaTest{ |
| 352 |
{``, ``, ``, true}, |
| 353 |
{`foo`, `foo`, `foo`, true}, |
| 354 |
{`日本語+`, `日本語\+`, `日本語`, false}, |
| 355 |
{`foo\.\$`, `foo\\\.\\\$`, `foo.$`, true}, |
| 356 |
{`foo.\$`, `foo\.\\\$`, `foo`, false}, |
| 357 |
{`!@#$%^&*()_+-=[{]}\|,<.>/?~`, `!@#\$%\^&\*\(\)_\+-=\[\{\]\}\\\|,<\.>/\?~`, `!@#`, false}, |
| 358 |
} |
| 359 |
|
| 360 |
func TestQuoteMeta(t *testing.T) { |
| 361 |
for _, tc := range metaTests { |
| 362 |
|
| 363 |
quoted := QuoteMeta(tc.pattern) |
| 364 |
if quoted != tc.output { |
| 365 |
t.Errorf("QuoteMeta(`%s`) = `%s`; want `%s`", |
| 366 |
tc.pattern, quoted, tc.output) |
| 367 |
continue |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
if tc.pattern != "" { |
| 373 |
re, err := Compile(quoted) |
| 374 |
if err != nil { |
| 375 |
t.Errorf("Unexpected error compiling QuoteMeta(`%s`): %v", tc.pattern, err) |
| 376 |
continue |
| 377 |
} |
| 378 |
src := "abc" + tc.pattern + "def" |
| 379 |
repl := "xyz" |
| 380 |
replaced := re.ReplaceAllString(src, repl) |
| 381 |
expected := "abcxyzdef" |
| 382 |
if replaced != expected { |
| 383 |
t.Errorf("QuoteMeta(`%s`).Replace(`%s`,`%s`) = `%s`; want `%s`", |
| 384 |
tc.pattern, src, repl, replaced, expected) |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
type subexpIndex struct { |
| 391 |
name string |
| 392 |
index int |
| 393 |
} |
| 394 |
|
| 395 |
type subexpCase struct { |
| 396 |
input string |
| 397 |
num int |
| 398 |
names []string |
| 399 |
indices []subexpIndex |
| 400 |
} |
| 401 |
|
| 402 |
var emptySubexpIndices = []subexpIndex{{"", -1}, {"missing", -1}} |
| 403 |
|
| 404 |
var subexpCases = []subexpCase{ |
| 405 |
{``, 0, nil, emptySubexpIndices}, |
| 406 |
{`.*`, 0, nil, emptySubexpIndices}, |
| 407 |
{`abba`, 0, nil, emptySubexpIndices}, |
| 408 |
{`ab(b)a`, 1, []string{"", ""}, emptySubexpIndices}, |
| 409 |
{`ab(.*)a`, 1, []string{"", ""}, emptySubexpIndices}, |
| 410 |
{`(.*)ab(.*)a`, 2, []string{"", "", ""}, emptySubexpIndices}, |
| 411 |
{`(.*)(ab)(.*)a`, 3, []string{"", "", "", ""}, emptySubexpIndices}, |
| 412 |
{`(.*)((a)b)(.*)a`, 4, []string{"", "", "", "", ""}, emptySubexpIndices}, |
| 413 |
{`(.*)(\(ab)(.*)a`, 3, []string{"", "", "", ""}, emptySubexpIndices}, |
| 414 |
{`(.*)(\(a\)b)(.*)a`, 3, []string{"", "", "", ""}, emptySubexpIndices}, |
| 415 |
{`(?P<foo>.*)(?P<bar>(a)b)(?P<foo>.*)a`, 4, []string{"", "foo", "bar", "", "foo"}, []subexpIndex{{"", -1}, {"missing", -1}, {"foo", 1}, {"bar", 2}}}, |
| 416 |
} |
| 417 |
|
| 418 |
func TestSubexp(t *testing.T) { |
| 419 |
for _, c := range subexpCases { |
| 420 |
re := MustCompile(c.input) |
| 421 |
n := re.NumSubexp() |
| 422 |
if n != c.num { |
| 423 |
t.Errorf("%q: NumSubexp = %d, want %d", c.input, n, c.num) |
| 424 |
continue |
| 425 |
} |
| 426 |
names := re.SubexpNames() |
| 427 |
if len(names) != 1+n { |
| 428 |
t.Errorf("%q: len(SubexpNames) = %d, want %d", c.input, len(names), n) |
| 429 |
continue |
| 430 |
} |
| 431 |
if c.names != nil { |
| 432 |
for i := range 1 + n { |
| 433 |
if names[i] != c.names[i] { |
| 434 |
t.Errorf("%q: SubexpNames[%d] = %q, want %q", c.input, i, names[i], c.names[i]) |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
for _, subexp := range c.indices { |
| 439 |
index := re.SubexpIndex(subexp.name) |
| 440 |
if index != subexp.index { |
| 441 |
t.Errorf("%q: SubexpIndex(%q) = %d, want %d", c.input, subexp.name, index, subexp.index) |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
var splitTests = []struct { |
| 448 |
s string |
| 449 |
r string |
| 450 |
n int |
| 451 |
out []string |
| 452 |
}{ |
| 453 |
{"foo:and:bar", ":", -1, []string{"foo", "and", "bar"}}, |
| 454 |
{"foo:and:bar", ":", 1, []string{"foo:and:bar"}}, |
| 455 |
{"foo:and:bar", ":", 2, []string{"foo", "and:bar"}}, |
| 456 |
{"foo:and:bar", "foo", -1, []string{"", ":and:bar"}}, |
| 457 |
{"foo:and:bar", "bar", -1, []string{"foo:and:", ""}}, |
| 458 |
{"foo:and:bar", "baz", -1, []string{"foo:and:bar"}}, |
| 459 |
{"baabaab", "a", -1, []string{"b", "", "b", "", "b"}}, |
| 460 |
{"baabaab", "a*", -1, []string{"b", "b", "b"}}, |
| 461 |
{"baabaab", "ba*", -1, []string{"", "", "", ""}}, |
| 462 |
{"foobar", "f*b*", -1, []string{"", "o", "o", "a", "r"}}, |
| 463 |
{"foobar", "f+.*b+", -1, []string{"", "ar"}}, |
| 464 |
{"foobooboar", "o{2}", -1, []string{"f", "b", "boar"}}, |
| 465 |
{"a,b,c,d,e,f", ",", 3, []string{"a", "b", "c,d,e,f"}}, |
| 466 |
{"a,b,c,d,e,f", ",", 0, nil}, |
| 467 |
{",", ",", -1, []string{"", ""}}, |
| 468 |
{",,,", ",", -1, []string{"", "", "", ""}}, |
| 469 |
{"", ",", -1, []string{""}}, |
| 470 |
{"", ".*", -1, []string{""}}, |
| 471 |
{"", ".+", -1, []string{""}}, |
| 472 |
{"", "", -1, []string{}}, |
| 473 |
{"foobar", "", -1, []string{"f", "o", "o", "b", "a", "r"}}, |
| 474 |
{"abaabaccadaaae", "a*", 5, []string{"", "b", "b", "c", "cadaaae"}}, |
| 475 |
{":x:y:z:", ":", -1, []string{"", "x", "y", "z", ""}}, |
| 476 |
} |
| 477 |
|
| 478 |
func TestSplit(t *testing.T) { |
| 479 |
for i, test := range splitTests { |
| 480 |
re, err := Compile(test.r) |
| 481 |
if err != nil { |
| 482 |
t.Errorf("#%d: %q: compile error: %s", i, test.r, err.Error()) |
| 483 |
continue |
| 484 |
} |
| 485 |
|
| 486 |
split := re.Split(test.s, test.n) |
| 487 |
if !reflect.DeepEqual(split, test.out) { |
| 488 |
t.Errorf("#%d: %q: got %q; want %q", i, test.r, split, test.out) |
| 489 |
} |
| 490 |
|
| 491 |
if QuoteMeta(test.r) == test.r { |
| 492 |
strsplit := strings.SplitN(test.s, test.r, test.n) |
| 493 |
if !reflect.DeepEqual(split, strsplit) { |
| 494 |
t.Errorf("#%d: Split(%q, %q, %d): regexp vs strings mismatch\nregexp=%q\nstrings=%q", i, test.s, test.r, test.n, split, strsplit) |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
func TestParseAndCompile(t *testing.T) { |
| 502 |
expr := "a$" |
| 503 |
s := "a\nb" |
| 504 |
|
| 505 |
for i, tc := range []struct { |
| 506 |
reFlags syntax.Flags |
| 507 |
expMatch bool |
| 508 |
}{ |
| 509 |
{syntax.Perl | syntax.OneLine, false}, |
| 510 |
{syntax.Perl &^ syntax.OneLine, true}, |
| 511 |
} { |
| 512 |
parsed, err := syntax.Parse(expr, tc.reFlags) |
| 513 |
if err != nil { |
| 514 |
t.Fatalf("%d: parse: %v", i, err) |
| 515 |
} |
| 516 |
re, err := Compile(parsed.String()) |
| 517 |
if err != nil { |
| 518 |
t.Fatalf("%d: compile: %v", i, err) |
| 519 |
} |
| 520 |
if match := re.MatchString(s); match != tc.expMatch { |
| 521 |
t.Errorf("%d: %q.MatchString(%q)=%t; expected=%t", i, re, s, match, tc.expMatch) |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
func TestSwitchBacktrack(t *testing.T) { |
| 529 |
re := MustCompile(`a|b`) |
| 530 |
long := make([]byte, maxBacktrackVector+1) |
| 531 |
|
| 532 |
|
| 533 |
re.Match(long) |
| 534 |
re.Match(long[:1]) |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|