if allNewBytes {
bb := &byteReplacer{}
+ for i := range bb.new {
+ bb.new[i] = byte(i)
+ }
for i := 0; i < len(oldnew); i += 2 {
o, n := oldnew[i][0], oldnew[i+1][0]
if bb.old.isSet(o) {
// old has a bit set for each old byte that should be replaced.
old byteBitmap
- // replacement byte, indexed by old byte. only valid if
- // corresponding old bit is set.
+ // replacement byte, indexed by old byte. old byte and new
+ // byte are the same if corresponding old bit is not set.
new [256]byte
}
ncopy := copy(buf, s[:])
s = s[ncopy:]
for i, b := range buf[:ncopy] {
- if r.old.isSet(b) {
- buf[i] = r.new[b]
- }
+ buf[i] = r.new[b]
}
wn, err := w.Write(buf[:ncopy])
n += wn
}
}
-func BenchmarkWriteString(b *testing.B) {
+func BenchmarkByteStringReplacerWriteString(b *testing.B) {
str := Repeat("I <3 to escape HTML & other text too.", 100)
buf := new(bytes.Buffer)
for i := 0; i < b.N; i++ {
}
}
+func BenchmarkByteReplacerWriteString(b *testing.B) {
+ str := Repeat("abcdefghijklmnopqrstuvwxyz", 100)
+ buf := new(bytes.Buffer)
+ for i := 0; i < b.N; i++ {
+ capitalLetters.WriteString(buf, str)
+ buf.Reset()
+ }
+}
+
// BenchmarkByteByteReplaces compares byteByteImpl against multiple Replaces.
func BenchmarkByteByteReplaces(b *testing.B) {
str := Repeat("a", 100) + Repeat("b", 100)