aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/strings/replace.go
diff options
context:
space:
mode:
authorNigel Tao <nigeltao@golang.org>2012-09-12 10:40:39 +1000
committerNigel Tao <nigeltao@golang.org>2012-09-12 10:40:39 +1000
commit6910356ea899e0c7b19da5f59c6058a737ed5e93 (patch)
treebb8d0678743726ccb855975e55bf0e547ddf709d /src/pkg/strings/replace.go
parente39072d65fd3aeb1db0744226c27abc1fa02e047 (diff)
downloadgo-6910356ea899e0c7b19da5f59c6058a737ed5e93.tar.xz
strings: fix NewReplacer(old0, new0, old1, new1, ...) to be consistent
when oldi == oldj. Benchmark numbers show no substantial change. R=eric.d.eisner, rogpeppe CC=golang-dev https://golang.org/cl/6496104
Diffstat (limited to 'src/pkg/strings/replace.go')
-rw-r--r--src/pkg/strings/replace.go70
1 files changed, 37 insertions, 33 deletions
diff --git a/src/pkg/strings/replace.go b/src/pkg/strings/replace.go
index f53a96ee0f..3a1322bade 100644
--- a/src/pkg/strings/replace.go
+++ b/src/pkg/strings/replace.go
@@ -33,47 +33,41 @@ func NewReplacer(oldnew ...string) *Replacer {
panic("strings.NewReplacer: odd argument count")
}
- // Possible implementations.
- var (
- bb byteReplacer
- bs byteStringReplacer
- gen genericReplacer
- )
-
- allOldBytes, allNewBytes := true, true
- for len(oldnew) > 0 {
- old, new := oldnew[0], oldnew[1]
- oldnew = oldnew[2:]
- if len(old) != 1 {
- allOldBytes = false
+ allNewBytes := true
+ for i := 0; i < len(oldnew); i += 2 {
+ if len(oldnew[i]) != 1 {
+ return &Replacer{r: makeGenericReplacer(oldnew)}
}
- if len(new) != 1 {
+ if len(oldnew[i+1]) != 1 {
allNewBytes = false
}
+ }
- // generic
- gen.p = append(gen.p, pair{old, new})
-
- // byte -> string
- if allOldBytes {
- bs.old.set(old[0])
- bs.new[old[0]] = []byte(new)
- }
-
- // byte -> byte
- if allOldBytes && allNewBytes {
- bb.old.set(old[0])
- bb.new[old[0]] = new[0]
+ if allNewBytes {
+ bb := &byteReplacer{}
+ for i := 0; i < len(oldnew); i += 2 {
+ o, n := oldnew[i][0], oldnew[i+1][0]
+ if bb.old[o>>5]&uint32(1<<(o&31)) != 0 {
+ // Later old->new maps do not override previous ones with the same old string.
+ continue
+ }
+ bb.old.set(o)
+ bb.new[o] = n
}
+ return &Replacer{r: bb}
}
- if allOldBytes && allNewBytes {
- return &Replacer{r: &bb}
- }
- if allOldBytes {
- return &Replacer{r: &bs}
+ bs := &byteStringReplacer{}
+ for i := 0; i < len(oldnew); i += 2 {
+ o, new := oldnew[i][0], oldnew[i+1]
+ if bs.old[o>>5]&uint32(1<<(o&31)) != 0 {
+ // Later old->new maps do not override previous ones with the same old string.
+ continue
+ }
+ bs.old.set(o)
+ bs.new[o] = []byte(new)
}
- return &Replacer{r: &gen}
+ return &Replacer{r: bs}
}
// Replace returns a copy of s with all replacements performed.
@@ -94,6 +88,16 @@ type genericReplacer struct {
type pair struct{ old, new string }
+func makeGenericReplacer(oldnew []string) *genericReplacer {
+ gen := &genericReplacer{
+ p: make([]pair, len(oldnew)/2),
+ }
+ for i := 0; i < len(oldnew); i += 2 {
+ gen.p[i/2] = pair{oldnew[i], oldnew[i+1]}
+ }
+ return gen
+}
+
type appendSliceWriter struct {
b []byte
}