aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2011-01-04 12:43:52 -0800
committerRob Pike <r@golang.org>2011-01-04 12:43:52 -0800
commit6a5a527173b0b8c10a3ddbfe75ddbef1f9ae342b (patch)
tree7d94d99773ef2ee7a232df802998224a9e6affe2
parent236f9638b41e94d8e21044df54f2281c52904d7f (diff)
downloadgo-6a5a527173b0b8c10a3ddbfe75ddbef1f9ae342b.tar.gz
go-6a5a527173b0b8c10a3ddbfe75ddbef1f9ae342b.zip
regexp: implement early out for failed anchored search.
R=rsc CC=golang-dev https://golang.org/cl/3813045
-rw-r--r--src/pkg/regexp/regexp.go11
1 files changed, 5 insertions, 6 deletions
diff --git a/src/pkg/regexp/regexp.go b/src/pkg/regexp/regexp.go
index be3ce2028e..2e03b798ab 100644
--- a/src/pkg/regexp/regexp.go
+++ b/src/pkg/regexp/regexp.go
@@ -783,13 +783,16 @@ func (re *Regexp) doExecute(str string, bytestr []byte, pos int) []int {
pos += advance
}
arena := &matchArena{nil, 2 * (re.nbra + 1)}
- for pos <= end {
- if !found {
+ for startPos := pos; pos <= end; {
+ if !found && (pos == startPos || !anchored) {
// prime the pump if we haven't seen a match yet
match := arena.noMatch()
match.m[0] = pos
s[out] = arena.addState(s[out], re.start.next, false, match, pos, end)
arena.free(match) // if addState saved it, ref was incremented
+ } else if len(s[out]) == 0 {
+ // machine has completed
+ break
}
in, out = out, in // old out state is new in state
// clear out old state
@@ -798,10 +801,6 @@ func (re *Regexp) doExecute(str string, bytestr []byte, pos int) []int {
arena.free(state.match)
}
s[out] = old[0:0] // truncate state vector
- if found && len(s[in]) == 0 {
- // machine has completed
- break
- }
charwidth := 1
c := endOfFile
if pos < end {