You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/stringx/replacer.go

75 lines
1.1 KiB
Go

4 years ago
package stringx
import "strings"
type (
// Replacer interface wraps the Replace method.
4 years ago
Replacer interface {
Replace(text string) string
}
replacer struct {
node
mapping map[string]string
}
)
// NewReplacer returns a Replacer.
4 years ago
func NewReplacer(mapping map[string]string) Replacer {
rep := &replacer{
4 years ago
mapping: mapping,
}
for k := range mapping {
rep.add(k)
}
return rep
}
func (r *replacer) Replace(text string) string {
var builder strings.Builder
chars := []rune(text)
size := len(chars)
start := -1
4 years ago
for i := 0; i < size; i++ {
child, ok := r.children[chars[i]]
if !ok {
builder.WriteRune(chars[i])
continue
}
if start < 0 {
start = i
}
end := -1
4 years ago
if child.end {
end = i + 1
}
j := i + 1
4 years ago
for ; j < size; j++ {
grandchild, ok := child.children[chars[j]]
if !ok {
break
}
child = grandchild
if child.end {
end = j + 1
i = j
}
}
if end > 0 {
i = j - 1
builder.WriteString(r.mapping[string(chars[start:end])])
} else {
builder.WriteRune(chars[i])
}
start = -1
}
return builder.String()
}