go-zero/core/stringx/replacer.go

75 lines
1.1 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package stringx
import "strings"
type (
// Replacer interface wraps the Replace method.
2020-07-26 17:09:05 +08:00
Replacer interface {
Replace(text string) string
}
replacer struct {
node
mapping map[string]string
}
)
// NewReplacer returns a Replacer.
2020-07-26 17:09:05 +08:00
func NewReplacer(mapping map[string]string) Replacer {
2021-04-15 19:49:17 +08:00
rep := &replacer{
2020-07-26 17:09:05 +08:00
mapping: mapping,
}
for k := range mapping {
rep.add(k)
}
return rep
}
func (r *replacer) Replace(text string) string {
var builder strings.Builder
2021-04-15 19:49:17 +08:00
chars := []rune(text)
size := len(chars)
start := -1
2020-07-26 17:09:05 +08:00
for i := 0; i < size; i++ {
child, ok := r.children[chars[i]]
if !ok {
builder.WriteRune(chars[i])
continue
}
if start < 0 {
start = i
}
2021-04-15 19:49:17 +08:00
end := -1
2020-07-26 17:09:05 +08:00
if child.end {
end = i + 1
}
2021-04-15 19:49:17 +08:00
j := i + 1
2020-07-26 17:09:05 +08:00
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()
}