diff --git a/src/pkg/regexp/find_test.go b/src/pkg/regexp/find_test.go new file mode 100644 index 00000000000..5d1a67a5846 --- /dev/null +++ b/src/pkg/regexp/find_test.go @@ -0,0 +1,442 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package regexp + +import ( + "fmt" + "testing" +) + +// For each pattern/text pair, what is the expected output of each function? +// We can derive the textual results from the indexed results, the non-submatch +// results from the submatched results, the single results from the 'all' results, +// and the byte results from the string results. Therefore the table includes +// only the FindAllStringSubmatchIndex result. +type FindTest struct { + pat string + text string + matches [][]int +} + +func (t FindTest) String() string { + return fmt.Sprintf("pat: %#q text: %#q", t.pat, t.text) +} + +var findTests = []FindTest{ + FindTest{``, ``, build(1, 0, 0)}, + FindTest{`^abcdefg`, "abcdefg", build(1, 0, 7)}, + FindTest{`a+`, "baaab", build(1, 1, 4)}, + FindTest{"abcd..", "abcdef", build(1, 0, 6)}, + FindTest{`a`, "a", build(1, 0, 1)}, + FindTest{`x`, "y", nil}, + FindTest{`b`, "abc", build(1, 1, 2)}, + FindTest{`.`, "a", build(1, 0, 1)}, + FindTest{`.*`, "abcdef", build(1, 0, 6)}, + FindTest{`^`, "abcde", build(1, 0, 0)}, + FindTest{`$`, "abcde", build(1, 5, 5)}, + FindTest{`^abcd$`, "abcd", build(1, 0, 4)}, + FindTest{`^bcd'`, "abcdef", nil}, + FindTest{`^abcd$`, "abcde", nil}, + FindTest{`a+`, "baaab", build(1, 1, 4)}, + FindTest{`a*`, "baaab", build(3, 0, 0, 1, 4, 5, 5)}, + FindTest{`[a-z]+`, "abcd", build(1, 0, 4)}, + FindTest{`[^a-z]+`, "ab1234cd", build(1, 2, 6)}, + FindTest{`[a\-\]z]+`, "az]-bcz", build(2, 0, 4, 6, 7)}, + FindTest{`[^\n]+`, "abcd\n", build(1, 0, 4)}, + FindTest{`[日本語]+`, "日本語日本語", build(1, 0, 18)}, + FindTest{`日本語+`, "日本語", build(1, 0, 9)}, + FindTest{`日本語+`, "日本語語語語", build(1, 0, 18)}, + FindTest{`()`, "", build(1, 0, 0, 0, 0)}, + FindTest{`(a)`, "a", build(1, 0, 1, 0, 1)}, + FindTest{`(.)(.)`, "日a", build(1, 0, 4, 0, 3, 3, 4)}, + FindTest{`(.*)`, "", build(1, 0, 0, 0, 0)}, + FindTest{`(.*)`, "abcd", build(1, 0, 4, 0, 4)}, + FindTest{`(..)(..)`, "abcd", build(1, 0, 4, 0, 2, 2, 4)}, + FindTest{`(([^xyz]*)(d))`, "abcd", build(1, 0, 4, 0, 4, 0, 3, 3, 4)}, + FindTest{`((a|b|c)*(d))`, "abcd", build(1, 0, 4, 0, 4, 2, 3, 3, 4)}, + FindTest{`(((a|b|c)*)(d))`, "abcd", build(1, 0, 4, 0, 4, 0, 3, 2, 3, 3, 4)}, + + FindTest{`a*(|(b))c*`, "aacc", build(1, 0, 4, 2, 2, -1, -1)}, + FindTest{`(.*).*`, "ab", build(1, 0, 2, 0, 2)}, + FindTest{`[.]`, ".", build(1, 0, 1)}, + FindTest{`/$`, "/abc/", build(1, 4, 5)}, + FindTest{`/$`, "/abc", nil}, + + // multiple matches + FindTest{`.`, "abc", build(3, 0, 1, 1, 2, 2, 3)}, + FindTest{`(.)`, "abc", build(3, 0, 1, 0, 1, 1, 2, 1, 2, 2, 3, 2, 3)}, + FindTest{`.(.)`, "abcd", build(2, 0, 2, 1, 2, 2, 4, 3, 4)}, + FindTest{`ab*`, "abbaab", build(3, 0, 3, 3, 4, 4, 6)}, + FindTest{`a(b*)`, "abbaab", build(3, 0, 3, 1, 3, 3, 4, 4, 4, 4, 6, 5, 6)}, + + // fixed bugs + FindTest{`ab$`, "cab", build(1, 1, 3)}, + FindTest{`axxb$`, "axxcb", nil}, + FindTest{`data`, "daXY data", build(1, 5, 9)}, + FindTest{`da(.)a$`, "daXY data", build(1, 5, 9, 7, 8)}, + + // can backslash-escape any punctuation + FindTest{`\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~`, + `!"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, build(1, 0, 31)}, + FindTest{`[\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~]+`, + `!"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, build(1, 0, 31)}, + FindTest{"\\`", "`", build(1, 0, 1)}, + FindTest{"[\\`]+", "`", build(1, 0, 1)}, +} + +// build is a helper to construct a [][]int by extracting n sequences from x. +// This represents n matches with len(x)/n submatches each. +func build(n int, x ...int) [][]int { + ret := make([][]int, n) + runLength := len(x) / n + j := 0 + for i := range ret { + ret[i] = make([]int, runLength) + copy(ret[i], x[j:]) + j += runLength + if j > len(x) { + panic("invalid build entry") + } + } + return ret +} + +// First the simple cases. + +func TestFind(t *testing.T) { + for _, test := range findTests { + result := MustCompile(test.pat).Find([]byte(test.text)) + switch { + case len(test.matches) == 0 && len(result) == 0: + // ok + case test.matches == nil && result != nil: + t.Errorf("expected no match; got one: %s", test) + case test.matches != nil && result == nil: + t.Errorf("expected match; got none: %s", test) + case test.matches != nil && result != nil: + expect := test.text[test.matches[0][0]:test.matches[0][1]] + if expect != string(result) { + t.Errorf("expected %q got %q: %s", expect, result, test) + } + } + } +} + +func TestFindString(t *testing.T) { + for _, test := range findTests { + result := MustCompile(test.pat).FindString(test.text) + switch { + case len(test.matches) == 0 && len(result) == 0: + // ok + case test.matches == nil && result != "": + t.Errorf("expected no match; got one: %s", test) + case test.matches != nil && result == "": + // Tricky because an empty result has two meanings: no match or empty match. + if test.matches[0][0] != test.matches[0][1] { + t.Errorf("expected match; got none: %s", test) + } + case test.matches != nil && result != "": + expect := test.text[test.matches[0][0]:test.matches[0][1]] + if expect != result { + t.Errorf("expected %q got %q: %s", expect, result, test) + } + } + } +} + +func testFindIndex(test *FindTest, result []int, t *testing.T) { + switch { + case len(test.matches) == 0 && len(result) == 0: + // ok + case test.matches == nil && result != nil: + t.Errorf("expected no match; got one: %s", test) + case test.matches != nil && result == nil: + t.Errorf("expected match; got none: %s", test) + case test.matches != nil && result != nil: + expect := test.matches[0] + if expect[0] != result[0] || expect[1] != result[1] { + t.Errorf("expected %v got %v: %s", expect, result, test) + } + } +} + +func TestFindIndex(t *testing.T) { + for _, test := range findTests { + testFindIndex(&test, MustCompile(test.pat).FindIndex([]byte(test.text)), t) + } +} + +func TestFindStringIndex(t *testing.T) { + for _, test := range findTests { + testFindIndex(&test, MustCompile(test.pat).FindStringIndex(test.text), t) + } +} + +// Now come the simple All cases. + +func TestFindAll(t *testing.T) { + for _, test := range findTests { + result := MustCompile(test.pat).FindAll([]byte(test.text), -1) + switch { + case test.matches == nil && result == nil: + // ok + case test.matches == nil && result != nil: + t.Errorf("expected no match; got one: %s", test) + case test.matches != nil && result == nil: + t.Errorf("expected match; got none: %s", test) + case test.matches != nil && result != nil: + if len(test.matches) != len(result) { + t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test) + continue + } + for k, e := range test.matches { + expect := test.text[e[0]:e[1]] + if expect != string(result[k]) { + t.Errorf("match %d: expected %q got %q: %s", k, expect, result[k], test) + } + } + } + } +} + +func TestFindAllString(t *testing.T) { + for _, test := range findTests { + result := MustCompile(test.pat).FindAllString(test.text, -1) + switch { + case test.matches == nil && result == nil: + // ok + case test.matches == nil && result != nil: + t.Errorf("expected no match; got one: %s", test) + case test.matches != nil && result == nil: + t.Errorf("expected match; got none: %s", test) + case test.matches != nil && result != nil: + if len(test.matches) != len(result) { + t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test) + continue + } + for k, e := range test.matches { + expect := test.text[e[0]:e[1]] + if expect != result[k] { + t.Errorf("expected %q got %q: %s", expect, result, test) + } + } + } + } +} + +func testFindAllIndex(test *FindTest, result [][]int, t *testing.T) { + switch { + case test.matches == nil && result == nil: + // ok + case test.matches == nil && result != nil: + t.Errorf("expected no match; got one: %s", test) + case test.matches != nil && result == nil: + t.Errorf("expected match; got none: %s", test) + case test.matches != nil && result != nil: + if len(test.matches) != len(result) { + t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test) + return + } + for k, e := range test.matches { + if e[0] != result[k][0] || e[1] != result[k][1] { + t.Errorf("match %d: expected %v got %v: %s", k, e, result[k], test) + } + } + } +} + +func TestFindAllIndex(t *testing.T) { + for _, test := range findTests { + testFindAllIndex(&test, MustCompile(test.pat).FindAllIndex([]byte(test.text), -1), t) + } +} + +func TestFindAllStringIndex(t *testing.T) { + for _, test := range findTests { + testFindAllIndex(&test, MustCompile(test.pat).FindAllStringIndex(test.text, -1), t) + } +} + +// Now come the Submatch cases. + +func testSubmatchBytes(test *FindTest, n int, submatches []int, result [][]byte, t *testing.T) { + if len(submatches) != len(result)*2 { + t.Errorf("match %d: expected %d submatches; got %d: %s", n, len(submatches)/2, len(result), test) + return + } + for k := 0; k < len(submatches); k += 2 { + if submatches[k] == -1 { + if result[k/2] != nil { + t.Errorf("match %d: expected nil got %q: %s", n, result, test) + } + continue + } + expect := test.text[submatches[k]:submatches[k+1]] + if expect != string(result[k/2]) { + t.Errorf("match %d: expected %q got %q: %s", n, expect, result, test) + return + } + } +} + +func TestFindSubmatch(t *testing.T) { + for _, test := range findTests { + result := MustCompile(test.pat).FindSubmatch([]byte(test.text)) + switch { + case test.matches == nil && result == nil: + // ok + case test.matches == nil && result != nil: + t.Errorf("expected no match; got one: %s", test) + case test.matches != nil && result == nil: + t.Errorf("expected match; got none: %s", test) + case test.matches != nil && result != nil: + testSubmatchBytes(&test, 0, test.matches[0], result, t) + } + } +} + +func testSubmatchString(test *FindTest, n int, submatches []int, result []string, t *testing.T) { + if len(submatches) != len(result)*2 { + t.Errorf("match %d: expected %d submatches; got %d: %s", n, len(submatches)/2, len(result), test) + return + } + for k := 0; k < len(submatches); k += 2 { + if submatches[k] == -1 { + if result[k/2] != "" { + t.Errorf("match %d: expected nil got %q: %s", n, result, test) + } + continue + } + expect := test.text[submatches[k]:submatches[k+1]] + if expect != result[k/2] { + t.Errorf("match %d: expected %q got %q: %s", n, expect, result, test) + return + } + } +} + +func TestFindStringSubmatch(t *testing.T) { + for _, test := range findTests { + result := MustCompile(test.pat).FindStringSubmatch(test.text) + switch { + case test.matches == nil && result == nil: + // ok + case test.matches == nil && result != nil: + t.Errorf("expected no match; got one: %s", test) + case test.matches != nil && result == nil: + t.Errorf("expected match; got none: %s", test) + case test.matches != nil && result != nil: + testSubmatchString(&test, 0, test.matches[0], result, t) + } + } +} + +func testSubmatchIndices(test *FindTest, n int, expect, result []int, t *testing.T) { + if len(expect) != len(result) { + t.Errorf("match %d: expected %d matches; got %d: %s", n, len(expect)/2, len(result)/2, test) + return + } + for k, e := range expect { + if e != result[k] { + t.Errorf("match %d: submatch error: expected %v got %v: %s", n, expect, result, test) + } + } +} + +func testFindSubmatchIndex(test *FindTest, result []int, t *testing.T) { + switch { + case test.matches == nil && result == nil: + // ok + case test.matches == nil && result != nil: + t.Errorf("expected no match; got one: %s", test) + case test.matches != nil && result == nil: + t.Errorf("expected match; got none: %s", test) + case test.matches != nil && result != nil: + testSubmatchIndices(test, 0, test.matches[0], result, t) + } +} + +func TestFindSubmatchIndex(t *testing.T) { + for _, test := range findTests { + testFindSubmatchIndex(&test, MustCompile(test.pat).FindSubmatchIndex([]byte(test.text)), t) + } +} + +func TestFindStringSubmatchndex(t *testing.T) { + for _, test := range findTests { + testFindSubmatchIndex(&test, MustCompile(test.pat).FindStringSubmatchIndex(test.text), t) + } +} + +// Now come the monster AllSubmatch cases. + +func TestFindAllSubmatch(t *testing.T) { + for _, test := range findTests { + result := MustCompile(test.pat).FindAllSubmatch([]byte(test.text), -1) + switch { + case test.matches == nil && result == nil: + // ok + case test.matches == nil && result != nil: + t.Errorf("expected no match; got one: %s", test) + case test.matches != nil && result == nil: + t.Errorf("expected match; got none: %s", test) + case len(test.matches) != len(result): + t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test) + case test.matches != nil && result != nil: + for k, match := range test.matches { + testSubmatchBytes(&test, k, match, result[k], t) + } + } + } +} + +func TestFindAllStringSubmatch(t *testing.T) { + for _, test := range findTests { + result := MustCompile(test.pat).FindAllStringSubmatch(test.text, -1) + switch { + case test.matches == nil && result == nil: + // ok + case test.matches == nil && result != nil: + t.Errorf("expected no match; got one: %s", test) + case test.matches != nil && result == nil: + t.Errorf("expected match; got none: %s", test) + case len(test.matches) != len(result): + t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test) + case test.matches != nil && result != nil: + for k, match := range test.matches { + testSubmatchString(&test, k, match, result[k], t) + } + } + } +} + +func testFindAllSubmatchIndex(test *FindTest, result [][]int, t *testing.T) { + switch { + case test.matches == nil && result == nil: + // ok + case test.matches == nil && result != nil: + t.Errorf("expected no match; got one: %s", test) + case test.matches != nil && result == nil: + t.Errorf("expected match; got none: %s", test) + case len(test.matches) != len(result): + t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test) + case test.matches != nil && result != nil: + for k, match := range test.matches { + testSubmatchIndices(test, k, match, result[k], t) + } + } +} + +func TestFindAllSubmatchIndex(t *testing.T) { + for _, test := range findTests { + testFindAllSubmatchIndex(&test, MustCompile(test.pat).FindAllSubmatchIndex([]byte(test.text), -1), t) + } +} + +func TestFindAllStringSubmatchndex(t *testing.T) { + for _, test := range findTests { + testFindAllSubmatchIndex(&test, MustCompile(test.pat).FindAllStringSubmatchIndex(test.text, -1), t) + } +} diff --git a/src/pkg/regexp/regexp.go b/src/pkg/regexp/regexp.go index aa90eb321b6..39a6b0cdeed 100644 --- a/src/pkg/regexp/regexp.go +++ b/src/pkg/regexp/regexp.go @@ -19,11 +19,41 @@ // '[' [ '^' ] { character-range } ']' // '(' regexp ')' // character-range: -// character '-' character +// character [ '-' character ] // -// All characters are UTF-8-encoded code points. -// Backslashes escape special characters, including inside -// character classes. +// All characters are UTF-8-encoded code points. Backslashes escape special +// characters, including inside character classes. +// +// There are 16 methods of Regexp that match a regular expression and identify +// the matched text. Their names are matched by this regular expression: +// +// Find(All)?(String)?(Submatch)?(Index)? +// +// If 'All' is present, the routine matches successive non-overlapping +// matches of the entire expression. Empty matches abutting a preceding +// match are ignored. The return value is a slice containing the successive +// return values of the corresponding non-'All' routine. These routines take +// an extra integer argument, n; if n >= 0, the function returns at most n +// matches/submatches. +// +// If 'String' is present, the argument is a string; otherwise it is a slice +// of bytes; return values are adjusted as appropriate. +// +// If 'Submatch' is present, the return value is a slice identifying the +// successive submatches of the expression. Submatches are matches of +// parenthesized subexpressions within the regular expression, numbered from +// left to right in order of opening parenthesis. Submatch 0 is the match of +// the entire expression, submatch 1 the match of the first parenthesized +// subexpression, and so on. +// +// If 'Index' is present, matches and submatches are identified by byte index +// pairs within the input string: result[2*n:2*n+1] identifies the indexes of +// the nth submatch. The pair for n==0 identifies the match of the entire +// expression. If 'Index' is not present, the match is identified by the +// text of the match/submatch. If an index is negative, it means that +// subexpression did not match any string in the input. +// +// (There are a few other methods that do not match this pattern.) // package regexp @@ -814,7 +844,7 @@ func (re *Regexp) doExecute(str string, bytestr []byte, pos int) []int { advance = bytes.Index(bytestr[pos:], re.prefixBytes) } if advance == -1 { - return []int{} + return nil } pos += advance + len(re.prefix) prefixed = true @@ -914,6 +944,7 @@ func (re *Regexp) doExecute(str string, bytestr []byte, pos int) []int { // s[a[2*i]:a[2*i+1]] for i > 0 is the substring matched by the ith parenthesized subexpression. // A negative value means the subexpression did not match any element of the string. // An empty array means "no match". +// Deprecated; use FindString. func (re *Regexp) ExecuteString(s string) (a []int) { return re.doExecute(s, nil, 0) } @@ -926,6 +957,7 @@ func (re *Regexp) ExecuteString(s string) (a []int) { // b[a[2*i]:a[2*i+1]] for i > 0 is the subslice matched by the ith parenthesized subexpression. // A negative value means the subexpression did not match any element of the slice. // An empty array means "no match". +// Deprecated; use Find. func (re *Regexp) Execute(b []byte) (a []int) { return re.doExecute("", b, 0) } @@ -944,6 +976,7 @@ func (re *Regexp) Match(b []byte) bool { return len(re.doExecute("", b, 0)) > 0 // a[0] is the substring matched by the entire expression. // a[i] for i > 0 is the substring matched by the ith parenthesized subexpression. // An empty array means ``no match''. +// Deprecated; use FindStringSubmatch. func (re *Regexp) MatchStrings(s string) (a []string) { r := re.doExecute(s, nil, 0) if r == nil { @@ -963,6 +996,7 @@ func (re *Regexp) MatchStrings(s string) (a []string) { // a[0] is the subslice matched by the entire expression. // a[i] for i > 0 is the subslice matched by the ith parenthesized subexpression. // An empty array means ``no match''. +// Deprecated; use FindSubmatch. func (re *Regexp) MatchSlices(b []byte) (a [][]byte) { r := re.doExecute("", b, 0) if r == nil { @@ -1123,7 +1157,7 @@ func QuoteMeta(s string) string { } // Find matches in slice b if b is non-nil, otherwise find matches in string s. -func (re *Regexp) allMatches(s string, b []byte, n int, deliver func(int, int)) { +func (re *Regexp) allMatches(s string, b []byte, n int, deliver func([]int)) { var end int if b == nil { end = len(s) @@ -1162,7 +1196,7 @@ func (re *Regexp) allMatches(s string, b []byte, n int, deliver func(int, int)) prevMatchEnd = matches[1] if accept { - deliver(matches[0], matches[1]) + deliver(matches) i++ } } @@ -1173,14 +1207,18 @@ func (re *Regexp) allMatches(s string, b []byte, n int, deliver func(int, int)) // matches. Text that does not match the expression will be skipped. Empty // matches abutting a preceding match are ignored. The function returns a slice // containing the matching substrings. +// Deprecated; use FindAll. func (re *Regexp) AllMatches(b []byte, n int) [][]byte { if n <= 0 { n = len(b) + 1 } + // TODO: here and below, grow the result slice on demand + // to avoid allocating a huge slice for a small result and + // wasting memory. result := make([][]byte, n) i := 0 - re.allMatches("", b, n, func(start, end int) { - result[i] = b[start:end] + re.allMatches("", b, n, func(match []int) { + result[i] = b[match[0]:match[1]] i++ }) return result[0:i] @@ -1191,14 +1229,15 @@ func (re *Regexp) AllMatches(b []byte, n int) [][]byte { // matches. Text that does not match the expression will be skipped. Empty // matches abutting a preceding match are ignored. The function returns a slice // containing the matching substrings. +// Deprecated; use FindAllString. func (re *Regexp) AllMatchesString(s string, n int) []string { if n <= 0 { n = len(s) + 1 } result := make([]string, n) i := 0 - re.allMatches(s, nil, n, func(start, end int) { - result[i] = s[start:end] + re.allMatches(s, nil, n, func(match []int) { + result[i] = s[match[0]:match[1]] i++ }) return result[0:i] @@ -1215,7 +1254,7 @@ func (re *Regexp) AllMatchesIter(b []byte, n int) <-chan []byte { } c := make(chan []byte, 10) go func() { - re.allMatches("", b, n, func(start, end int) { c <- b[start:end] }) + re.allMatches("", b, n, func(match []int) { c <- b[match[0]:match[1]] }) close(c) }() return c @@ -1232,8 +1271,284 @@ func (re *Regexp) AllMatchesStringIter(s string, n int) <-chan string { } c := make(chan string, 10) go func() { - re.allMatches(s, nil, n, func(start, end int) { c <- s[start:end] }) + re.allMatches(s, nil, n, func(match []int) { c <- s[match[0]:match[1]] }) close(c) }() return c } + +// Find returns a slice holding the text of the leftmost match in b of the regular expression. +// A return value of nil indicates no match. +func (re *Regexp) Find(b []byte) []byte { + a := re.doExecute("", b, 0) + if a == nil { + return nil + } + return b[a[0]:a[1]] +} + +// FindIndex returns a two-element slice of integers defining the location of +// the leftmost match in b of the regular expression. The match itself is at +// b[loc[0]:loc[1]]. +// A return value of nil indicates no match. +func (re *Regexp) FindIndex(b []byte) (loc []int) { + a := re.doExecute("", b, 0) + if a == nil { + return nil + } + return a[0:2] +} + +// FindString returns a string holding the text of the leftmost match in s of the regular +// expression. If there is no match, the return value is an empty string, +// but it will also be empty if the regular expression successfully matches +// an empty string. Use FindStringIndex or FindStringSubmatch if it is +// necessary to distinguish these cases. +func (re *Regexp) FindString(s string) string { + a := re.doExecute(s, nil, 0) + if a == nil { + return "" + } + return s[a[0]:a[1]] +} + +// FindStringIndex returns a two-element slice of integers defining the +// location of the leftmost match in s of the regular expression. The match +// itself is at s[loc[0]:loc[1]]. +// A return value of nil indicates no match. +func (re *Regexp) FindStringIndex(s string) []int { + a := re.doExecute(s, nil, 0) + if a == nil { + return nil + } + return a[0:2] +} + +// FindSubmatch returns a slice of slices holding the text of the leftmost +// match of the regular expression in b and the matches, if any, of its +// subexpressions, as defined by the 'Submatch' descriptions in the package +// comment. +// A return value of nil indicates no match. +func (re *Regexp) FindSubmatch(b []byte) [][]byte { + a := re.doExecute("", b, 0) + if a == nil { + return nil + } + ret := make([][]byte, len(a)/2) + for i := range ret { + if a[2*i] >= 0 { + ret[i] = b[a[2*i]:a[2*i+1]] + } + } + return ret +} + +// FindSubmatchIndex returns a slice holding the index pairs identifying the +// leftmost match of the regular expression in b and the matches, if any, of +// its subexpressions, as defined by the 'Submatch' and 'Index' descriptions +// in the package comment. +// A return value of nil indicates no match. +func (re *Regexp) FindSubmatchIndex(b []byte) []int { + return re.doExecute("", b, 0) +} + +// FindStringSubmatch returns a slice of strings holding the text of the +// leftmost match of the regular expression in s and the matches, if any, of +// its subexpressions, as defined by the 'Submatch' description in the +// package comment. +// A return value of nil indicates no match. +func (re *Regexp) FindStringSubmatch(s string) []string { + a := re.doExecute(s, nil, 0) + if a == nil { + return nil + } + ret := make([]string, len(a)/2) + for i := range ret { + if a[2*i] >= 0 { + ret[i] = s[a[2*i]:a[2*i+1]] + } + } + return ret +} + +// FindStringSubmatchIndex returns a slice holding the index pairs +// identifying the leftmost match of the regular expression in s and the +// matches, if any, of its subexpressions, as defined by the 'Submatch' and +// 'Index' descriptions in the package comment. +// A return value of nil indicates no match. +func (re *Regexp) FindStringSubmatchIndex(s string) []int { + return re.doExecute(s, nil, 0) +} + +// FindAll is the 'All' version of Find; it returns a slice of all successive +// matches of the expression, as defined by the 'All' description in the +// package comment. +// A return value of nil indicates no match. +func (re *Regexp) FindAll(b []byte, n int) [][]byte { + if n < 0 { + n = len(b) + 1 + } + result := make([][]byte, n) + i := 0 + re.allMatches("", b, n, func(match []int) { + result[i] = b[match[0]:match[1]] + i++ + }) + if i == 0 { + return nil + } + return result[0:i] +} + +// FindAllIndex is the 'All' version of FindIndex; it returns a slice of all +// successive matches of the expression, as defined by the 'All' description +// in the package comment. +// A return value of nil indicates no match. +func (re *Regexp) FindAllIndex(b []byte, n int) [][]int { + if n < 0 { + n = len(b) + 1 + } + result := make([][]int, n) + i := 0 + re.allMatches("", b, n, func(match []int) { + result[i] = match[0:2] + i++ + }) + if i == 0 { + return nil + } + return result[0:i] +} + +// FindAllString is the 'All' version of FindString; it returns a slice of all +// successive matches of the expression, as defined by the 'All' description +// in the package comment. +// A return value of nil indicates no match. +func (re *Regexp) FindAllString(s string, n int) []string { + if n < 0 { + n = len(s) + 1 + } + result := make([]string, n) + i := 0 + re.allMatches(s, nil, n, func(match []int) { + result[i] = s[match[0]:match[1]] + i++ + }) + if i == 0 { + return nil + } + return result[0:i] +} + +// FindAllStringIndex is the 'All' version of FindStringIndex; it returns a +// slice of all successive matches of the expression, as defined by the 'All' +// description in the package comment. +// A return value of nil indicates no match. +func (re *Regexp) FindAllStringIndex(s string, n int) [][]int { + if n < 0 { + n = len(s) + 1 + } + result := make([][]int, n) + i := 0 + re.allMatches(s, nil, n, func(match []int) { + result[i] = match[0:2] + i++ + }) + if i == 0 { + return nil + } + return result[0:i] +} + +// FindAllSubmatch is the 'All' version of FindSubmatch; it returns a slice +// of all successive matches of the expression, as defined by the 'All' +// description in the package comment. +// A return value of nil indicates no match. +func (re *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte { + if n < 0 { + n = len(b) + 1 + } + result := make([][][]byte, n) + i := 0 + re.allMatches("", b, n, func(match []int) { + slice := make([][]byte, len(match)/2) + for j := range slice { + if match[2*j] >= 0 { + slice[j] = b[match[2*j]:match[2*j+1]] + } + } + result[i] = slice + i++ + }) + if i == 0 { + return nil + } + return result[0:i] +} + +// FindAllSubmatchIndex is the 'All' version of FindSubmatchIndex; it returns +// a slice of all successive matches of the expression, as defined by the +// 'All' description in the package comment. +// A return value of nil indicates no match. +func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int { + if n < 0 { + n = len(b) + 1 + } + result := make([][]int, n) + i := 0 + re.allMatches("", b, n, func(match []int) { + result[i] = match + i++ + }) + if i == 0 { + return nil + } + return result[0:i] +} + +// FindAllStringSubmatch is the 'All' version of FindStringSubmatch; it +// returns a slice of all successive matches of the expression, as defined by +// the 'All' description in the package comment. +// A return value of nil indicates no match. +func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string { + if n < 0 { + n = len(s) + 1 + } + result := make([][]string, n) + i := 0 + re.allMatches(s, nil, n, func(match []int) { + slice := make([]string, len(match)/2) + for j := range slice { + if match[2*j] >= 0 { + slice[j] = s[match[2*j]:match[2*j+1]] + } + } + result[i] = slice + i++ + }) + if i == 0 { + return nil + } + return result[0:i] +} + +// FindAllStringSubmatchIndex is the 'All' version of +// FindStringSubmatchIndex; it returns a slice of all successive matches of +// the expression, as defined by the 'All' description in the package +// comment. +// A return value of nil indicates no match. +func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int { + if n < 0 { + n = len(s) + 1 + } + result := make([][]int, n) + i := 0 + re.allMatches(s, nil, n, func(match []int) { + result[i] = match + i++ + }) + if i == 0 { + return nil + } + return result[0:i] +}