Source file
src/regexp/syntax/simplify_test.go
1
2
3
4
5 package syntax
6
7 import "testing"
8
9 var simplifyTests = []struct {
10 Regexp string
11 Simple string
12 }{
13
14 {`a`, `a`},
15 {`ab`, `ab`},
16 {`a|b`, `[ab]`},
17 {`ab|cd`, `ab|cd`},
18 {`(ab)*`, `(ab)*`},
19 {`(ab)+`, `(ab)+`},
20 {`(ab)?`, `(ab)?`},
21 {`.`, `(?s:.)`},
22 {`^`, `(?m:^)`},
23 {`$`, `(?m:$)`},
24 {`[ac]`, `[ac]`},
25 {`[^ac]`, `[^ac]`},
26
27
28 {`[[:alnum:]]`, `[0-9A-Za-z]`},
29 {`[[:alpha:]]`, `[A-Za-z]`},
30 {`[[:blank:]]`, `[\t ]`},
31 {`[[:cntrl:]]`, `[\x00-\x1f\x7f]`},
32 {`[[:digit:]]`, `[0-9]`},
33 {`[[:graph:]]`, `[!-~]`},
34 {`[[:lower:]]`, `[a-z]`},
35 {`[[:print:]]`, `[ -~]`},
36 {`[[:punct:]]`, "[!-/:-@\\[-`\\{-~]"},
37 {`[[:space:]]`, `[\t-\r ]`},
38 {`[[:upper:]]`, `[A-Z]`},
39 {`[[:xdigit:]]`, `[0-9A-Fa-f]`},
40
41
42 {`\d`, `[0-9]`},
43 {`\s`, `[\t\n\f\r ]`},
44 {`\w`, `[0-9A-Z_a-z]`},
45 {`\D`, `[^0-9]`},
46 {`\S`, `[^\t\n\f\r ]`},
47 {`\W`, `[^0-9A-Z_a-z]`},
48 {`[\d]`, `[0-9]`},
49 {`[\s]`, `[\t\n\f\r ]`},
50 {`[\w]`, `[0-9A-Z_a-z]`},
51 {`[\D]`, `[^0-9]`},
52 {`[\S]`, `[^\t\n\f\r ]`},
53 {`[\W]`, `[^0-9A-Z_a-z]`},
54
55
56 {`a{1}`, `a`},
57 {`a{2}`, `aa`},
58 {`a{5}`, `aaaaa`},
59 {`a{0,1}`, `a?`},
60
61
62
63 {`(a){0,2}`, `(?:(a)(a)?)?`},
64 {`(a){0,4}`, `(?:(a)(?:(a)(?:(a)(a)?)?)?)?`},
65 {`(a){2,6}`, `(a)(a)(?:(a)(?:(a)(?:(a)(a)?)?)?)?`},
66 {`a{0,2}`, `(?:aa?)?`},
67 {`a{0,4}`, `(?:a(?:a(?:aa?)?)?)?`},
68 {`a{2,6}`, `aa(?:a(?:a(?:aa?)?)?)?`},
69 {`a{0,}`, `a*`},
70 {`a{1,}`, `a+`},
71 {`a{2,}`, `aa+`},
72 {`a{5,}`, `aaaaa+`},
73
74
75 {`(?:a{1,}){1,}`, `a+`},
76 {`(a{1,}b{1,})`, `(a+b+)`},
77 {`a{1,}|b{1,}`, `a+|b+`},
78 {`(?:a{1,})*`, `(?:a+)*`},
79 {`(?:a{1,})+`, `a+`},
80 {`(?:a{1,})?`, `(?:a+)?`},
81 {``, `(?:)`},
82 {`a{0}`, `(?:)`},
83
84
85 {`[ab]`, `[ab]`},
86 {`[abc]`, `[a-c]`},
87 {`[a-za-za-z]`, `[a-z]`},
88 {`[A-Za-zA-Za-z]`, `[A-Za-z]`},
89 {`[ABCDEFGH]`, `[A-H]`},
90 {`[AB-CD-EF-GH]`, `[A-H]`},
91 {`[W-ZP-XE-R]`, `[E-Z]`},
92 {`[a-ee-gg-m]`, `[a-m]`},
93 {`[a-ea-ha-m]`, `[a-m]`},
94 {`[a-ma-ha-e]`, `[a-m]`},
95 {`[a-zA-Z0-9 -~]`, `[ -~]`},
96
97
98 {`[^[:cntrl:][:^cntrl:]]`, `[^\x00-\x{10FFFF}]`},
99
100
101 {`[[:cntrl:][:^cntrl:]]`, `(?s:.)`},
102
103
104 {`(?i)A`, `(?i:A)`},
105 {`(?i)a`, `(?i:A)`},
106 {`(?i)[A]`, `(?i:A)`},
107 {`(?i)[a]`, `(?i:A)`},
108 {`(?i)K`, `(?i:K)`},
109 {`(?i)k`, `(?i:K)`},
110 {`(?i)\x{212a}`, "(?i:K)"},
111 {`(?i)[K]`, "[Kk\u212A]"},
112 {`(?i)[k]`, "[Kk\u212A]"},
113 {`(?i)[\x{212a}]`, "[Kk\u212A]"},
114 {`(?i)[a-z]`, "[A-Za-z\u017F\u212A]"},
115 {`(?i)[\x00-\x{FFFD}]`, "[\\x00-\uFFFD]"},
116 {`(?i)[\x00-\x{10FFFF}]`, `(?s:.)`},
117
118
119
120
121
122
123
124 {`(a|b|c|)`, `([a-c]|(?:))`},
125 {`(a|b|)`, `([ab]|(?:))`},
126 {`(|)`, `()`},
127 {`a()`, `a()`},
128 {`(()|())`, `(()|())`},
129 {`(a|)`, `(a|(?:))`},
130 {`ab()cd()`, `ab()cd()`},
131 {`()`, `()`},
132 {`()*`, `()*`},
133 {`()+`, `()+`},
134 {`()?`, `()?`},
135 {`(){0}`, `(?:)`},
136 {`(){1}`, `()`},
137 {`(){1,}`, `()+`},
138 {`(){0,2}`, `(?:()()?)?`},
139 }
140
141 func TestSimplify(t *testing.T) {
142 for _, tt := range simplifyTests {
143 re, err := Parse(tt.Regexp, MatchNL|Perl&^OneLine)
144 if err != nil {
145 t.Errorf("Parse(%#q) = error %v", tt.Regexp, err)
146 continue
147 }
148 s := re.Simplify().String()
149 if s != tt.Simple {
150 t.Errorf("Simplify(%#q) = %#q, want %#q", tt.Regexp, s, tt.Simple)
151 }
152 }
153 }
154
View as plain text