all_test.go 25 KB

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