blob: 01ea3742a8b47bf0f09ed77cf17e27160651d262 [file] [log] [blame]
Rob Pike8f5b2772008-10-09 19:40:53 -07001// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Rob Pikebe7e0f82008-11-19 15:38:46 -08005package regexp
Rob Pike8f5b2772008-10-09 19:40:53 -07006
7import (
Rick Arnold94b3f6d2012-11-27 12:58:27 -05008 "reflect"
Russ Cox22be4bf2014-10-20 12:16:46 -04009 "regexp/syntax"
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080010 "strings"
11 "testing"
Rob Pike8f5b2772008-10-09 19:40:53 -070012)
13
Russ Coxbe2edb52009-03-03 08:39:12 -080014var good_re = []string{
Rob Pike0b05e912008-10-14 17:45:49 -070015 ``,
16 `.`,
17 `^.$`,
18 `a`,
19 `a*`,
20 `a+`,
21 `a?`,
22 `a|b`,
23 `a*|b*`,
24 `(a*|b)(c*|d)`,
25 `[a-z]`,
26 `[a-abc-c\-\]\[]`,
27 `[a-z]+`,
Rob Pike0b05e912008-10-14 17:45:49 -070028 `[abc]`,
29 `[^1234]`,
Rob Pike5a4d4312009-08-05 14:40:34 -070030 `[^\n]`,
Russ Cox6f33f342010-04-26 10:00:18 -070031 `\!\\`,
Russ Coxbe2edb52009-03-03 08:39:12 -080032}
Rob Pike75df21c2008-10-14 16:32:43 -070033
Rob Pike74a60ed2009-01-15 17:22:15 -080034type stringError struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080035 re string
Russ Cox3c6c8832012-12-11 12:19:39 -050036 err string
Rob Pike75df21c2008-10-14 16:32:43 -070037}
Russ Cox91549432009-10-07 11:55:06 -070038
Russ Coxbe2edb52009-03-03 08:39:12 -080039var bad_re = []stringError{
Russ Cox3c6c8832012-12-11 12:19:39 -050040 {`*`, "missing argument to repetition operator: `*`"},
41 {`+`, "missing argument to repetition operator: `+`"},
42 {`?`, "missing argument to repetition operator: `?`"},
43 {`(abc`, "missing closing ): `(abc`"},
44 {`abc)`, "unexpected ): `abc)`"},
45 {`x[a-z`, "missing closing ]: `[a-z`"},
46 {`[z-a]`, "invalid character class range: `z-a`"},
47 {`abc\`, "trailing backslash at end of expression"},
48 {`a**`, "invalid nested repetition operator: `**`"},
49 {`a*+`, "invalid nested repetition operator: `*+`"},
50 {`\x`, "invalid escape sequence: `\\x`"},
Russ Coxbe2edb52009-03-03 08:39:12 -080051}
Rob Pike75df21c2008-10-14 16:32:43 -070052
Russ Cox3c6c8832012-12-11 12:19:39 -050053func compileTest(t *testing.T, expr string, error string) *Regexp {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080054 re, err := Compile(expr)
Russ Cox3c6c8832012-12-11 12:19:39 -050055 if error == "" && err != nil {
Russ Coxeb692922011-11-01 22:05:34 -040056 t.Error("compiling `", expr, "`; unexpected error: ", err.Error())
Rob Pike8f5b2772008-10-09 19:40:53 -070057 }
Russ Cox3c6c8832012-12-11 12:19:39 -050058 if error != "" && err == nil {
59 t.Error("compiling `", expr, "`; missing error")
60 } else if error != "" && !strings.Contains(err.Error(), error) {
61 t.Error("compiling `", expr, "`; wrong error: ", err.Error(), "; want ", error)
62 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080063 return re
Rob Pike75df21c2008-10-14 16:32:43 -070064}
65
Russ Cox839a6842009-01-20 14:40:40 -080066func TestGoodCompile(t *testing.T) {
Rob Pike75df21c2008-10-14 16:32:43 -070067 for i := 0; i < len(good_re); i++ {
Russ Cox3c6c8832012-12-11 12:19:39 -050068 compileTest(t, good_re[i], "")
Rob Pike75df21c2008-10-14 16:32:43 -070069 }
Rob Pikebe7e0f82008-11-19 15:38:46 -080070}
71
Russ Cox839a6842009-01-20 14:40:40 -080072func TestBadCompile(t *testing.T) {
Rob Pike75df21c2008-10-14 16:32:43 -070073 for i := 0; i < len(bad_re); i++ {
Robert Griesemer40621d52009-11-09 12:07:39 -080074 compileTest(t, bad_re[i].re, bad_re[i].err)
Rob Pike75df21c2008-10-14 16:32:43 -070075 }
Rob Pikebe7e0f82008-11-19 15:38:46 -080076}
77
Rob Pike079a1172010-08-12 17:16:37 +100078func matchTest(t *testing.T, test *FindTest) {
Russ Cox3c6c8832012-12-11 12:19:39 -050079 re := compileTest(t, test.pat, "")
Rob Pike1da03aa2009-01-06 13:54:53 -080080 if re == nil {
Robert Griesemer40621d52009-11-09 12:07:39 -080081 return
Rob Pike1da03aa2009-01-06 13:54:53 -080082 }
Rob Pike079a1172010-08-12 17:16:37 +100083 m := re.MatchString(test.text)
84 if m != (len(test.matches) > 0) {
85 t.Errorf("MatchString failure on %s: %t should be %t", test, m, len(test.matches) > 0)
Rob Pike3355cad2009-08-05 15:44:45 -070086 }
87 // now try bytes
Rob Pike079a1172010-08-12 17:16:37 +100088 m = re.Match([]byte(test.text))
89 if m != (len(test.matches) > 0) {
90 t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0)
Rob Pike1da03aa2009-01-06 13:54:53 -080091 }
92}
93
Russ Cox839a6842009-01-20 14:40:40 -080094func TestMatch(t *testing.T) {
Rob Pike079a1172010-08-12 17:16:37 +100095 for _, test := range findTests {
96 matchTest(t, &test)
Rob Pike75df21c2008-10-14 16:32:43 -070097 }
Rob Pike8f5b2772008-10-09 19:40:53 -070098}
Rob Pike1da03aa2009-01-06 13:54:53 -080099
Rob Pike079a1172010-08-12 17:16:37 +1000100func matchFunctionTest(t *testing.T, test *FindTest) {
101 m, err := MatchString(test.pat, test.text)
Rob Pike1da03aa2009-01-06 13:54:53 -0800102 if err == nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800103 return
Rob Pike1da03aa2009-01-06 13:54:53 -0800104 }
Rob Pike079a1172010-08-12 17:16:37 +1000105 if m != (len(test.matches) > 0) {
106 t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0)
Rob Pike1da03aa2009-01-06 13:54:53 -0800107 }
108}
109
Russ Cox839a6842009-01-20 14:40:40 -0800110func TestMatchFunction(t *testing.T) {
Rob Pike079a1172010-08-12 17:16:37 +1000111 for _, test := range findTests {
112 matchFunctionTest(t, &test)
Rob Pike1da03aa2009-01-06 13:54:53 -0800113 }
114}
Steve Newmana6c7a802009-06-18 17:55:47 -0700115
116type ReplaceTest struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800117 pattern, replacement, input, output string
Steve Newmana6c7a802009-06-18 17:55:47 -0700118}
119
Russ Cox91549432009-10-07 11:55:06 -0700120var replaceTests = []ReplaceTest{
Steve Newmana6c7a802009-06-18 17:55:47 -0700121 // Test empty input and/or replacement, with pattern that matches the empty string.
Robert Griesemer34788912010-10-22 10:06:33 -0700122 {"", "", "", ""},
123 {"", "x", "", "x"},
124 {"", "", "abc", "abc"},
125 {"", "x", "abc", "xaxbxcx"},
Steve Newmana6c7a802009-06-18 17:55:47 -0700126
127 // Test empty input and/or replacement, with pattern that does not match the empty string.
Robert Griesemer34788912010-10-22 10:06:33 -0700128 {"b", "", "", ""},
129 {"b", "x", "", ""},
130 {"b", "", "abc", "ac"},
131 {"b", "x", "abc", "axc"},
132 {"y", "", "", ""},
133 {"y", "x", "", ""},
134 {"y", "", "abc", "abc"},
135 {"y", "x", "abc", "abc"},
Steve Newmana6c7a802009-06-18 17:55:47 -0700136
137 // Multibyte characters -- verify that we don't try to match in the middle
138 // of a character.
Robert Griesemer34788912010-10-22 10:06:33 -0700139 {"[a-c]*", "x", "\u65e5", "x\u65e5x"},
140 {"[^\u65e5]", "x", "abc\u65e5def", "xxx\u65e5xxx"},
Steve Newmana6c7a802009-06-18 17:55:47 -0700141
142 // Start and end of a string.
Robert Griesemer34788912010-10-22 10:06:33 -0700143 {"^[a-c]*", "x", "abcdabc", "xdabc"},
144 {"[a-c]*$", "x", "abcdabc", "abcdx"},
145 {"^[a-c]*$", "x", "abcdabc", "abcdabc"},
146 {"^[a-c]*", "x", "abc", "x"},
147 {"[a-c]*$", "x", "abc", "x"},
148 {"^[a-c]*$", "x", "abc", "x"},
149 {"^[a-c]*", "x", "dabce", "xdabce"},
150 {"[a-c]*$", "x", "dabce", "dabcex"},
151 {"^[a-c]*$", "x", "dabce", "dabce"},
152 {"^[a-c]*", "x", "", "x"},
153 {"[a-c]*$", "x", "", "x"},
154 {"^[a-c]*$", "x", "", "x"},
Steve Newmana6c7a802009-06-18 17:55:47 -0700155
Robert Griesemer34788912010-10-22 10:06:33 -0700156 {"^[a-c]+", "x", "abcdabc", "xdabc"},
157 {"[a-c]+$", "x", "abcdabc", "abcdx"},
158 {"^[a-c]+$", "x", "abcdabc", "abcdabc"},
159 {"^[a-c]+", "x", "abc", "x"},
160 {"[a-c]+$", "x", "abc", "x"},
161 {"^[a-c]+$", "x", "abc", "x"},
162 {"^[a-c]+", "x", "dabce", "dabce"},
163 {"[a-c]+$", "x", "dabce", "dabce"},
164 {"^[a-c]+$", "x", "dabce", "dabce"},
165 {"^[a-c]+", "x", "", ""},
166 {"[a-c]+$", "x", "", ""},
167 {"^[a-c]+$", "x", "", ""},
Steve Newmana6c7a802009-06-18 17:55:47 -0700168
169 // Other cases.
Robert Griesemer34788912010-10-22 10:06:33 -0700170 {"abc", "def", "abcdefg", "defdefg"},
171 {"bc", "BC", "abcbcdcdedef", "aBCBCdcdedef"},
172 {"abc", "", "abcdabc", "d"},
173 {"x", "xXx", "xxxXxxx", "xXxxXxxXxXxXxxXxxXx"},
174 {"abc", "d", "", ""},
175 {"abc", "d", "abc", "d"},
176 {".+", "x", "abc", "x"},
177 {"[a-c]*", "x", "def", "xdxexfx"},
178 {"[a-c]+", "x", "abcbcdcdedef", "xdxdedef"},
179 {"[a-c]*", "x", "abcbcdcdedef", "xdxdxexdxexfx"},
Russ Cox7201ba22012-02-07 23:46:47 -0500180
181 // Substitutions
182 {"a+", "($0)", "banana", "b(a)n(a)n(a)"},
183 {"a+", "(${0})", "banana", "b(a)n(a)n(a)"},
184 {"a+", "(${0})$0", "banana", "b(a)an(a)an(a)a"},
185 {"a+", "(${0})$0", "banana", "b(a)an(a)an(a)a"},
186 {"hello, (.+)", "goodbye, ${1}", "hello, world", "goodbye, world"},
187 {"hello, (.+)", "goodbye, $1x", "hello, world", "goodbye, "},
188 {"hello, (.+)", "goodbye, ${1}x", "hello, world", "goodbye, worldx"},
189 {"hello, (.+)", "<$0><$1><$2><$3>", "hello, world", "<hello, world><world><><>"},
190 {"hello, (?P<noun>.+)", "goodbye, $noun!", "hello, world", "goodbye, world!"},
191 {"hello, (?P<noun>.+)", "goodbye, ${noun}", "hello, world", "goodbye, world"},
192 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "hi", "hihihi"},
193 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "bye", "byebyebye"},
194 {"(?P<x>hi)|(?P<x>bye)", "$xyz", "hi", ""},
195 {"(?P<x>hi)|(?P<x>bye)", "${x}yz", "hi", "hiyz"},
196 {"(?P<x>hi)|(?P<x>bye)", "hello $$x", "hi", "hello $x"},
197 {"a+", "${oops", "aaa", "${oops"},
198 {"a+", "$$", "aaa", "$"},
199 {"a+", "$", "aaa", "$"},
Erik St. Martin54b7ccd2012-12-22 11:14:56 -0500200
201 // Substitution when subexpression isn't found
202 {"(x)?", "$1", "123", "123"},
203 {"abc", "$1", "123", "123"},
Russ Cox7201ba22012-02-07 23:46:47 -0500204}
205
206var replaceLiteralTests = []ReplaceTest{
207 // Substitutions
208 {"a+", "($0)", "banana", "b($0)n($0)n($0)"},
209 {"a+", "(${0})", "banana", "b(${0})n(${0})n(${0})"},
210 {"a+", "(${0})$0", "banana", "b(${0})$0n(${0})$0n(${0})$0"},
211 {"a+", "(${0})$0", "banana", "b(${0})$0n(${0})$0n(${0})$0"},
212 {"hello, (.+)", "goodbye, ${1}", "hello, world", "goodbye, ${1}"},
213 {"hello, (?P<noun>.+)", "goodbye, $noun!", "hello, world", "goodbye, $noun!"},
214 {"hello, (?P<noun>.+)", "goodbye, ${noun}", "hello, world", "goodbye, ${noun}"},
215 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "hi", "$x$x$x"},
216 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "bye", "$x$x$x"},
217 {"(?P<x>hi)|(?P<x>bye)", "$xyz", "hi", "$xyz"},
218 {"(?P<x>hi)|(?P<x>bye)", "${x}yz", "hi", "${x}yz"},
219 {"(?P<x>hi)|(?P<x>bye)", "hello $$x", "hi", "hello $$x"},
220 {"a+", "${oops", "aaa", "${oops"},
221 {"a+", "$$", "aaa", "$$"},
222 {"a+", "$", "aaa", "$"},
Steve Newmana6c7a802009-06-18 17:55:47 -0700223}
224
Andrew Gerrandf25e0162010-03-07 12:41:49 +1100225type ReplaceFuncTest struct {
226 pattern string
227 replacement func(string) string
228 input, output string
229}
230
231var replaceFuncTests = []ReplaceFuncTest{
Robert Griesemer34788912010-10-22 10:06:33 -0700232 {"[a-c]", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxayxbyxcydef"},
233 {"[a-c]+", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxabcydef"},
234 {"[a-c]*", func(s string) string { return "x" + s + "y" }, "defabcdef", "xydxyexyfxabcydxyexyfxy"},
Andrew Gerrandf25e0162010-03-07 12:41:49 +1100235}
236
Steve Newmana6c7a802009-06-18 17:55:47 -0700237func TestReplaceAll(t *testing.T) {
Russ Coxca6a0fe2009-09-15 09:41:59 -0700238 for _, tc := range replaceTests {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800239 re, err := Compile(tc.pattern)
Steve Newmana6c7a802009-06-18 17:55:47 -0700240 if err != nil {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800241 t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
242 continue
Steve Newmana6c7a802009-06-18 17:55:47 -0700243 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800244 actual := re.ReplaceAllString(tc.input, tc.replacement)
Rob Pike3355cad2009-08-05 15:44:45 -0700245 if actual != tc.output {
Russ Cox7201ba22012-02-07 23:46:47 -0500246 t.Errorf("%q.ReplaceAllString(%q,%q) = %q; want %q",
Robert Griesemer40621d52009-11-09 12:07:39 -0800247 tc.pattern, tc.input, tc.replacement, actual, tc.output)
Rob Pike3355cad2009-08-05 15:44:45 -0700248 }
249 // now try bytes
Russ Cox9750adb2010-02-25 16:01:29 -0800250 actual = string(re.ReplaceAll([]byte(tc.input), []byte(tc.replacement)))
Steve Newmana6c7a802009-06-18 17:55:47 -0700251 if actual != tc.output {
Russ Cox7201ba22012-02-07 23:46:47 -0500252 t.Errorf("%q.ReplaceAll(%q,%q) = %q; want %q",
253 tc.pattern, tc.input, tc.replacement, actual, tc.output)
254 }
255 }
256}
257
258func TestReplaceAllLiteral(t *testing.T) {
259 // Run ReplaceAll tests that do not have $ expansions.
260 for _, tc := range replaceTests {
261 if strings.Contains(tc.replacement, "$") {
262 continue
263 }
264 re, err := Compile(tc.pattern)
265 if err != nil {
266 t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
267 continue
268 }
269 actual := re.ReplaceAllLiteralString(tc.input, tc.replacement)
270 if actual != tc.output {
271 t.Errorf("%q.ReplaceAllLiteralString(%q,%q) = %q; want %q",
272 tc.pattern, tc.input, tc.replacement, actual, tc.output)
273 }
274 // now try bytes
275 actual = string(re.ReplaceAllLiteral([]byte(tc.input), []byte(tc.replacement)))
276 if actual != tc.output {
277 t.Errorf("%q.ReplaceAllLiteral(%q,%q) = %q; want %q",
278 tc.pattern, tc.input, tc.replacement, actual, tc.output)
279 }
280 }
281
282 // Run literal-specific tests.
283 for _, tc := range replaceLiteralTests {
284 re, err := Compile(tc.pattern)
285 if err != nil {
286 t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
287 continue
288 }
289 actual := re.ReplaceAllLiteralString(tc.input, tc.replacement)
290 if actual != tc.output {
291 t.Errorf("%q.ReplaceAllLiteralString(%q,%q) = %q; want %q",
292 tc.pattern, tc.input, tc.replacement, actual, tc.output)
293 }
294 // now try bytes
295 actual = string(re.ReplaceAllLiteral([]byte(tc.input), []byte(tc.replacement)))
296 if actual != tc.output {
297 t.Errorf("%q.ReplaceAllLiteral(%q,%q) = %q; want %q",
Robert Griesemer40621d52009-11-09 12:07:39 -0800298 tc.pattern, tc.input, tc.replacement, actual, tc.output)
Steve Newmana6c7a802009-06-18 17:55:47 -0700299 }
300 }
301}
302
Andrew Gerrandf25e0162010-03-07 12:41:49 +1100303func TestReplaceAllFunc(t *testing.T) {
304 for _, tc := range replaceFuncTests {
305 re, err := Compile(tc.pattern)
306 if err != nil {
307 t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
308 continue
309 }
310 actual := re.ReplaceAllStringFunc(tc.input, tc.replacement)
311 if actual != tc.output {
Rob Pikefa7791e2013-09-27 10:09:15 +1000312 t.Errorf("%q.ReplaceFunc(%q,fn) = %q; want %q",
313 tc.pattern, tc.input, actual, tc.output)
Andrew Gerrandf25e0162010-03-07 12:41:49 +1100314 }
315 // now try bytes
316 actual = string(re.ReplaceAllFunc([]byte(tc.input), func(s []byte) []byte { return []byte(tc.replacement(string(s))) }))
317 if actual != tc.output {
Rob Pikefa7791e2013-09-27 10:09:15 +1000318 t.Errorf("%q.ReplaceFunc(%q,fn) = %q; want %q",
319 tc.pattern, tc.input, actual, tc.output)
Andrew Gerrandf25e0162010-03-07 12:41:49 +1100320 }
321 }
322}
323
Rob Pikea9e7c932010-12-17 10:23:46 -0800324type MetaTest struct {
325 pattern, output, literal string
326 isLiteral bool
Steve Newmana6c7a802009-06-18 17:55:47 -0700327}
328
Rob Pikea9e7c932010-12-17 10:23:46 -0800329var metaTests = []MetaTest{
330 {``, ``, ``, true},
331 {`foo`, `foo`, `foo`, true},
332 {`foo\.\$`, `foo\\\.\\\$`, `foo.$`, true}, // has meta but no operator
333 {`foo.\$`, `foo\.\\\$`, `foo`, false}, // has escaped operators and real operators
Russ Cox6c230fb2011-09-26 18:33:13 -0400334 {`!@#$%^&*()_+-=[{]}\|,<.>/?~`, `!@#\$%\^&\*\(\)_\+-=\[\{\]\}\\\|,<\.>/\?~`, `!@#`, false},
Steve Newmana6c7a802009-06-18 17:55:47 -0700335}
336
337func TestQuoteMeta(t *testing.T) {
Rob Pikea9e7c932010-12-17 10:23:46 -0800338 for _, tc := range metaTests {
Steve Newmana6c7a802009-06-18 17:55:47 -0700339 // Verify that QuoteMeta returns the expected string.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800340 quoted := QuoteMeta(tc.pattern)
Steve Newmana6c7a802009-06-18 17:55:47 -0700341 if quoted != tc.output {
342 t.Errorf("QuoteMeta(`%s`) = `%s`; want `%s`",
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800343 tc.pattern, quoted, tc.output)
344 continue
Steve Newmana6c7a802009-06-18 17:55:47 -0700345 }
346
347 // Verify that the quoted string is in fact treated as expected
348 // by Compile -- i.e. that it matches the original, unquoted string.
349 if tc.pattern != "" {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800350 re, err := Compile(quoted)
Steve Newmana6c7a802009-06-18 17:55:47 -0700351 if err != nil {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800352 t.Errorf("Unexpected error compiling QuoteMeta(`%s`): %v", tc.pattern, err)
353 continue
Steve Newmana6c7a802009-06-18 17:55:47 -0700354 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800355 src := "abc" + tc.pattern + "def"
356 repl := "xyz"
357 replaced := re.ReplaceAllString(src, repl)
358 expected := "abcxyzdef"
Steve Newmana6c7a802009-06-18 17:55:47 -0700359 if replaced != expected {
360 t.Errorf("QuoteMeta(`%s`).Replace(`%s`,`%s`) = `%s`; want `%s`",
Robert Griesemer40621d52009-11-09 12:07:39 -0800361 tc.pattern, src, repl, replaced, expected)
Steve Newmana6c7a802009-06-18 17:55:47 -0700362 }
363 }
364 }
365}
366
Rob Pikea9e7c932010-12-17 10:23:46 -0800367func TestLiteralPrefix(t *testing.T) {
368 for _, tc := range metaTests {
369 // Literal method needs to scan the pattern.
370 re := MustCompile(tc.pattern)
371 str, complete := re.LiteralPrefix()
372 if complete != tc.isLiteral {
373 t.Errorf("LiteralPrefix(`%s`) = %t; want %t", tc.pattern, complete, tc.isLiteral)
374 }
375 if str != tc.literal {
376 t.Errorf("LiteralPrefix(`%s`) = `%s`; want `%s`", tc.pattern, str, tc.literal)
Rob Pike5bd40942010-12-16 16:55:26 -0800377 }
378 }
379}
380
Russ Cox21d37212012-01-19 01:24:01 -0500381type subexpCase struct {
382 input string
383 num int
384 names []string
Peter Froehliche1033d02009-12-24 08:43:35 +1100385}
386
Russ Cox21d37212012-01-19 01:24:01 -0500387var subexpCases = []subexpCase{
388 {``, 0, nil},
389 {`.*`, 0, nil},
390 {`abba`, 0, nil},
391 {`ab(b)a`, 1, []string{"", ""}},
392 {`ab(.*)a`, 1, []string{"", ""}},
393 {`(.*)ab(.*)a`, 2, []string{"", "", ""}},
394 {`(.*)(ab)(.*)a`, 3, []string{"", "", "", ""}},
395 {`(.*)((a)b)(.*)a`, 4, []string{"", "", "", "", ""}},
396 {`(.*)(\(ab)(.*)a`, 3, []string{"", "", "", ""}},
397 {`(.*)(\(a\)b)(.*)a`, 3, []string{"", "", "", ""}},
398 {`(?P<foo>.*)(?P<bar>(a)b)(?P<foo>.*)a`, 4, []string{"", "foo", "bar", "", "foo"}},
Peter Froehliche1033d02009-12-24 08:43:35 +1100399}
400
Russ Cox21d37212012-01-19 01:24:01 -0500401func TestSubexp(t *testing.T) {
402 for _, c := range subexpCases {
Kyle Consalusaae02a12010-06-02 23:04:44 -0700403 re := MustCompile(c.input)
Peter Froehliche1033d02009-12-24 08:43:35 +1100404 n := re.NumSubexp()
Russ Cox21d37212012-01-19 01:24:01 -0500405 if n != c.num {
406 t.Errorf("%q: NumSubexp = %d, want %d", c.input, n, c.num)
407 continue
408 }
409 names := re.SubexpNames()
410 if len(names) != 1+n {
411 t.Errorf("%q: len(SubexpNames) = %d, want %d", c.input, len(names), n)
412 continue
413 }
414 if c.names != nil {
415 for i := 0; i < 1+n; i++ {
416 if names[i] != c.names[i] {
417 t.Errorf("%q: SubexpNames[%d] = %q, want %q", c.input, i, names[i], c.names[i])
418 }
419 }
Peter Froehliche1033d02009-12-24 08:43:35 +1100420 }
421 }
422}
423
Rick Arnold94b3f6d2012-11-27 12:58:27 -0500424var splitTests = []struct {
425 s string
426 r string
427 n int
428 out []string
429}{
430 {"foo:and:bar", ":", -1, []string{"foo", "and", "bar"}},
431 {"foo:and:bar", ":", 1, []string{"foo:and:bar"}},
432 {"foo:and:bar", ":", 2, []string{"foo", "and:bar"}},
433 {"foo:and:bar", "foo", -1, []string{"", ":and:bar"}},
434 {"foo:and:bar", "bar", -1, []string{"foo:and:", ""}},
435 {"foo:and:bar", "baz", -1, []string{"foo:and:bar"}},
436 {"baabaab", "a", -1, []string{"b", "", "b", "", "b"}},
437 {"baabaab", "a*", -1, []string{"b", "b", "b"}},
438 {"baabaab", "ba*", -1, []string{"", "", "", ""}},
439 {"foobar", "f*b*", -1, []string{"", "o", "o", "a", "r"}},
440 {"foobar", "f+.*b+", -1, []string{"", "ar"}},
441 {"foobooboar", "o{2}", -1, []string{"f", "b", "boar"}},
442 {"a,b,c,d,e,f", ",", 3, []string{"a", "b", "c,d,e,f"}},
443 {"a,b,c,d,e,f", ",", 0, nil},
444 {",", ",", -1, []string{"", ""}},
445 {",,,", ",", -1, []string{"", "", "", ""}},
446 {"", ",", -1, []string{""}},
447 {"", ".*", -1, []string{""}},
448 {"", ".+", -1, []string{""}},
449 {"", "", -1, []string{}},
450 {"foobar", "", -1, []string{"f", "o", "o", "b", "a", "r"}},
451 {"abaabaccadaaae", "a*", 5, []string{"", "b", "b", "c", "cadaaae"}},
452 {":x:y:z:", ":", -1, []string{"", "x", "y", "z", ""}},
453}
454
455func TestSplit(t *testing.T) {
456 for i, test := range splitTests {
457 re, err := Compile(test.r)
458 if err != nil {
459 t.Errorf("#%d: %q: compile error: %s", i, test.r, err.Error())
460 continue
461 }
462
463 split := re.Split(test.s, test.n)
464 if !reflect.DeepEqual(split, test.out) {
465 t.Errorf("#%d: %q: got %q; want %q", i, test.r, split, test.out)
466 }
467
468 if QuoteMeta(test.r) == test.r {
469 strsplit := strings.SplitN(test.s, test.r, test.n)
470 if !reflect.DeepEqual(split, strsplit) {
471 t.Errorf("#%d: Split(%q, %q, %d): regexp vs strings mismatch\nregexp=%q\nstrings=%q", i, test.s, test.r, test.n, split, strsplit)
472 }
473 }
474 }
475}
476
Russ Cox22be4bf2014-10-20 12:16:46 -0400477// Check that one-pass cutoff does trigger.
Rob Pikef54f7902014-05-13 12:17:49 -0700478func TestOnePassCutoff(t *testing.T) {
Russ Cox22be4bf2014-10-20 12:16:46 -0400479 re, err := syntax.Parse(`^x{1,1000}y{1,1000}$`, syntax.Perl)
480 if err != nil {
481 t.Fatalf("parse: %v", err)
Dmitriy Vyukov280eb702014-06-24 17:19:10 -0700482 }
Russ Cox22be4bf2014-10-20 12:16:46 -0400483 p, err := syntax.Compile(re.Simplify())
484 if err != nil {
485 t.Fatalf("compile: %v", err)
486 }
487 if compileOnePass(p) != notOnePass {
488 t.Fatalf("makeOnePass succeeded; wanted notOnePass")
489 }
Rob Pikef54f7902014-05-13 12:17:49 -0700490}
491
Rob Pikec62069c2009-11-19 23:12:01 -0800492func BenchmarkLiteral(b *testing.B) {
Rob Pike8a2d7062011-02-01 17:48:42 -0800493 x := strings.Repeat("x", 50) + "y"
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800494 b.StopTimer()
Rob Pike8a2d7062011-02-01 17:48:42 -0800495 re := MustCompile("y")
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800496 b.StartTimer()
Rob Pikec62069c2009-11-19 23:12:01 -0800497 for i := 0; i < b.N; i++ {
498 if !re.MatchString(x) {
Rob Pike6b772462011-12-20 10:36:25 -0800499 b.Fatalf("no match!")
Rob Pikec62069c2009-11-19 23:12:01 -0800500 }
501 }
502}
503
504func BenchmarkNotLiteral(b *testing.B) {
Rob Pike8a2d7062011-02-01 17:48:42 -0800505 x := strings.Repeat("x", 50) + "y"
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800506 b.StopTimer()
Rob Pike8a2d7062011-02-01 17:48:42 -0800507 re := MustCompile(".y")
Kyle Consalusaae02a12010-06-02 23:04:44 -0700508 b.StartTimer()
509 for i := 0; i < b.N; i++ {
510 if !re.MatchString(x) {
Rob Pike6b772462011-12-20 10:36:25 -0800511 b.Fatalf("no match!")
Kyle Consalusaae02a12010-06-02 23:04:44 -0700512 }
513 }
514}
515
516func BenchmarkMatchClass(b *testing.B) {
517 b.StopTimer()
518 x := strings.Repeat("xxxx", 20) + "w"
519 re := MustCompile("[abcdw]")
520 b.StartTimer()
521 for i := 0; i < b.N; i++ {
522 if !re.MatchString(x) {
Rob Pike6b772462011-12-20 10:36:25 -0800523 b.Fatalf("no match!")
Kyle Consalusaae02a12010-06-02 23:04:44 -0700524 }
525 }
526}
527
528func BenchmarkMatchClass_InRange(b *testing.B) {
529 b.StopTimer()
Robert Hencke3fbd4782011-05-30 18:02:59 +1000530 // 'b' is between 'a' and 'c', so the charclass
Kyle Consalusaae02a12010-06-02 23:04:44 -0700531 // range checking is no help here.
532 x := strings.Repeat("bbbb", 20) + "c"
533 re := MustCompile("[ac]")
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800534 b.StartTimer()
Rob Pikec62069c2009-11-19 23:12:01 -0800535 for i := 0; i < b.N; i++ {
536 if !re.MatchString(x) {
Rob Pike6b772462011-12-20 10:36:25 -0800537 b.Fatalf("no match!")
Rob Pikec62069c2009-11-19 23:12:01 -0800538 }
539 }
540}
Andrew Gerrandf25e0162010-03-07 12:41:49 +1100541
542func BenchmarkReplaceAll(b *testing.B) {
543 x := "abcdefghijklmnopqrstuvwxyz"
544 b.StopTimer()
Kyle Consalusaae02a12010-06-02 23:04:44 -0700545 re := MustCompile("[cjrw]")
Andrew Gerrandf25e0162010-03-07 12:41:49 +1100546 b.StartTimer()
547 for i := 0; i < b.N; i++ {
548 re.ReplaceAllString(x, "")
549 }
550}
Rob Pikec0d0d4e2011-01-03 11:31:51 -0800551
552func BenchmarkAnchoredLiteralShortNonMatch(b *testing.B) {
553 b.StopTimer()
554 x := []byte("abcdefghijklmnopqrstuvwxyz")
555 re := MustCompile("^zbc(d|e)")
556 b.StartTimer()
557 for i := 0; i < b.N; i++ {
558 re.Match(x)
559 }
560}
561
562func BenchmarkAnchoredLiteralLongNonMatch(b *testing.B) {
563 b.StopTimer()
564 x := []byte("abcdefghijklmnopqrstuvwxyz")
565 for i := 0; i < 15; i++ {
566 x = append(x, x...)
567 }
568 re := MustCompile("^zbc(d|e)")
569 b.StartTimer()
570 for i := 0; i < b.N; i++ {
571 re.Match(x)
572 }
573}
574
575func BenchmarkAnchoredShortMatch(b *testing.B) {
576 b.StopTimer()
577 x := []byte("abcdefghijklmnopqrstuvwxyz")
578 re := MustCompile("^.bc(d|e)")
579 b.StartTimer()
580 for i := 0; i < b.N; i++ {
581 re.Match(x)
582 }
583}
584
585func BenchmarkAnchoredLongMatch(b *testing.B) {
586 b.StopTimer()
587 x := []byte("abcdefghijklmnopqrstuvwxyz")
588 for i := 0; i < 15; i++ {
589 x = append(x, x...)
590 }
591 re := MustCompile("^.bc(d|e)")
592 b.StartTimer()
593 for i := 0; i < b.N; i++ {
594 re.Match(x)
595 }
596}
David Covert76236ef2014-03-07 15:30:02 -0500597
598func BenchmarkOnePassShortA(b *testing.B) {
599 b.StopTimer()
600 x := []byte("abcddddddeeeededd")
601 re := MustCompile("^.bc(d|e)*$")
602 b.StartTimer()
603 for i := 0; i < b.N; i++ {
604 re.Match(x)
605 }
606}
Rob Pikef54f7902014-05-13 12:17:49 -0700607
David Covert76236ef2014-03-07 15:30:02 -0500608func BenchmarkNotOnePassShortA(b *testing.B) {
609 b.StopTimer()
610 x := []byte("abcddddddeeeededd")
611 re := MustCompile(".bc(d|e)*$")
612 b.StartTimer()
613 for i := 0; i < b.N; i++ {
614 re.Match(x)
615 }
616}
Rob Pikef54f7902014-05-13 12:17:49 -0700617
David Covert76236ef2014-03-07 15:30:02 -0500618func BenchmarkOnePassShortB(b *testing.B) {
619 b.StopTimer()
620 x := []byte("abcddddddeeeededd")
621 re := MustCompile("^.bc(?:d|e)*$")
622 b.StartTimer()
623 for i := 0; i < b.N; i++ {
624 re.Match(x)
625 }
626}
Rob Pikef54f7902014-05-13 12:17:49 -0700627
David Covert76236ef2014-03-07 15:30:02 -0500628func BenchmarkNotOnePassShortB(b *testing.B) {
629 b.StopTimer()
630 x := []byte("abcddddddeeeededd")
631 re := MustCompile(".bc(?:d|e)*$")
632 b.StartTimer()
633 for i := 0; i < b.N; i++ {
634 re.Match(x)
635 }
636}
Rob Pikef54f7902014-05-13 12:17:49 -0700637
David Covert76236ef2014-03-07 15:30:02 -0500638func BenchmarkOnePassLongPrefix(b *testing.B) {
639 b.StopTimer()
640 x := []byte("abcdefghijklmnopqrstuvwxyz")
641 re := MustCompile("^abcdefghijklmnopqrstuvwxyz.*$")
642 b.StartTimer()
643 for i := 0; i < b.N; i++ {
644 re.Match(x)
645 }
646}
Rob Pikef54f7902014-05-13 12:17:49 -0700647
David Covert76236ef2014-03-07 15:30:02 -0500648func BenchmarkOnePassLongNotPrefix(b *testing.B) {
649 b.StopTimer()
650 x := []byte("abcdefghijklmnopqrstuvwxyz")
651 re := MustCompile("^.bcdefghijklmnopqrstuvwxyz.*$")
652 b.StartTimer()
653 for i := 0; i < b.N; i++ {
654 re.Match(x)
655 }
656}