m[b>>5] |= uint32(1 << (b & 31))
}
+func (m *byteBitmap) isSet(b byte) bool {
+ return m[b>>5]&uint32(1<<(b&31)) != 0
+}
+
// NewReplacer returns a new Replacer from a list of old, new string pairs.
// Replacements are performed in order, without overlapping matches.
func NewReplacer(oldnew ...string) *Replacer {
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 {
+ if bb.old.isSet(o) {
// Later old->new maps do not override previous ones with the same old string.
continue
}
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 {
+ if bs.old.isSet(o) {
// Later old->new maps do not override previous ones with the same old string.
continue
}
var buf []byte // lazily allocated
for i := 0; i < len(s); i++ {
b := s[i]
- if r.old[b>>5]&uint32(1<<(b&31)) != 0 {
+ if r.old.isSet(b) {
if buf == nil {
buf = []byte(s)
}
ncopy := copy(buf, s[:])
s = s[ncopy:]
for i, b := range buf[:ncopy] {
- if r.old[b>>5]&uint32(1<<(b&31)) != 0 {
+ if r.old.isSet(b) {
buf[i] = r.new[b]
}
}
anyChanges := false
for i := 0; i < len(s); i++ {
b := s[i]
- if r.old[b>>5]&uint32(1<<(b&31)) != 0 {
+ if r.old.isSet(b) {
anyChanges = true
newSize += len(r.new[b])
} else {
bi := buf
for i := 0; i < len(s); i++ {
b := s[i]
- if r.old[b>>5]&uint32(1<<(b&31)) != 0 {
+ if r.old.isSet(b) {
n := copy(bi, r.new[b])
bi = bi[n:]
} else {
last := 0
for i := 0; i < len(s); i++ {
b := s[i]
- if r.old[b>>5]&uint32(1<<(b&31)) == 0 {
+ if !r.old.isSet(b) {
continue
}
if last != i {