1
// Copyright 2011 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.
7
// Simplify returns a regexp equivalent to re but without counted repetitions
8
// and with various other simplifications, such as rewriting /(?:a+)+/ to /a+/.
9
// The resulting regexp will execute correctly but its string representation
10
// will not produce the same parse tree, because capturing parentheses
11
// may have been duplicated or removed. For example, the simplified form
12
// for /(x){1,2}/ is /(x)(x)?/ but both parentheses capture as $1.
13
// The returned regexp may share structure with or be the original.
14
func (re *Regexp) Simplify() *Regexp {
19
case OpCapture, OpConcat, OpAlternate:
20
// Simplify children, building new Regexp if children change.
22
for i, sub := range re.Sub {
23
nsub := sub.Simplify()
24
if nre == re && nsub != sub {
29
nre.Sub = append(nre.Sub0[:0], re.Sub[:i]...)
32
nre.Sub = append(nre.Sub, nsub)
37
case OpStar, OpPlus, OpQuest:
38
sub := re.Sub[0].Simplify()
39
return simplify1(re.Op, re.Flags, sub, re)
42
// Special special case: x{0} matches the empty string
43
// and doesn't even need to consider x.
44
if re.Min == 0 && re.Max == 0 {
45
return &Regexp{Op: OpEmptyMatch}
49
sub := re.Sub[0].Simplify()
51
// x{n,} means at least n matches of x.
53
// Special case: x{0,} is x*.
55
return simplify1(OpStar, re.Flags, sub, nil)
58
// Special case: x{1,} is x+.
60
return simplify1(OpPlus, re.Flags, sub, nil)
63
// General case: x{4,} is xxxx+.
64
nre := &Regexp{Op: OpConcat}
65
nre.Sub = nre.Sub0[:0]
66
for i := 0; i < re.Min-1; i++ {
67
nre.Sub = append(nre.Sub, sub)
69
nre.Sub = append(nre.Sub, simplify1(OpPlus, re.Flags, sub, nil))
73
// Special case x{0} handled above.
75
// Special case: x{1} is just x.
76
if re.Min == 1 && re.Max == 1 {
80
// General case: x{n,m} means n copies of x and m copies of x?
81
// The machine will do less work if we nest the final m copies,
82
// so that x{2,5} = xx(x(x(x)?)?)?
84
// Build leading prefix: xx.
87
prefix = &Regexp{Op: OpConcat}
88
prefix.Sub = prefix.Sub0[:0]
89
for i := 0; i < re.Min; i++ {
90
prefix.Sub = append(prefix.Sub, sub)
94
// Build and attach suffix: (x(x(x)?)?)?
96
suffix := simplify1(OpQuest, re.Flags, sub, nil)
97
for i := re.Min + 1; i < re.Max; i++ {
98
nre2 := &Regexp{Op: OpConcat}
99
nre2.Sub = append(nre2.Sub0[:0], sub, suffix)
100
suffix = simplify1(OpQuest, re.Flags, nre2, nil)
105
prefix.Sub = append(prefix.Sub, suffix)
111
// Some degenerate case like min > max or min < max < 0.
112
// Handle as impossible match.
113
return &Regexp{Op: OpNoMatch}
119
// simplify1 implements Simplify for the unary OpStar,
120
// OpPlus, and OpQuest operators. It returns the simple regexp
123
// Regexp{Op: op, Flags: flags, Sub: {sub}}
125
// under the assumption that sub is already simple, and
126
// without first allocating that structure. If the regexp
127
// to be returned turns out to be equivalent to re, simplify1
128
// returns re instead.
130
// simplify1 is factored out of Simplify because the implementation
131
// for other operators generates these unary expressions.
132
// Letting them call simplify1 makes sure the expressions they
133
// generate are simple.
134
func simplify1(op Op, flags Flags, sub, re *Regexp) *Regexp {
135
// Special case: repeat the empty string as much as
136
// you want, but it's still the empty string.
137
if sub.Op == OpEmptyMatch {
140
// The operators are idempotent if the flags match.
141
if op == sub.Op && flags&NonGreedy == sub.Flags&NonGreedy {
144
if re != nil && re.Op == op && re.Flags&NonGreedy == flags&NonGreedy && sub == re.Sub[0] {
148
re = &Regexp{Op: op, Flags: flags}
149
re.Sub = append(re.Sub0[:0], sub)