github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/stdlibs/regexp/all_test.gno (about)

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