You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
422 lines
18 KiB
Go
422 lines
18 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"testing"
|
|
)
|
|
|
|
var reTests = []struct {
|
|
re string
|
|
flags []ReFlag
|
|
str string
|
|
result []Group // Stores all zero-groups in the match
|
|
}{
|
|
{"a", nil, "abc", []Group{{0, 1}}},
|
|
{"a", nil, "bca", []Group{{2, 3}}},
|
|
{"l", nil, "ggllgg", []Group{{2, 3}, {3, 4}}},
|
|
{"(b|c)", nil, "abdceb", []Group{{1, 2}, {3, 4}, {5, 6}}},
|
|
{"a+", nil, "brerereraaaaabbbbb", []Group{{8, 13}}},
|
|
{"ab+", nil, "qweqweqweaqweqweabbbbbr", []Group{{16, 22}}},
|
|
{"(b|c|A)", nil, "ooaoobocA", []Group{{5, 6}, {7, 8}, {8, 9}}},
|
|
{"ab*", nil, "a", []Group{{0, 1}}},
|
|
{"ab*", nil, "abb", []Group{{0, 3}}},
|
|
{"a*b", nil, "aaab", []Group{{0, 4}}},
|
|
{"a*b", nil, "qwqw", []Group{}},
|
|
{"(abc)*", nil, "abcabcabc", []Group{{0, 9}, {9, 9}}},
|
|
{"((abc)|(def))*", nil, "abcdef", []Group{{0, 6}, {6, 6}}},
|
|
{"(abc)*|(def)*", nil, "abcdef", []Group{{0, 3}, {3, 6}, {6, 6}}},
|
|
{"b*a*a", nil, "bba", []Group{{0, 3}}},
|
|
{"(ab)+", nil, "abcabddd", []Group{{0, 2}, {3, 5}}},
|
|
{"a(b(c|d)*)*", nil, "abccbd", []Group{{0, 6}}},
|
|
{"a(b|c)*d+", nil, "abccdd", []Group{{0, 6}}},
|
|
{"a*", nil, "", []Group{{0, 0}}},
|
|
{"a|b", nil, "c", []Group{}},
|
|
{"(a|b)*c", nil, "aabbc", []Group{{0, 5}}},
|
|
{"a(b|b)", nil, "ab", []Group{{0, 2}}},
|
|
{"a*", nil, "aaaaaaaa", []Group{{0, 8}, {8, 8}}},
|
|
|
|
{"ab?", nil, "ab", []Group{{0, 2}}},
|
|
{"a?b", nil, "ab", []Group{{0, 2}}},
|
|
{"a?", nil, "", []Group{{0, 0}}},
|
|
{"a?b?c", nil, "a", []Group{}},
|
|
{"a?b?c?", nil, "ab", []Group{{0, 2}, {2, 2}}},
|
|
{"a?b?c?", nil, "ac", []Group{{0, 2}, {2, 2}}},
|
|
{"a?b?c", nil, "abc", []Group{{0, 3}}},
|
|
{"a?b?c", nil, "acb", []Group{{0, 2}}},
|
|
|
|
{"[abc]", nil, "defadefbdefce", []Group{{3, 4}, {7, 8}, {11, 12}}},
|
|
{"[ab]c", nil, "ab", []Group{}},
|
|
{"g[ab]c", nil, "gac", []Group{{0, 3}}},
|
|
{"g[ab]c", nil, "gbc", []Group{{0, 3}}},
|
|
{"g[ab]c", nil, "gc", []Group{}},
|
|
{"g[ab]c", nil, "gfc", []Group{}},
|
|
{"[ab]*", nil, "aabbbabaababab", []Group{{0, 14}, {14, 14}}},
|
|
{"[ab]+", nil, "aabbbablaababab", []Group{{0, 7}, {8, 15}}},
|
|
{"[Ff]r[Uu]it", nil, "fruit", []Group{{0, 5}}},
|
|
{"[Ff]r[Uu]it", nil, "FrUit", []Group{{0, 5}}},
|
|
{"[Ff]r[Uu|]it", nil, "Fr|it", []Group{{0, 5}}},
|
|
{"[Ff]r([Uu]|[pP])it", nil, "Frpit", []Group{{0, 5}}},
|
|
{"[Ff]r[Uu]|[pP]it", nil, "Frpit", []Group{{2, 5}}},
|
|
{"[a-zA-Z]+", nil, "Hello, how is it going?", []Group{{0, 5}, {7, 10}, {11, 13}, {14, 16}, {17, 22}}},
|
|
|
|
{".+", nil, "Hello, how is it going?", []Group{{0, 23}}},
|
|
{"a.", nil, "a ", []Group{{0, 2}}},
|
|
{"a.b", nil, "a/b", []Group{{0, 3}}},
|
|
{".", nil, "a ", []Group{{0, 1}, {1, 2}}},
|
|
{"a.", nil, "a ", []Group{{0, 2}}},
|
|
{".+b", nil, "abc", []Group{{0, 2}}},
|
|
|
|
{`\d`, nil, "1a0a3s'''34343s", []Group{{0, 1}, {2, 3}, {4, 5}, {9, 10}, {10, 11}, {11, 12}, {12, 13}, {13, 14}}},
|
|
{`\\`, nil, `a\b\c\qwe\`, []Group{{1, 2}, {3, 4}, {5, 6}, {9, 10}}},
|
|
{`\W`, nil, `"Hello", he said. How are you doing?`, []Group{{0, 1}, {6, 7}, {7, 8}, {8, 9}, {11, 12}, {16, 17}, {17, 18}, {21, 22}, {25, 26}, {29, 30}, {35, 36}}},
|
|
{`\w`, nil, ";';';';';'qwe12", []Group{{10, 11}, {11, 12}, {12, 13}, {13, 14}, {14, 15}}},
|
|
{`\s`, nil, "a b c d", []Group{{1, 2}, {3, 4}, {5, 6}, {6, 7}}},
|
|
{`\<`, nil, "<HTML><body>", []Group{{0, 1}, {6, 7}}},
|
|
{`\(.+\)`, nil, "Not (paranthesized), (so) is (this) not", []Group{{4, 35}}},
|
|
|
|
{"[^abc]+", nil, "qarbtopsaplpclkpasdmb prejip0r,p", []Group{{0, 1}, {2, 3}, {4, 8}, {9, 12}, {13, 16}, {17, 20}, {21, 32}}},
|
|
{"[^a]+", nil, "qqqaq", []Group{{0, 3}, {4, 5}}},
|
|
{"[^0-9]+", nil, "a1b2c3dd", []Group{{0, 1}, {2, 3}, {4, 5}, {6, 8}}},
|
|
{"[^abc]+", nil, "ababababbababaccacacacaca", []Group{}},
|
|
{`\[`, nil, "a[b[c[]]]", []Group{{1, 2}, {3, 4}, {5, 6}}},
|
|
{`\([^)]+\)`, nil, "Not (paranthesized), (so) is (this) not", []Group{{4, 19}, {21, 25}, {29, 35}}},
|
|
|
|
{"^ab", nil, "ab bab", []Group{{0, 2}}},
|
|
{"^aaaa^", nil, "aaaaaaaa", []Group{}},
|
|
{"^([bB][Gg])", nil, "bG", []Group{{0, 2}}},
|
|
{"b$", nil, "ba", []Group{}},
|
|
{"(boy|girl)$", nil, "girlf", []Group{}},
|
|
{`\bint\b`, nil, "print int integer", []Group{{6, 9}}},
|
|
{`int\b`, nil, "ints", []Group{}},
|
|
{`int(\b|a)`, nil, "inta", []Group{{0, 4}}},
|
|
{`\b\d+\b`, nil, "511 a3 43", []Group{{0, 3}, {7, 9}}},
|
|
{`\Bint\B`, nil, "prints int integer print", []Group{{2, 5}}},
|
|
{`^`, nil, "5^3^2", []Group{{0, 0}}},
|
|
{`\^`, nil, "5^3^2", []Group{{1, 2}, {3, 4}}},
|
|
{`pool$`, nil, "pool carpool", []Group{{8, 12}}},
|
|
{`^int$`, nil, "print int integer", []Group{}},
|
|
{`^int$`, nil, "int", []Group{{0, 3}}},
|
|
{`b*`, nil, "aaaaaaaaaaqweqwe", []Group{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}, {10, 10}, {11, 11}, {12, 12}, {13, 13}, {14, 14}, {15, 15}, {16, 16}}},
|
|
|
|
{"a{4}", nil, "aabaaa", []Group{}},
|
|
{"ab{5}", nil, "abbbbbab", []Group{{0, 6}}},
|
|
{"(a|b){3,4}", nil, "aba", []Group{{0, 3}}},
|
|
{"(a|b){3,4}", nil, "ababaa", []Group{{0, 4}}},
|
|
{"(bc){5,}", nil, "bcbcbcbcbcbcbcbc", []Group{{0, 16}}},
|
|
{`\d{3,4}`, nil, "1209", []Group{{0, 4}}},
|
|
{`\d{3,4}`, nil, "109", []Group{{0, 3}}},
|
|
{`\d{3,4}`, nil, "5", []Group{}},
|
|
{`\d{3,4}`, nil, "123135", []Group{{0, 4}}},
|
|
{`\d{3,4}`, nil, "89a-0", []Group{}},
|
|
{`\d{3,4}`, nil, "ababab555", []Group{{6, 9}}},
|
|
{`\bpaint\b`, nil, "paints", []Group{}},
|
|
{`\b\w{5}\b`, nil, "paint", []Group{{0, 5}}},
|
|
{`[^\w]`, nil, "abcdef1230[]qq';;'", []Group{{10, 11}, {11, 12}, {14, 15}, {15, 16}, {16, 17}, {17, 18}}},
|
|
{`[^\W]`, nil, "abcdef1230[]qq';;'", []Group{{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9, 10}, {12, 13}, {13, 14}}},
|
|
{`[\[\]]`, nil, "a[b[l]]", []Group{{1, 2}, {3, 4}, {5, 6}, {6, 7}}},
|
|
|
|
// Unicode tests
|
|
{`.+`, nil, "úïäö´«åæïëòöê»éãçâï«úïòíñ", []Group{{0, 25}}},
|
|
{`a.b`, nil, "a²b", []Group{{0, 3}}},
|
|
{`[^a]+`, nil, "úïäö´«åæïëòöê»éãçâï«úïòíñ", []Group{{0, 25}}},
|
|
|
|
// Fun experiment - AI-generated tests
|
|
{"(abc|def|ghi)", nil, "abcdefg", []Group{{0, 3}, {3, 6}}},
|
|
{"a(b|c)d", nil, "abcd", []Group{}},
|
|
{"a(b|c)*d", nil, "abcbcd", []Group{{0, 6}}},
|
|
{"a(b|c)+d", nil, "abcbcd", []Group{{0, 6}}},
|
|
{"a(b|c)?d", nil, "abd", []Group{{0, 3}}},
|
|
{".+", nil, "hello world", []Group{{0, 11}}},
|
|
{"a.b", nil, "aXb", []Group{{0, 3}}},
|
|
{"a.*b", nil, "aXb", []Group{{0, 3}}},
|
|
{"a.{2,3}b", nil, "aXXb", []Group{{0, 4}}},
|
|
{"a.{2,}b", nil, "aXXXb", []Group{{0, 5}}},
|
|
{"a.{0,3}b", nil, "ab", []Group{{0, 2}}},
|
|
{"[abc]+", nil, "abcabc", []Group{{0, 6}}},
|
|
{"[a-zA-Z]+", nil, "HelloWorld", []Group{{0, 10}}},
|
|
{"[^abc]+", nil, "defghi", []Group{{0, 6}}},
|
|
{"^hello", nil, "hello world", []Group{{0, 5}}},
|
|
{"world$", nil, "hello world", []Group{{6, 11}}},
|
|
{`\bhello\b`, nil, "hello world", []Group{{0, 5}}},
|
|
{`\Bhello\B`, nil, "hello world", []Group{}},
|
|
{"(hello|world)", nil, "hello world", []Group{{0, 5}, {6, 11}}},
|
|
{"(hello|world)+", nil, "hello world", []Group{{0, 5}, {6, 11}}},
|
|
{"(hello|world)*", nil, "hello world", []Group{{0, 5}, {5, 5}, {6, 11}, {11, 11}}},
|
|
{"(hello|world)?", nil, "hello world", []Group{{0, 5}, {5, 5}, {6, 11}, {11, 11}}},
|
|
{"ú.+ï", nil, "úïäö´«åæïëòöê»éãçâï«úïòíñ", []Group{{0, 22}}},
|
|
{"(?=hello)", nil, "hello world", []Group{{0, 0}}},
|
|
{"(?!hello)", nil, "hello world", []Group{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}, {10, 10}, {11, 11}}},
|
|
{"(?<=hello)", nil, "hello world", []Group{{5, 5}}},
|
|
{"(?<!hello)", nil, "hello world", []Group{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {6, 6}, {7, 7}, {8, 8}, {9, 9}, {10, 10}, {11, 11}}},
|
|
{"^((3[7-9])|([4-9][0-9])|([1-9][0-9][0-9])|(1000))$", nil, "40", []Group{{0, 2}}},
|
|
{"^((3[7-9])|([4-9][0-9])|([1-9][0-9][0-9])|(1000))$", nil, "040", []Group{}},
|
|
{"^((3[7-9])|([4-9][0-9])|([1-9][0-9][0-9])|(1000))$", nil, "400", []Group{{0, 3}}},
|
|
{"^((3[7-9])|([4-9][0-9])|([1-9][0-9][0-9])|(1000))$", nil, "4000", []Group{}},
|
|
{"a{1,3}", nil, "aaaaa", []Group{{0, 3}, {3, 5}}},
|
|
{`\\[ab\\]`, nil, "a", []Group{}},
|
|
{`\\[ab\\]`, nil, `\a`, []Group{{0, 2}}},
|
|
|
|
// Lookaround tests
|
|
{"(?<=bo)y", nil, "boy", []Group{{2, 3}}},
|
|
{"bo(?=y)", nil, "boy", []Group{{0, 2}}},
|
|
{"(?<=f)f+(?=f)", nil, "fffff", []Group{{1, 4}}},
|
|
{"(?<=f)f+(?=f)", nil, "fffffa", []Group{{1, 4}}},
|
|
|
|
// Test cases from Python's RE test suite
|
|
{`[\1]`, nil, "\x01", []Group{{0, 1}}},
|
|
|
|
{`\0`, nil, "\x00", []Group{{0, 1}}},
|
|
{`[\0a]`, nil, "\x00", []Group{{0, 1}}},
|
|
{`[\0a]`, nil, "\x00", []Group{{0, 1}}},
|
|
{`[a\0]`, nil, "\x00", []Group{{0, 1}}},
|
|
{`[^a\0]`, nil, "\x00", []Group{}},
|
|
|
|
{`\a[\b]\f\n\r\t\v`, nil, "\a\b\f\n\r\t\v", []Group{{0, 7}}},
|
|
{`[\a][\b][\f][\n][\r][\t][\v]`, nil, "\a\b\f\n\r\t\v", []Group{{0, 7}}},
|
|
{`\u`, nil, "", nil},
|
|
{`\xff`, nil, "ÿ", []Group{{0, 1}}},
|
|
{`\x00ffffffffffffff`, nil, "\xff", []Group{}},
|
|
{`\x00f`, nil, "\x0f", []Group{}},
|
|
{`\x00fe`, nil, "\xfe", []Group{}},
|
|
{`^\w+=(\\[\000-\277]|[^\n\\])*`, nil, "SRC=eval.c g.c blah blah blah \\\\\n\tapes.c", []Group{{0, 32}}},
|
|
|
|
{`a.b`, nil, `acb`, []Group{{0, 3}}},
|
|
{`a.b`, nil, "a\nb", []Group{}},
|
|
{`a.*b`, nil, "acc\nccb", []Group{}},
|
|
{`a.{4,5}b`, nil, "acc\nccb", []Group{}},
|
|
{`a.b`, nil, "a\rb", []Group{{0, 3}}},
|
|
{`a.b`, []ReFlag{RE_MULTILINE}, "a\nb", []Group{{0, 3}}},
|
|
{`a.*b`, []ReFlag{RE_MULTILINE}, "acc\nccb", []Group{{0, 7}}},
|
|
{`a.{4,5}b`, []ReFlag{RE_MULTILINE}, "acc\nccb", []Group{{0, 7}}},
|
|
|
|
{`)`, nil, ``, nil},
|
|
{`^$`, nil, ``, []Group{{0, 0}}},
|
|
{`abc`, nil, `abc`, []Group{{0, 3}}},
|
|
{`abc`, nil, `xbc`, []Group{}},
|
|
{`abc`, nil, `axc`, []Group{}},
|
|
{`abc`, nil, `abx`, []Group{}},
|
|
{`abc`, nil, `xabcy`, []Group{{1, 4}}},
|
|
{`abc`, nil, `ababc`, []Group{{2, 5}}},
|
|
{`ab*c`, nil, `abc`, []Group{{0, 3}}},
|
|
{`ab*bc`, nil, `abc`, []Group{{0, 3}}},
|
|
{`ab*bc`, nil, `abbc`, []Group{{0, 4}}},
|
|
{`ab*bc`, nil, `abbbbc`, []Group{{0, 6}}},
|
|
{`ab+bc`, nil, `abbc`, []Group{{0, 4}}},
|
|
{`ab+bc`, nil, `abc`, []Group{}},
|
|
{`ab+bc`, nil, `abq`, []Group{}},
|
|
{`ab+bc`, nil, `abbbbc`, []Group{{0, 6}}},
|
|
{`ab?bc`, nil, `abbc`, []Group{{0, 4}}},
|
|
{`ab?bc`, nil, `abc`, []Group{{0, 3}}},
|
|
{`ab?bc`, nil, `abbbbc`, []Group{}},
|
|
{`ab?c`, nil, `abc`, []Group{{0, 3}}},
|
|
{`^abc$`, nil, `abc`, []Group{{0, 3}}},
|
|
{`^abc$`, nil, `abcc`, []Group{}},
|
|
{`^abc`, nil, `abcc`, []Group{{0, 3}}},
|
|
{`^abc$`, nil, `aabc`, []Group{}},
|
|
{`abc$`, nil, `aabc`, []Group{{1, 4}}},
|
|
{`^`, nil, `abc`, []Group{{0, 0}}},
|
|
{`$`, nil, `abc`, []Group{{3, 3}}},
|
|
{`a.c`, nil, `abc`, []Group{{0, 3}}},
|
|
{`a.c`, nil, `axc`, []Group{{0, 3}}},
|
|
{`a.*c`, nil, `axyzc`, []Group{{0, 5}}},
|
|
{`a.*c`, nil, `axyzd`, []Group{}},
|
|
{`a[bc]d`, nil, `abc`, []Group{}},
|
|
{`a[bc]d`, nil, `abd`, []Group{{0, 3}}},
|
|
{`a[b-d]e`, nil, `abd`, []Group{}},
|
|
{`a[b-d]e`, nil, `ace`, []Group{{0, 3}}},
|
|
{`a[b-d]`, nil, `aac`, []Group{{1, 3}}},
|
|
{`a[-b]`, nil, `a-`, []Group{{0, 2}}}, // If a character class has a hyphen without a start or end character, it is treated as a literal hyphen
|
|
{`a[\-b]`, nil, `a-`, []Group{{0, 2}}},
|
|
{`a[b-]`, nil, `a-`, []Group{{0, 2}}}, // If a character class has a hyphen without a start or end character, it is treated as a literal hyphen
|
|
|
|
{`a[]b`, nil, `-`, nil},
|
|
{`a[`, nil, `-`, nil},
|
|
{`a\`, nil, `-`, nil},
|
|
{`abc)`, nil, `-`, nil},
|
|
{`(abc`, nil, `-`, nil},
|
|
{`a]`, nil, `a]`, []Group{{0, 2}}},
|
|
{`a[]]b`, nil, `a]b`, []Group{{0, 3}}},
|
|
{`a[\]]b`, nil, `a]b`, []Group{{0, 3}}},
|
|
{`a[^bc]d`, nil, `aed`, []Group{{0, 3}}},
|
|
{`a[^bc]d`, nil, `abd`, []Group{}},
|
|
{`a[^-b]c`, nil, `adc`, []Group{{0, 3}}},
|
|
{`a[^-b]c`, nil, `a-c`, []Group{}},
|
|
{`a[^]b]c`, nil, `a]c`, []Group{}},
|
|
{`a[^]b]c`, nil, `adc`, []Group{{0, 3}}},
|
|
{`\ba\b`, nil, `a-`, []Group{{0, 1}}},
|
|
{`\ba\b`, nil, `-a`, []Group{{1, 2}}},
|
|
{`\ba\b`, nil, `-a-`, []Group{{1, 2}}},
|
|
{`\by\b`, nil, `xy`, []Group{}},
|
|
{`\by\b`, nil, `yz`, []Group{}},
|
|
{`\by\b`, nil, `xyz`, []Group{}},
|
|
{`x\b`, nil, `xyz`, []Group{}},
|
|
{`x\B`, nil, `xyz`, []Group{{0, 1}}},
|
|
{`\Bz`, nil, `xyz`, []Group{{2, 3}}},
|
|
{`z\B`, nil, `xyz`, []Group{}},
|
|
{`\Bx`, nil, `xyz`, []Group{}},
|
|
{`\Ba\B`, nil, `a-`, []Group{}},
|
|
{`\Ba\B`, nil, `-a`, []Group{}},
|
|
{`\Ba\B`, nil, `-a-`, []Group{}},
|
|
{`\By\B`, nil, `xy`, []Group{}},
|
|
{`\By\B`, nil, `yz`, []Group{}},
|
|
{`\By\b`, nil, `xy`, []Group{{1, 2}}},
|
|
{`\by\B`, nil, `yz`, []Group{{0, 1}}},
|
|
{`\By\B`, nil, `xyz`, []Group{{1, 2}}},
|
|
{`ab|cd`, nil, `abc`, []Group{{0, 2}}},
|
|
{`ab|cd`, nil, `abcd`, []Group{{0, 2}, {2, 4}}},
|
|
{`$b`, nil, `b`, []Group{}},
|
|
{`a\(b`, nil, `a(b`, []Group{{0, 3}}},
|
|
{`a\(*b`, nil, `ab`, []Group{{0, 2}}},
|
|
{`a\(*b`, nil, `a((b`, []Group{{0, 4}}},
|
|
{`a\\b`, nil, `a\b`, []Group{{0, 3}}},
|
|
{`a+b+c`, nil, `aabbabc`, []Group{{4, 7}}},
|
|
{`)(`, nil, `-`, nil},
|
|
{`[^ab]*`, nil, `cde`, []Group{{0, 3}, {3, 3}}},
|
|
{`abc`, nil, ``, []Group{}},
|
|
{`a*`, nil, ``, []Group{{0, 0}}},
|
|
{`a|b|c|d|e`, nil, `e`, []Group{{0, 1}}},
|
|
{`abcd*efg`, nil, `abcdefg`, []Group{{0, 7}}},
|
|
{`ab*`, nil, `xabyabbbz`, []Group{{1, 3}, {4, 8}}},
|
|
{`ab*`, nil, `xayabbbz`, []Group{{1, 2}, {3, 7}}},
|
|
{`[abhgefdc]ij`, nil, `hij`, []Group{{0, 3}}},
|
|
{`a[bcd]*dcdcde`, nil, `adcdcde`, []Group{{0, 7}}},
|
|
{`a[bcd]+dcdcde`, nil, `adcdcde`, []Group{}},
|
|
{`[a-zA-Z_][a-zA-Z0-9_]*`, nil, `alpha`, []Group{{0, 5}}},
|
|
{`multiple words of text`, nil, `uh-uh`, []Group{}},
|
|
{`multiple words`, nil, `multiple words, yeah`, []Group{{0, 14}}},
|
|
{`[k]`, nil, `ab`, []Group{}},
|
|
{`a[-]?c`, nil, `ac`, []Group{{0, 2}}},
|
|
|
|
// Todo - add numeric range tests
|
|
}
|
|
|
|
var groupTests = []struct {
|
|
re string
|
|
flags []ReFlag
|
|
str string
|
|
result []Match
|
|
}{
|
|
{"(a)(b)", nil, "ab", []Match{[]Group{{0, 2}, {0, 1}, {1, 2}}}},
|
|
{"((a))(b)", nil, "ab", []Match{[]Group{{0, 2}, {0, 1}, {0, 1}, {1, 2}}}},
|
|
{"(0)", nil, "ab", []Match{[]Group{}}},
|
|
{"(a)b", nil, "ab", []Match{[]Group{{0, 2}, {0, 1}}}},
|
|
{"a(b)", nil, "ab", []Match{[]Group{{0, 2}, {1, 2}}}},
|
|
{"(a|b)", nil, "ab", []Match{[]Group{{0, 1}, {0, 1}}, []Group{{1, 2}, {1, 2}}}},
|
|
{"(a)|(b)", nil, "ab", []Match{[]Group{{0, 1}, {0, 1}, {-1, -1}}, []Group{{1, 2}, {-1, -1}, {1, 2}}}},
|
|
{"(a+)(a)", nil, "aaaa", []Match{[]Group{{0, 4}, {0, 3}, {3, 4}}}},
|
|
{"(a+)|(a)", nil, "aaaa", []Match{[]Group{{0, 4}, {0, 4}, {-1, -1}}}},
|
|
{"(a+)(aa)", nil, "aaaa", []Match{[]Group{{0, 4}, {0, 2}, {2, 4}}}},
|
|
{"(aaaa)|(aaaa)", nil, "aaaa", []Match{[]Group{{0, 4}, {0, 4}, {-1, -1}}}},
|
|
{"(aaa)|(aaaa)", nil, "aaaa", []Match{[]Group{{0, 4}, {-1, -1}, {0, 4}}}},
|
|
{"(aaa)|(aaaa)", nil, "aaaa", []Match{[]Group{{0, 4}, {-1, -1}, {0, 4}}}},
|
|
{"(aaaa)|(aaa)", nil, "aaaa", []Match{[]Group{{0, 4}, {0, 4}, {-1, -1}}}},
|
|
{"(a)|(aa)", nil, "aa", []Match{[]Group{{0, 2}, {-1, -1}, {0, 2}}}},
|
|
{"(a?)a?", nil, "b", []Match{[]Group{{0, 0}, {0, 0}}, []Group{{1, 1}, {1, 1}}}},
|
|
{"(a?)a?", nil, "ab", []Match{[]Group{{0, 1}, {0, 1}}, []Group{{1, 1}, {1, 1}}, []Group{{2, 2}, {2, 2}}}},
|
|
{"(a?)a?", nil, "aa", []Match{[]Group{{0, 2}, {0, 1}}, []Group{{2, 2}, {2, 2}}}},
|
|
{"a((b.d){3})", nil, "abfdbhdbid", []Match{[]Group{{0, 10}, {1, 10}, {7, 10}}}},
|
|
|
|
// Test cases from Python's RE test suite
|
|
{`(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)\071`, nil, `abcdefghijkl9`, []Match{[]Group{{0, 13}, {0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9, 10}, {10, 11}, {11, 12}}}},
|
|
{`()ef`, nil, `def`, []Match{[]Group{{1, 3}, {1, 1}}}},
|
|
{`(?:)ef`, nil, `def`, []Match{[]Group{{1, 3}}}},
|
|
{`(?:)`, nil, `def`, []Match{[]Group{{0, 0}}, []Group{{1, 1}}, []Group{{2, 2}}, []Group{{3, 3}}}},
|
|
{`((a))`, nil, `abc`, []Match{[]Group{{0, 1}, {0, 1}, {0, 1}}}},
|
|
{`(a)b(c)`, nil, `abc`, []Match{[]Group{{0, 3}, {0, 1}, {2, 3}}}},
|
|
{`(a+|b)*`, nil, `ab`, []Match{[]Group{{0, 2}, {1, 2}}, []Group{{2, 2}}}},
|
|
{`(a+|b)+`, nil, `ab`, []Match{[]Group{{0, 2}, {1, 2}}}},
|
|
{`(a+|b)?`, nil, `ab`, []Match{[]Group{{0, 1}, {0, 1}}, []Group{{1, 2}, {1, 2}}, []Group{{2, 2}}}},
|
|
{`(a|b|c|d|e)f`, nil, `ef`, []Match{[]Group{{0, 2}, {0, 1}}}},
|
|
{`(ab|cd)e`, nil, `abcde`, []Match{[]Group{{2, 5}, {2, 4}}}},
|
|
{`^(ab|cd)e`, nil, `abcde`, []Match{}},
|
|
{`(abc|)ef`, nil, `abcdef`, []Match{[]Group{{4, 6}, {4, 4}}}},
|
|
{`(a|b)c*d`, nil, `abcd`, []Match{[]Group{{1, 4}, {1, 2}}}},
|
|
{`(ab|ab*)bc`, nil, `abc`, []Match{[]Group{{0, 3}, {0, 1}}}},
|
|
{`a([bc]*)c*`, nil, `abc`, []Match{[]Group{{0, 3}, {1, 3}}}},
|
|
{`a([bc]*)(c*d)`, nil, `abcd`, []Match{[]Group{{0, 4}, {1, 3}, {3, 4}}}},
|
|
{`a([bc]+)(c*d)`, nil, `abcd`, []Match{[]Group{{0, 4}, {1, 3}, {3, 4}}}},
|
|
{`a([bc]*)(c+d)`, nil, `abcd`, []Match{[]Group{{0, 4}, {1, 2}, {2, 4}}}},
|
|
{`(ab|a)b*c`, nil, `abc`, []Match{[]Group{{0, 3}, {0, 2}}}},
|
|
{`((a)(b)c)(d)`, nil, `abcd`, []Match{[]Group{{0, 4}, {0, 3}, {0, 1}, {1, 2}, {3, 4}}}},
|
|
{`^a(bc+|b[eh])g|.h$`, nil, `abh`, []Match{[]Group{{1, 3}}}},
|
|
{`(bc+d$|ef*g.|h?i(j|k))`, nil, `effgz`, []Match{[]Group{{0, 5}, {0, 5}}}},
|
|
{`(bc+d$|ef*g.|h?i(j|k))`, nil, `ij`, []Match{[]Group{{0, 2}, {0, 2}, {1, 2}}}},
|
|
{`(bc+d$|ef*g.|h?i(j|k))`, nil, `effg`, []Match{}},
|
|
{`(bc+d$|ef*g.|h?i(j|k))`, nil, `bcdd`, []Match{}},
|
|
{`(bc+d$|ef*g.|h?i(j|k))`, nil, `reffgz`, []Match{[]Group{{1, 6}, {1, 6}}}},
|
|
{`(((((((((a)))))))))`, nil, `a`, []Match{[]Group{{0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}}}},
|
|
{`(.*)c(.*)`, nil, `abcde`, []Match{[]Group{{0, 5}, {0, 2}, {3, 5}}}},
|
|
{`\((.*), (.*)\)`, nil, `(a, b)`, []Match{[]Group{{0, 6}, {1, 2}, {4, 5}}}},
|
|
}
|
|
|
|
func TestFindAllMatches(t *testing.T) {
|
|
for _, test := range reTests {
|
|
t.Run(test.re+" "+test.str, func(t *testing.T) {
|
|
regComp, err := Compile(test.re, test.flags...)
|
|
if err != nil {
|
|
if test.result != nil {
|
|
panic(fmt.Errorf("Test Error: %v", err))
|
|
}
|
|
} else {
|
|
matchIndices := FindAllMatches(regComp, test.str)
|
|
zeroGroups := make([]Group, len(matchIndices))
|
|
for i, m := range matchIndices {
|
|
zeroGroups[i] = m[0]
|
|
}
|
|
if !slices.Equal(test.result, zeroGroups) {
|
|
t.Errorf("Wanted %v Got %v\n", test.result, zeroGroups)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFindString(t *testing.T) {
|
|
for _, test := range reTests {
|
|
t.Run(test.re+" "+test.str, func(t *testing.T) {
|
|
regComp, err := Compile(test.re, test.flags...)
|
|
if err != nil {
|
|
if test.result != nil {
|
|
panic(err)
|
|
}
|
|
} else {
|
|
foundString := FindString(regComp, test.str)
|
|
if len(test.result) == 0 {
|
|
if foundString != "" {
|
|
t.Errorf("Expected no match got %v\n", foundString)
|
|
}
|
|
} else {
|
|
expectedString := test.str[test.result[0].startIdx:test.result[0].endIdx]
|
|
if foundString != expectedString {
|
|
t.Errorf("Wanted %v Got %v\n", expectedString, foundString)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFindAllGroups(t *testing.T) {
|
|
for _, test := range groupTests {
|
|
t.Run(test.re+" "+test.str, func(t *testing.T) {
|
|
regComp, err := Compile(test.re, test.flags...)
|
|
if err != nil {
|
|
if test.result != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
matchIndices := FindAllMatches(regComp, test.str)
|
|
for i := range matchIndices {
|
|
for j := range matchIndices[i] {
|
|
if matchIndices[i][j].isValid() {
|
|
if test.result[i][j] != matchIndices[i][j] {
|
|
t.Errorf("Wanted %v Got %v\n", test.result, matchIndices)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|