diff options
| author | Andrew Gerrand <adg@golang.org> | 2013-11-01 11:18:49 +1100 |
|---|---|---|
| committer | Andrew Gerrand <adg@golang.org> | 2013-11-01 11:18:49 +1100 |
| commit | efb3af3d5c33dbdbad0fc52e95680843bb77ee72 (patch) | |
| tree | 7bea6509151a4cd32294f6c1deeaeb6c1821f478 /src/pkg/strings/replace.go | |
| parent | 7191e085990417cb17e51a61470debdea6297b5a (diff) | |
| download | go-efb3af3d5c33dbdbad0fc52e95680843bb77ee72.tar.xz | |
[release-branch.go1.2] strings: fix Replacer bug with prefix matches
««« CL 16880043 / 0eb6508d3e88
strings: fix Replacer bug with prefix matches
singleStringReplacer had a bug where if a string was replaced
at the beginning and no output had yet been produced into the
temp buffer before matching ended, an invalid nil check (used
as a proxy for having matched anything) meant it always
returned its input.
Fixes #6659
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/16880043
»»»
R=golang-dev
CC=golang-dev
https://golang.org/cl/20570044
Diffstat (limited to 'src/pkg/strings/replace.go')
| -rw-r--r-- | src/pkg/strings/replace.go | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/src/pkg/strings/replace.go b/src/pkg/strings/replace.go index f63b1792c5..54c9323e04 100644 --- a/src/pkg/strings/replace.go +++ b/src/pkg/strings/replace.go @@ -364,17 +364,18 @@ func makeSingleStringReplacer(pattern string, value string) *singleStringReplace func (r *singleStringReplacer) Replace(s string) string { var buf []byte - i := 0 + i, matched := 0, false for { match := r.finder.next(s[i:]) if match == -1 { break } + matched = true buf = append(buf, s[i:i+match]...) buf = append(buf, r.value...) i += match + len(r.finder.pattern) } - if buf == nil { + if !matched { return s } buf = append(buf, s[i:]...) |
