Source file src/regexp/all_test.go

     1  // 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  
     5  package regexp
     6  
     7  import (
     8  	"bytes"
     9  	"reflect"
    10  	"regexp/syntax"
    11  	"slices"
    12  	"strings"
    13  	"testing"
    14  	"unicode/utf8"
    15  )
    16  
    17  var goodRe = []string{
    18  	``,
    19  	`.`,
    20  	`^.$`,
    21  	`a`,
    22  	`a*`,
    23  	`a+`,
    24  	`a?`,
    25  	`a|b`,
    26  	`a*|b*`,
    27  	`(a*|b)(c*|d)`,
    28  	`[a-z]`,
    29  	`[a-abc-c\-\]\[]`,
    30  	`[a-z]+`,
    31  	`[abc]`,
    32  	`[^1234]`,
    33  	`[^\n]`,
    34  	`\!\\`,
    35  }
    36  
    37  type stringError struct {
    38  	re  string
    39  	err string
    40  }
    41  
    42  var badRe = []stringError{
    43  	{`*`, "missing argument to repetition operator: `*`"},
    44  	{`+`, "missing argument to repetition operator: `+`"},
    45  	{`?`, "missing argument to repetition operator: `?`"},
    46  	{`(abc`, "missing closing ): `(abc`"},
    47  	{`abc)`, "unexpected ): `abc)`"},
    48  	{`x[a-z`, "missing closing ]: `[a-z`"},
    49  	{`[z-a]`, "invalid character class range: `z-a`"},
    50  	{`abc\`, "trailing backslash at end of expression"},
    51  	{`a**`, "invalid nested repetition operator: `**`"},
    52  	{`a*+`, "invalid nested repetition operator: `*+`"},
    53  	{`\x`, "invalid escape sequence: `\\x`"},
    54  	{strings.Repeat(`\pL`, 27000), "expression too large"},
    55  }
    56  
    57  func compileTest(t *testing.T, expr string, error string) *Regexp {
    58  	re, err := Compile(expr)
    59  	if error == "" && err != nil {
    60  		t.Error("compiling `", expr, "`; unexpected error: ", err.Error())
    61  	}
    62  	if error != "" && err == nil {
    63  		t.Error("compiling `", expr, "`; missing error")
    64  	} else if error != "" && !strings.Contains(err.Error(), error) {
    65  		t.Error("compiling `", expr, "`; wrong error: ", err.Error(), "; want ", error)
    66  	}
    67  	return re
    68  }
    69  
    70  func TestGoodCompile(t *testing.T) {
    71  	for i := 0; i < len(goodRe); i++ {
    72  		compileTest(t, goodRe[i], "")
    73  	}
    74  }
    75  
    76  func TestBadCompile(t *testing.T) {
    77  	for i := 0; i < len(badRe); i++ {
    78  		compileTest(t, badRe[i].re, badRe[i].err)
    79  	}
    80  }
    81  
    82  func matchTest(t *testing.T, test *FindTest) {
    83  	if test.max == 0 {
    84  		return
    85  	}
    86  	re := compileTest(t, test.pat, "")
    87  	if re == nil {
    88  		return
    89  	}
    90  	m := re.MatchString(test.text)
    91  	if m != (len(test.matches) > 0) {
    92  		t.Errorf("MatchString failure on %s: %t should be %t", test, m, len(test.matches) > 0)
    93  	}
    94  	// now try bytes
    95  	m = re.Match([]byte(test.text))
    96  	if m != (len(test.matches) > 0) {
    97  		t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0)
    98  	}
    99  }
   100  
   101  func TestMatch(t *testing.T) {
   102  	for _, test := range findTests {
   103  		matchTest(t, &test)
   104  	}
   105  }
   106  
   107  func matchFunctionTest(t *testing.T, test *FindTest) {
   108  	m, err := MatchString(test.pat, test.text)
   109  	if err == nil {
   110  		return
   111  	}
   112  	if m != (len(test.matches) > 0) {
   113  		t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0)
   114  	}
   115  }
   116  
   117  func TestMatchFunction(t *testing.T) {
   118  	for _, test := range findTests {
   119  		matchFunctionTest(t, &test)
   120  	}
   121  }
   122  
   123  func copyMatchTest(t *testing.T, test *FindTest) {
   124  	re := compileTest(t, test.pat, "")
   125  	if re == nil {
   126  		return
   127  	}
   128  	m1 := re.MatchString(test.text)
   129  	m2 := re.Copy().MatchString(test.text)
   130  	if m1 != m2 {
   131  		t.Errorf("Copied Regexp match failure on %s: original gave %t; copy gave %t; should be %t",
   132  			test, m1, m2, len(test.matches) > 0)
   133  	}
   134  }
   135  
   136  func TestCopyMatch(t *testing.T) {
   137  	for _, test := range findTests {
   138  		copyMatchTest(t, &test)
   139  	}
   140  }
   141  
   142  type ReplaceTest struct {
   143  	pattern, replacement, input, output string
   144  }
   145  
   146  var replaceTests = []ReplaceTest{
   147  	// Test empty input and/or replacement, with pattern that matches the empty string.
   148  	{"", "", "", ""},
   149  	{"", "x", "", "x"},
   150  	{"", "", "abc", "abc"},
   151  	{"", "x", "abc", "xaxbxcx"},
   152  
   153  	// Test empty input and/or replacement, with pattern that does not match the empty string.
   154  	{"b", "", "", ""},
   155  	{"b", "x", "", ""},
   156  	{"b", "", "abc", "ac"},
   157  	{"b", "x", "abc", "axc"},
   158  	{"y", "", "", ""},
   159  	{"y", "x", "", ""},
   160  	{"y", "", "abc", "abc"},
   161  	{"y", "x", "abc", "abc"},
   162  
   163  	// Multibyte characters -- verify that we don't try to match in the middle
   164  	// of a character.
   165  	{"[a-c]*", "x", "\u65e5", "x\u65e5x"},
   166  	{"[^\u65e5]", "x", "abc\u65e5def", "xxx\u65e5xxx"},
   167  
   168  	// Start and end of a string.
   169  	{"^[a-c]*", "x", "abcdabc", "xdabc"},
   170  	{"[a-c]*$", "x", "abcdabc", "abcdx"},
   171  	{"^[a-c]*$", "x", "abcdabc", "abcdabc"},
   172  	{"^[a-c]*", "x", "abc", "x"},
   173  	{"[a-c]*$", "x", "abc", "x"},
   174  	{"^[a-c]*$", "x", "abc", "x"},
   175  	{"^[a-c]*", "x", "dabce", "xdabce"},
   176  	{"[a-c]*$", "x", "dabce", "dabcex"},
   177  	{"^[a-c]*$", "x", "dabce", "dabce"},
   178  	{"^[a-c]*", "x", "", "x"},
   179  	{"[a-c]*$", "x", "", "x"},
   180  	{"^[a-c]*$", "x", "", "x"},
   181  
   182  	{"^[a-c]+", "x", "abcdabc", "xdabc"},
   183  	{"[a-c]+$", "x", "abcdabc", "abcdx"},
   184  	{"^[a-c]+$", "x", "abcdabc", "abcdabc"},
   185  	{"^[a-c]+", "x", "abc", "x"},
   186  	{"[a-c]+$", "x", "abc", "x"},
   187  	{"^[a-c]+$", "x", "abc", "x"},
   188  	{"^[a-c]+", "x", "dabce", "dabce"},
   189  	{"[a-c]+$", "x", "dabce", "dabce"},
   190  	{"^[a-c]+$", "x", "dabce", "dabce"},
   191  	{"^[a-c]+", "x", "", ""},
   192  	{"[a-c]+$", "x", "", ""},
   193  	{"^[a-c]+$", "x", "", ""},
   194  
   195  	// Other cases.
   196  	{"abc", "def", "abcdefg", "defdefg"},
   197  	{"bc", "BC", "abcbcdcdedef", "aBCBCdcdedef"},
   198  	{"abc", "", "abcdabc", "d"},
   199  	{"x", "xXx", "xxxXxxx", "xXxxXxxXxXxXxxXxxXx"},
   200  	{"abc", "d", "", ""},
   201  	{"abc", "d", "abc", "d"},
   202  	{".+", "x", "abc", "x"},
   203  	{"[a-c]*", "x", "def", "xdxexfx"},
   204  	{"[a-c]+", "x", "abcbcdcdedef", "xdxdedef"},
   205  	{"[a-c]*", "x", "abcbcdcdedef", "xdxdxexdxexfx"},
   206  
   207  	// Substitutions
   208  	{"a+", "($0)", "banana", "b(a)n(a)n(a)"},
   209  	{"a+", "(${0})", "banana", "b(a)n(a)n(a)"},
   210  	{"a+", "(${0})$0", "banana", "b(a)an(a)an(a)a"},
   211  	{"a+", "(${0})$0", "banana", "b(a)an(a)an(a)a"},
   212  	{"hello, (.+)", "goodbye, ${1}", "hello, world", "goodbye, world"},
   213  	{"hello, (.+)", "goodbye, $1x", "hello, world", "goodbye, "},
   214  	{"hello, (.+)", "goodbye, ${1}x", "hello, world", "goodbye, worldx"},
   215  	{"hello, (.+)", "<$0><$1><$2><$3>", "hello, world", "<hello, world><world><><>"},
   216  	{"hello, (?P<noun>.+)", "goodbye, $noun!", "hello, world", "goodbye, world!"},
   217  	{"hello, (?P<noun>.+)", "goodbye, ${noun}", "hello, world", "goodbye, world"},
   218  	{"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "hi", "hihihi"},
   219  	{"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "bye", "byebyebye"},
   220  	{"(?P<x>hi)|(?P<x>bye)", "$xyz", "hi", ""},
   221  	{"(?P<x>hi)|(?P<x>bye)", "${x}yz", "hi", "hiyz"},
   222  	{"(?P<x>hi)|(?P<x>bye)", "hello $$x", "hi", "hello $x"},
   223  	{"a+", "${oops", "aaa", "${oops"},
   224  	{"a+", "$$", "aaa", "$"},
   225  	{"a+", "$", "aaa", "$"},
   226  
   227  	// Substitution when subexpression isn't found
   228  	{"(x)?", "$1", "123", "123"},
   229  	{"abc", "$1", "123", "123"},
   230  
   231  	// Substitutions involving a (x){0}
   232  	{"(a)(b){0}(c)", ".$1|$3.", "xacxacx", "x.a|c.x.a|c.x"},
   233  	{"(a)(((b))){0}c", ".$1.", "xacxacx", "x.a.x.a.x"},
   234  	{"((a(b){0}){3}){5}(h)", "y caramb$2", "say aaaaaaaaaaaaaaaah", "say ay caramba"},
   235  	{"((a(b){0}){3}){5}h", "y caramb$2", "say aaaaaaaaaaaaaaaah", "say ay caramba"},
   236  }
   237  
   238  var replaceLiteralTests = []ReplaceTest{
   239  	// Substitutions
   240  	{"a+", "($0)", "banana", "b($0)n($0)n($0)"},
   241  	{"a+", "(${0})", "banana", "b(${0})n(${0})n(${0})"},
   242  	{"a+", "(${0})$0", "banana", "b(${0})$0n(${0})$0n(${0})$0"},
   243  	{"a+", "(${0})$0", "banana", "b(${0})$0n(${0})$0n(${0})$0"},
   244  	{"hello, (.+)", "goodbye, ${1}", "hello, world", "goodbye, ${1}"},
   245  	{"hello, (?P<noun>.+)", "goodbye, $noun!", "hello, world", "goodbye, $noun!"},
   246  	{"hello, (?P<noun>.+)", "goodbye, ${noun}", "hello, world", "goodbye, ${noun}"},
   247  	{"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "hi", "$x$x$x"},
   248  	{"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "bye", "$x$x$x"},
   249  	{"(?P<x>hi)|(?P<x>bye)", "$xyz", "hi", "$xyz"},
   250  	{"(?P<x>hi)|(?P<x>bye)", "${x}yz", "hi", "${x}yz"},
   251  	{"(?P<x>hi)|(?P<x>bye)", "hello $$x", "hi", "hello $$x"},
   252  	{"a+", "${oops", "aaa", "${oops"},
   253  	{"a+", "$$", "aaa", "$$"},
   254  	{"a+", "$", "aaa", "$"},
   255  }
   256  
   257  type ReplaceFuncTest struct {
   258  	pattern       string
   259  	replacement   func(string) string
   260  	input, output string
   261  }
   262  
   263  var replaceFuncTests = []ReplaceFuncTest{
   264  	{"[a-c]", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxayxbyxcydef"},
   265  	{"[a-c]+", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxabcydef"},
   266  	{"[a-c]*", func(s string) string { return "x" + s + "y" }, "defabcdef", "xydxyexyfxabcydxyexyfxy"},
   267  }
   268  
   269  func TestReplaceAll(t *testing.T) {
   270  	for _, tc := range replaceTests {
   271  		re, err := Compile(tc.pattern)
   272  		if err != nil {
   273  			t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
   274  			continue
   275  		}
   276  		actual := re.ReplaceAllString(tc.input, tc.replacement)
   277  		if actual != tc.output {
   278  			t.Errorf("%q.ReplaceAllString(%q,%q) = %q; want %q",
   279  				tc.pattern, tc.input, tc.replacement, actual, tc.output)
   280  		}
   281  		// now try bytes
   282  		actual = string(re.ReplaceAll([]byte(tc.input), []byte(tc.replacement)))
   283  		if actual != tc.output {
   284  			t.Errorf("%q.ReplaceAll(%q,%q) = %q; want %q",
   285  				tc.pattern, tc.input, tc.replacement, actual, tc.output)
   286  		}
   287  	}
   288  }
   289  
   290  func TestReplaceAllLiteral(t *testing.T) {
   291  	// Run ReplaceAll tests that do not have $ expansions.
   292  	for _, tc := range replaceTests {
   293  		if strings.Contains(tc.replacement, "$") {
   294  			continue
   295  		}
   296  		re, err := Compile(tc.pattern)
   297  		if err != nil {
   298  			t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
   299  			continue
   300  		}
   301  		actual := re.ReplaceAllLiteralString(tc.input, tc.replacement)
   302  		if actual != tc.output {
   303  			t.Errorf("%q.ReplaceAllLiteralString(%q,%q) = %q; want %q",
   304  				tc.pattern, tc.input, tc.replacement, actual, tc.output)
   305  		}
   306  		// now try bytes
   307  		actual = string(re.ReplaceAllLiteral([]byte(tc.input), []byte(tc.replacement)))
   308  		if actual != tc.output {
   309  			t.Errorf("%q.ReplaceAllLiteral(%q,%q) = %q; want %q",
   310  				tc.pattern, tc.input, tc.replacement, actual, tc.output)
   311  		}
   312  	}
   313  
   314  	// Run literal-specific tests.
   315  	for _, tc := range replaceLiteralTests {
   316  		re, err := Compile(tc.pattern)
   317  		if err != nil {
   318  			t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
   319  			continue
   320  		}
   321  		actual := re.ReplaceAllLiteralString(tc.input, tc.replacement)
   322  		if actual != tc.output {
   323  			t.Errorf("%q.ReplaceAllLiteralString(%q,%q) = %q; want %q",
   324  				tc.pattern, tc.input, tc.replacement, actual, tc.output)
   325  		}
   326  		// now try bytes
   327  		actual = string(re.ReplaceAllLiteral([]byte(tc.input), []byte(tc.replacement)))
   328  		if actual != tc.output {
   329  			t.Errorf("%q.ReplaceAllLiteral(%q,%q) = %q; want %q",
   330  				tc.pattern, tc.input, tc.replacement, actual, tc.output)
   331  		}
   332  	}
   333  }
   334  
   335  func TestReplaceAllFunc(t *testing.T) {
   336  	for _, tc := range replaceFuncTests {
   337  		re, err := Compile(tc.pattern)
   338  		if err != nil {
   339  			t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
   340  			continue
   341  		}
   342  		actual := re.ReplaceAllStringFunc(tc.input, tc.replacement)
   343  		if actual != tc.output {
   344  			t.Errorf("%q.ReplaceFunc(%q,fn) = %q; want %q",
   345  				tc.pattern, tc.input, actual, tc.output)
   346  		}
   347  		// now try bytes
   348  		actual = string(re.ReplaceAllFunc([]byte(tc.input), func(s []byte) []byte { return []byte(tc.replacement(string(s))) }))
   349  		if actual != tc.output {
   350  			t.Errorf("%q.ReplaceFunc(%q,fn) = %q; want %q",
   351  				tc.pattern, tc.input, actual, tc.output)
   352  		}
   353  	}
   354  }
   355  
   356  type MetaTest struct {
   357  	pattern, output, literal string
   358  	isLiteral                bool
   359  }
   360  
   361  var metaTests = []MetaTest{
   362  	{``, ``, ``, true},
   363  	{`foo`, `foo`, `foo`, true},
   364  	{`日本語+`, `日本語\+`, `日本語`, false},
   365  	{`foo\.\$`, `foo\\\.\\\$`, `foo.$`, true}, // has meta but no operator
   366  	{`foo.\$`, `foo\.\\\$`, `foo`, false},     // has escaped operators and real operators
   367  	{`!@#$%^&*()_+-=[{]}\|,<.>/?~`, `!@#\$%\^&\*\(\)_\+-=\[\{\]\}\\\|,<\.>/\?~`, `!@#`, false},
   368  }
   369  
   370  var literalPrefixTests = []MetaTest{
   371  	// See golang.org/issue/11175.
   372  	// output is unused.
   373  	{`^0^0$`, ``, `0`, false},
   374  	{`^0^`, ``, ``, false},
   375  	{`^0$`, ``, `0`, true},
   376  	{`$0^`, ``, ``, false},
   377  	{`$0$`, ``, ``, false},
   378  	{`^^0$$`, ``, ``, false},
   379  	{`^$^$`, ``, ``, false},
   380  	{`$$0^^`, ``, ``, false},
   381  	{`a\x{fffd}b`, ``, `a`, false},
   382  	{`\x{fffd}b`, ``, ``, false},
   383  	{"\ufffd", ``, ``, false},
   384  }
   385  
   386  func TestQuoteMeta(t *testing.T) {
   387  	for _, tc := range metaTests {
   388  		// Verify that QuoteMeta returns the expected string.
   389  		quoted := QuoteMeta(tc.pattern)
   390  		if quoted != tc.output {
   391  			t.Errorf("QuoteMeta(`%s`) = `%s`; want `%s`",
   392  				tc.pattern, quoted, tc.output)
   393  			continue
   394  		}
   395  
   396  		// Verify that the quoted string is in fact treated as expected
   397  		// by Compile -- i.e. that it matches the original, unquoted string.
   398  		if tc.pattern != "" {
   399  			re, err := Compile(quoted)
   400  			if err != nil {
   401  				t.Errorf("Unexpected error compiling QuoteMeta(`%s`): %v", tc.pattern, err)
   402  				continue
   403  			}
   404  			src := "abc" + tc.pattern + "def"
   405  			repl := "xyz"
   406  			replaced := re.ReplaceAllString(src, repl)
   407  			expected := "abcxyzdef"
   408  			if replaced != expected {
   409  				t.Errorf("QuoteMeta(`%s`).Replace(`%s`,`%s`) = `%s`; want `%s`",
   410  					tc.pattern, src, repl, replaced, expected)
   411  			}
   412  		}
   413  	}
   414  }
   415  
   416  func TestLiteralPrefix(t *testing.T) {
   417  	for _, tc := range append(metaTests, literalPrefixTests...) {
   418  		// Literal method needs to scan the pattern.
   419  		re := MustCompile(tc.pattern)
   420  		str, complete := re.LiteralPrefix()
   421  		if complete != tc.isLiteral {
   422  			t.Errorf("LiteralPrefix(`%s`) = %t; want %t", tc.pattern, complete, tc.isLiteral)
   423  		}
   424  		if str != tc.literal {
   425  			t.Errorf("LiteralPrefix(`%s`) = `%s`; want `%s`", tc.pattern, str, tc.literal)
   426  		}
   427  	}
   428  }
   429  
   430  type subexpIndex struct {
   431  	name  string
   432  	index int
   433  }
   434  
   435  type subexpCase struct {
   436  	input   string
   437  	num     int
   438  	names   []string
   439  	indices []subexpIndex
   440  }
   441  
   442  var emptySubexpIndices = []subexpIndex{{"", -1}, {"missing", -1}}
   443  
   444  var subexpCases = []subexpCase{
   445  	{``, 0, nil, emptySubexpIndices},
   446  	{`.*`, 0, nil, emptySubexpIndices},
   447  	{`abba`, 0, nil, emptySubexpIndices},
   448  	{`ab(b)a`, 1, []string{"", ""}, emptySubexpIndices},
   449  	{`ab(.*)a`, 1, []string{"", ""}, emptySubexpIndices},
   450  	{`(.*)ab(.*)a`, 2, []string{"", "", ""}, emptySubexpIndices},
   451  	{`(.*)(ab)(.*)a`, 3, []string{"", "", "", ""}, emptySubexpIndices},
   452  	{`(.*)((a)b)(.*)a`, 4, []string{"", "", "", "", ""}, emptySubexpIndices},
   453  	{`(.*)(\(ab)(.*)a`, 3, []string{"", "", "", ""}, emptySubexpIndices},
   454  	{`(.*)(\(a\)b)(.*)a`, 3, []string{"", "", "", ""}, emptySubexpIndices},
   455  	{`(?P<foo>.*)(?P<bar>(a)b)(?P<foo>.*)a`, 4, []string{"", "foo", "bar", "", "foo"}, []subexpIndex{{"", -1}, {"missing", -1}, {"foo", 1}, {"bar", 2}}},
   456  }
   457  
   458  func TestSubexp(t *testing.T) {
   459  	for _, c := range subexpCases {
   460  		re := MustCompile(c.input)
   461  		n := re.NumSubexp()
   462  		if n != c.num {
   463  			t.Errorf("%q: NumSubexp = %d, want %d", c.input, n, c.num)
   464  			continue
   465  		}
   466  		names := re.SubexpNames()
   467  		if len(names) != 1+n {
   468  			t.Errorf("%q: len(SubexpNames) = %d, want %d", c.input, len(names), n)
   469  			continue
   470  		}
   471  		if c.names != nil {
   472  			for i := 0; i < 1+n; i++ {
   473  				if names[i] != c.names[i] {
   474  					t.Errorf("%q: SubexpNames[%d] = %q, want %q", c.input, i, names[i], c.names[i])
   475  				}
   476  			}
   477  		}
   478  		for _, subexp := range c.indices {
   479  			index := re.SubexpIndex(subexp.name)
   480  			if index != subexp.index {
   481  				t.Errorf("%q: SubexpIndex(%q) = %d, want %d", c.input, subexp.name, index, subexp.index)
   482  			}
   483  		}
   484  	}
   485  }
   486  
   487  var splitTests = []struct {
   488  	s   string
   489  	r   string
   490  	n   int
   491  	out []string
   492  }{
   493  	{"foo:and:bar", ":", -1, []string{"foo", "and", "bar"}},
   494  	{"foo:and:bar", ":", 1, []string{"foo:and:bar"}},
   495  	{"foo:and:bar", ":", 2, []string{"foo", "and:bar"}},
   496  	{"foo:and:bar", "foo", -1, []string{"", ":and:bar"}},
   497  	{"foo:and:bar", "bar", -1, []string{"foo:and:", ""}},
   498  	{"foo:and:bar", "baz", -1, []string{"foo:and:bar"}},
   499  	{"baabaab", "a", -1, []string{"b", "", "b", "", "b"}},
   500  	{"baabaab", "a*", -1, []string{"b", "b", "b"}},
   501  	{"baabaab", "ba*", -1, []string{"", "", "", ""}},
   502  	{"foobar", "f*b*", -1, []string{"", "o", "o", "a", "r"}},
   503  	{"foobar", "f+.*b+", -1, []string{"", "ar"}},
   504  	{"foobooboar", "o{2}", -1, []string{"f", "b", "boar"}},
   505  	{"a,b,c,d,e,f", ",", 3, []string{"a", "b", "c,d,e,f"}},
   506  	{"a,b,c,d,e,f", ",", 0, nil},
   507  	{",", ",", -1, []string{"", ""}},
   508  	{",,,", ",", -1, []string{"", "", "", ""}},
   509  	{"", ",", -1, []string{""}},
   510  	{"", ".*", -1, []string{""}},
   511  	{"", ".+", -1, []string{""}},
   512  	{"", "", -1, []string{}},
   513  	{"foobar", "", -1, []string{"f", "o", "o", "b", "a", "r"}},
   514  	{"abaabaccadaaae", "a*", 5, []string{"", "b", "b", "c", "cadaaae"}},
   515  	{":x:y:z:", ":", -1, []string{"", "x", "y", "z", ""}},
   516  }
   517  
   518  func TestSplit(t *testing.T) {
   519  	for i, test := range splitTests {
   520  		re, err := Compile(test.r)
   521  		if err != nil {
   522  			t.Errorf("#%d: %q: compile error: %s", i, test.r, err.Error())
   523  			continue
   524  		}
   525  
   526  		split := re.Split(test.s, test.n)
   527  		if !slices.Equal(split, test.out) {
   528  			t.Errorf("#%d: %q: got %q; want %q", i, test.r, split, test.out)
   529  		}
   530  
   531  		if QuoteMeta(test.r) == test.r {
   532  			strsplit := strings.SplitN(test.s, test.r, test.n)
   533  			if !slices.Equal(split, strsplit) {
   534  				t.Errorf("#%d: Split(%q, %q, %d): regexp vs strings mismatch\nregexp=%q\nstrings=%q", i, test.s, test.r, test.n, split, strsplit)
   535  			}
   536  		}
   537  	}
   538  }
   539  
   540  // The following sequence of Match calls used to panic. See issue #12980.
   541  func TestParseAndCompile(t *testing.T) {
   542  	expr := "a$"
   543  	s := "a\nb"
   544  
   545  	for i, tc := range []struct {
   546  		reFlags  syntax.Flags
   547  		expMatch bool
   548  	}{
   549  		{syntax.Perl | syntax.OneLine, false},
   550  		{syntax.Perl &^ syntax.OneLine, true},
   551  	} {
   552  		parsed, err := syntax.Parse(expr, tc.reFlags)
   553  		if err != nil {
   554  			t.Fatalf("%d: parse: %v", i, err)
   555  		}
   556  		re, err := Compile(parsed.String())
   557  		if err != nil {
   558  			t.Fatalf("%d: compile: %v", i, err)
   559  		}
   560  		if match := re.MatchString(s); match != tc.expMatch {
   561  			t.Errorf("%d: %q.MatchString(%q)=%t; expected=%t", i, re, s, match, tc.expMatch)
   562  		}
   563  	}
   564  }
   565  
   566  // Check that one-pass cutoff does trigger.
   567  func TestOnePassCutoff(t *testing.T) {
   568  	re, err := syntax.Parse(`^x{1,1000}y{1,1000}$`, syntax.Perl)
   569  	if err != nil {
   570  		t.Fatalf("parse: %v", err)
   571  	}
   572  	p, err := syntax.Compile(re.Simplify())
   573  	if err != nil {
   574  		t.Fatalf("compile: %v", err)
   575  	}
   576  	if compileOnePass(p) != nil {
   577  		t.Fatalf("makeOnePass succeeded; wanted nil")
   578  	}
   579  }
   580  
   581  // Check that the same machine can be used with the standard matcher
   582  // and then the backtracker when there are no captures.
   583  func TestSwitchBacktrack(t *testing.T) {
   584  	re := MustCompile(`a|b`)
   585  	long := make([]byte, maxBacktrackVector+1)
   586  
   587  	// The following sequence of Match calls used to panic. See issue #10319.
   588  	re.Match(long)     // triggers standard matcher
   589  	re.Match(long[:1]) // triggers backtracker
   590  }
   591  
   592  func BenchmarkFind(b *testing.B) {
   593  	b.StopTimer()
   594  	re := MustCompile("a+b+")
   595  	wantSubs := "aaabb"
   596  	s := []byte("acbb" + wantSubs + "dd")
   597  	b.StartTimer()
   598  	b.ReportAllocs()
   599  	for i := 0; i < b.N; i++ {
   600  		subs := re.Find(s)
   601  		if string(subs) != wantSubs {
   602  			b.Fatalf("Find(%q) = %q; want %q", s, subs, wantSubs)
   603  		}
   604  	}
   605  }
   606  
   607  func BenchmarkFindAllNoMatches(b *testing.B) {
   608  	re := MustCompile("a+b+")
   609  	s := []byte("acddee")
   610  	b.ReportAllocs()
   611  	b.ResetTimer()
   612  	for i := 0; i < b.N; i++ {
   613  		all := re.FindAll(s, -1)
   614  		if all != nil {
   615  			b.Fatalf("FindAll(%q) = %q; want nil", s, all)
   616  		}
   617  	}
   618  }
   619  
   620  func BenchmarkFindAllTenMatches(b *testing.B) {
   621  	re := MustCompile("a+b+")
   622  	s := bytes.Repeat([]byte("acddeeabbax"), 10)
   623  	b.ReportAllocs()
   624  	b.ResetTimer()
   625  	for i := 0; i < b.N; i++ {
   626  		all := re.FindAll(s, -1)
   627  		if len(all) != 10 {
   628  			b.Fatalf("FindAll(%q) = %q; want 10 matches", s, all)
   629  		}
   630  	}
   631  }
   632  
   633  func BenchmarkFindString(b *testing.B) {
   634  	b.StopTimer()
   635  	re := MustCompile("a+b+")
   636  	wantSubs := "aaabb"
   637  	s := "acbb" + wantSubs + "dd"
   638  	b.StartTimer()
   639  	b.ReportAllocs()
   640  	for i := 0; i < b.N; i++ {
   641  		subs := re.FindString(s)
   642  		if subs != wantSubs {
   643  			b.Fatalf("FindString(%q) = %q; want %q", s, subs, wantSubs)
   644  		}
   645  	}
   646  }
   647  
   648  func BenchmarkFindSubmatch(b *testing.B) {
   649  	b.StopTimer()
   650  	re := MustCompile("a(a+b+)b")
   651  	wantSubs := "aaabb"
   652  	s := []byte("acbb" + wantSubs + "dd")
   653  	b.StartTimer()
   654  	b.ReportAllocs()
   655  	for i := 0; i < b.N; i++ {
   656  		subs := re.FindSubmatch(s)
   657  		if string(subs[0]) != wantSubs {
   658  			b.Fatalf("FindSubmatch(%q)[0] = %q; want %q", s, subs[0], wantSubs)
   659  		}
   660  		if string(subs[1]) != "aab" {
   661  			b.Fatalf("FindSubmatch(%q)[1] = %q; want %q", s, subs[1], "aab")
   662  		}
   663  	}
   664  }
   665  
   666  func BenchmarkFindStringSubmatch(b *testing.B) {
   667  	b.StopTimer()
   668  	re := MustCompile("a(a+b+)b")
   669  	wantSubs := "aaabb"
   670  	s := "acbb" + wantSubs + "dd"
   671  	b.StartTimer()
   672  	b.ReportAllocs()
   673  	for i := 0; i < b.N; i++ {
   674  		subs := re.FindStringSubmatch(s)
   675  		if subs[0] != wantSubs {
   676  			b.Fatalf("FindStringSubmatch(%q)[0] = %q; want %q", s, subs[0], wantSubs)
   677  		}
   678  		if subs[1] != "aab" {
   679  			b.Fatalf("FindStringSubmatch(%q)[1] = %q; want %q", s, subs[1], "aab")
   680  		}
   681  	}
   682  }
   683  
   684  func BenchmarkLiteral(b *testing.B) {
   685  	x := strings.Repeat("x", 50) + "y"
   686  	b.StopTimer()
   687  	re := MustCompile("y")
   688  	b.StartTimer()
   689  	for i := 0; i < b.N; i++ {
   690  		if !re.MatchString(x) {
   691  			b.Fatalf("no match!")
   692  		}
   693  	}
   694  }
   695  
   696  func BenchmarkNotLiteral(b *testing.B) {
   697  	x := strings.Repeat("x", 50) + "y"
   698  	b.StopTimer()
   699  	re := MustCompile(".y")
   700  	b.StartTimer()
   701  	for i := 0; i < b.N; i++ {
   702  		if !re.MatchString(x) {
   703  			b.Fatalf("no match!")
   704  		}
   705  	}
   706  }
   707  
   708  func BenchmarkMatchClass(b *testing.B) {
   709  	b.StopTimer()
   710  	x := strings.Repeat("xxxx", 20) + "w"
   711  	re := MustCompile("[abcdw]")
   712  	b.StartTimer()
   713  	for i := 0; i < b.N; i++ {
   714  		if !re.MatchString(x) {
   715  			b.Fatalf("no match!")
   716  		}
   717  	}
   718  }
   719  
   720  func BenchmarkMatchClass_InRange(b *testing.B) {
   721  	b.StopTimer()
   722  	// 'b' is between 'a' and 'c', so the charclass
   723  	// range checking is no help here.
   724  	x := strings.Repeat("bbbb", 20) + "c"
   725  	re := MustCompile("[ac]")
   726  	b.StartTimer()
   727  	for i := 0; i < b.N; i++ {
   728  		if !re.MatchString(x) {
   729  			b.Fatalf("no match!")
   730  		}
   731  	}
   732  }
   733  
   734  func BenchmarkReplaceAll(b *testing.B) {
   735  	x := "abcdefghijklmnopqrstuvwxyz"
   736  	b.StopTimer()
   737  	re := MustCompile("[cjrw]")
   738  	b.StartTimer()
   739  	for i := 0; i < b.N; i++ {
   740  		re.ReplaceAllString(x, "")
   741  	}
   742  }
   743  
   744  func BenchmarkAnchoredLiteralShortNonMatch(b *testing.B) {
   745  	b.StopTimer()
   746  	x := []byte("abcdefghijklmnopqrstuvwxyz")
   747  	re := MustCompile("^zbc(d|e)")
   748  	b.StartTimer()
   749  	for i := 0; i < b.N; i++ {
   750  		re.Match(x)
   751  	}
   752  }
   753  
   754  func BenchmarkAnchoredLiteralLongNonMatch(b *testing.B) {
   755  	b.StopTimer()
   756  	x := []byte("abcdefghijklmnopqrstuvwxyz")
   757  	for i := 0; i < 15; i++ {
   758  		x = append(x, x...)
   759  	}
   760  	re := MustCompile("^zbc(d|e)")
   761  	b.StartTimer()
   762  	for i := 0; i < b.N; i++ {
   763  		re.Match(x)
   764  	}
   765  }
   766  
   767  func BenchmarkAnchoredShortMatch(b *testing.B) {
   768  	b.StopTimer()
   769  	x := []byte("abcdefghijklmnopqrstuvwxyz")
   770  	re := MustCompile("^.bc(d|e)")
   771  	b.StartTimer()
   772  	for i := 0; i < b.N; i++ {
   773  		re.Match(x)
   774  	}
   775  }
   776  
   777  func BenchmarkAnchoredLongMatch(b *testing.B) {
   778  	b.StopTimer()
   779  	x := []byte("abcdefghijklmnopqrstuvwxyz")
   780  	for i := 0; i < 15; i++ {
   781  		x = append(x, x...)
   782  	}
   783  	re := MustCompile("^.bc(d|e)")
   784  	b.StartTimer()
   785  	for i := 0; i < b.N; i++ {
   786  		re.Match(x)
   787  	}
   788  }
   789  
   790  func BenchmarkOnePassShortA(b *testing.B) {
   791  	b.StopTimer()
   792  	x := []byte("abcddddddeeeededd")
   793  	re := MustCompile("^.bc(d|e)*$")
   794  	b.StartTimer()
   795  	for i := 0; i < b.N; i++ {
   796  		re.Match(x)
   797  	}
   798  }
   799  
   800  func BenchmarkNotOnePassShortA(b *testing.B) {
   801  	b.StopTimer()
   802  	x := []byte("abcddddddeeeededd")
   803  	re := MustCompile(".bc(d|e)*$")
   804  	b.StartTimer()
   805  	for i := 0; i < b.N; i++ {
   806  		re.Match(x)
   807  	}
   808  }
   809  
   810  func BenchmarkOnePassShortB(b *testing.B) {
   811  	b.StopTimer()
   812  	x := []byte("abcddddddeeeededd")
   813  	re := MustCompile("^.bc(?:d|e)*$")
   814  	b.StartTimer()
   815  	for i := 0; i < b.N; i++ {
   816  		re.Match(x)
   817  	}
   818  }
   819  
   820  func BenchmarkNotOnePassShortB(b *testing.B) {
   821  	b.StopTimer()
   822  	x := []byte("abcddddddeeeededd")
   823  	re := MustCompile(".bc(?:d|e)*$")
   824  	b.StartTimer()
   825  	for i := 0; i < b.N; i++ {
   826  		re.Match(x)
   827  	}
   828  }
   829  
   830  func BenchmarkOnePassLongPrefix(b *testing.B) {
   831  	b.StopTimer()
   832  	x := []byte("abcdefghijklmnopqrstuvwxyz")
   833  	re := MustCompile("^abcdefghijklmnopqrstuvwxyz.*$")
   834  	b.StartTimer()
   835  	for i := 0; i < b.N; i++ {
   836  		re.Match(x)
   837  	}
   838  }
   839  
   840  func BenchmarkOnePassLongNotPrefix(b *testing.B) {
   841  	b.StopTimer()
   842  	x := []byte("abcdefghijklmnopqrstuvwxyz")
   843  	re := MustCompile("^.bcdefghijklmnopqrstuvwxyz.*$")
   844  	b.StartTimer()
   845  	for i := 0; i < b.N; i++ {
   846  		re.Match(x)
   847  	}
   848  }
   849  
   850  func BenchmarkMatchParallelShared(b *testing.B) {
   851  	x := []byte("this is a long line that contains foo bar baz")
   852  	re := MustCompile("foo (ba+r)? baz")
   853  	b.ResetTimer()
   854  	b.RunParallel(func(pb *testing.PB) {
   855  		for pb.Next() {
   856  			re.Match(x)
   857  		}
   858  	})
   859  }
   860  
   861  func BenchmarkMatchParallelCopied(b *testing.B) {
   862  	x := []byte("this is a long line that contains foo bar baz")
   863  	re := MustCompile("foo (ba+r)? baz")
   864  	b.ResetTimer()
   865  	b.RunParallel(func(pb *testing.PB) {
   866  		re := re.Copy()
   867  		for pb.Next() {
   868  			re.Match(x)
   869  		}
   870  	})
   871  }
   872  
   873  var sink string
   874  
   875  func BenchmarkQuoteMetaAll(b *testing.B) {
   876  	specials := make([]byte, 0)
   877  	for i := byte(0); i < utf8.RuneSelf; i++ {
   878  		if special(i) {
   879  			specials = append(specials, i)
   880  		}
   881  	}
   882  	s := string(specials)
   883  	b.SetBytes(int64(len(s)))
   884  	b.ResetTimer()
   885  	for i := 0; i < b.N; i++ {
   886  		sink = QuoteMeta(s)
   887  	}
   888  }
   889  
   890  func BenchmarkQuoteMetaNone(b *testing.B) {
   891  	s := "abcdefghijklmnopqrstuvwxyz"
   892  	b.SetBytes(int64(len(s)))
   893  	b.ResetTimer()
   894  	for i := 0; i < b.N; i++ {
   895  		sink = QuoteMeta(s)
   896  	}
   897  }
   898  
   899  var compileBenchData = []struct{ name, re string }{
   900  	{"Onepass", `^a.[l-nA-Cg-j]?e$`},
   901  	{"Medium", `^((a|b|[d-z0-9])*(日){4,5}.)+$`},
   902  	{"Hard", strings.Repeat(`((abc)*|`, 50) + strings.Repeat(`)`, 50)},
   903  }
   904  
   905  func BenchmarkCompile(b *testing.B) {
   906  	for _, data := range compileBenchData {
   907  		b.Run(data.name, func(b *testing.B) {
   908  			b.ReportAllocs()
   909  			for i := 0; i < b.N; i++ {
   910  				if _, err := Compile(data.re); err != nil {
   911  					b.Fatal(err)
   912  				}
   913  			}
   914  		})
   915  	}
   916  }
   917  
   918  func TestDeepEqual(t *testing.T) {
   919  	re1 := MustCompile("a.*b.*c.*d")
   920  	re2 := MustCompile("a.*b.*c.*d")
   921  	if !reflect.DeepEqual(re1, re2) { // has always been true, since Go 1.
   922  		t.Errorf("DeepEqual(re1, re2) = false, want true")
   923  	}
   924  
   925  	re1.MatchString("abcdefghijklmn")
   926  	if !reflect.DeepEqual(re1, re2) {
   927  		t.Errorf("DeepEqual(re1, re2) = false, want true")
   928  	}
   929  
   930  	re2.MatchString("abcdefghijklmn")
   931  	if !reflect.DeepEqual(re1, re2) {
   932  		t.Errorf("DeepEqual(re1, re2) = false, want true")
   933  	}
   934  
   935  	re2.MatchString(strings.Repeat("abcdefghijklmn", 100))
   936  	if !reflect.DeepEqual(re1, re2) {
   937  		t.Errorf("DeepEqual(re1, re2) = false, want true")
   938  	}
   939  }
   940  
   941  var minInputLenTests = []struct {
   942  	Regexp string
   943  	min    int
   944  }{
   945  	{``, 0},
   946  	{`a`, 1},
   947  	{`aa`, 2},
   948  	{`(aa)a`, 3},
   949  	{`(?:aa)a`, 3},
   950  	{`a?a`, 1},
   951  	{`(aaa)|(aa)`, 2},
   952  	{`(aa)+a`, 3},
   953  	{`(aa)*a`, 1},
   954  	{`(aa){3,5}`, 6},
   955  	{`[a-z]`, 1},
   956  	{`日`, 3},
   957  }
   958  
   959  func TestMinInputLen(t *testing.T) {
   960  	for _, tt := range minInputLenTests {
   961  		re, _ := syntax.Parse(tt.Regexp, syntax.Perl)
   962  		m := minInputLen(re)
   963  		if m != tt.min {
   964  			t.Errorf("regexp %#q has minInputLen %d, should be %d", tt.Regexp, m, tt.min)
   965  		}
   966  	}
   967  }
   968  
   969  func TestUnmarshalText(t *testing.T) {
   970  	unmarshaled := new(Regexp)
   971  	for i := range goodRe {
   972  		re := compileTest(t, goodRe[i], "")
   973  		marshaled, err := re.MarshalText()
   974  		if err != nil {
   975  			t.Errorf("regexp %#q failed to marshal: %s", re, err)
   976  			continue
   977  		}
   978  		if err := unmarshaled.UnmarshalText(marshaled); err != nil {
   979  			t.Errorf("regexp %#q failed to unmarshal: %s", re, err)
   980  			continue
   981  		}
   982  		if unmarshaled.String() != goodRe[i] {
   983  			t.Errorf("UnmarshalText returned unexpected value: %s", unmarshaled.String())
   984  		}
   985  
   986  		buf := make([]byte, 4, 32)
   987  		marshalAppend, err := re.AppendText(buf)
   988  		if err != nil {
   989  			t.Errorf("regexp %#q failed to marshal: %s", re, err)
   990  			continue
   991  		}
   992  		marshalAppend = marshalAppend[4:]
   993  		if err := unmarshaled.UnmarshalText(marshalAppend); err != nil {
   994  			t.Errorf("regexp %#q failed to unmarshal: %s", re, err)
   995  			continue
   996  		}
   997  		if unmarshaled.String() != goodRe[i] {
   998  			t.Errorf("UnmarshalText returned unexpected value: %s", unmarshaled.String())
   999  		}
  1000  	}
  1001  	t.Run("invalid pattern", func(t *testing.T) {
  1002  		re := new(Regexp)
  1003  		err := re.UnmarshalText([]byte(`\`))
  1004  		if err == nil {
  1005  			t.Error("unexpected success")
  1006  		}
  1007  	})
  1008  }
  1009  

View as plain text