|
|
@ -1,6 +1,8 @@
|
|
|
|
package stringx
|
|
|
|
package stringx
|
|
|
|
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
import (
|
|
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
type (
|
|
|
|
// Replacer interface wraps the Replace method.
|
|
|
|
// Replacer interface wraps the Replace method.
|
|
|
@ -30,68 +32,27 @@ func NewReplacer(mapping map[string]string) Replacer {
|
|
|
|
|
|
|
|
|
|
|
|
// Replace replaces text with given substitutes.
|
|
|
|
// Replace replaces text with given substitutes.
|
|
|
|
func (r *replacer) Replace(text string) string {
|
|
|
|
func (r *replacer) Replace(text string) string {
|
|
|
|
var builder strings.Builder
|
|
|
|
var buf bytes.Buffer
|
|
|
|
var start int
|
|
|
|
target := []rune(text)
|
|
|
|
chars := []rune(text)
|
|
|
|
|
|
|
|
size := len(chars)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for start < size {
|
|
|
|
|
|
|
|
cur := r.node
|
|
|
|
cur := r.node
|
|
|
|
|
|
|
|
nextStart := 0
|
|
|
|
if start > 0 {
|
|
|
|
for len(target) != 0 {
|
|
|
|
builder.WriteString(string(chars[:start]))
|
|
|
|
used, jump, matched := cur.longestMatch(target, nextStart)
|
|
|
|
}
|
|
|
|
if matched {
|
|
|
|
|
|
|
|
replaced := r.mapping[string(target[:used])]
|
|
|
|
for i := start; i < size; i++ {
|
|
|
|
target = append([]rune(replaced), target[used:]...)
|
|
|
|
child, ok := cur.children[chars[i]]
|
|
|
|
cur = r.node
|
|
|
|
if ok {
|
|
|
|
} else {
|
|
|
|
cur = child
|
|
|
|
buf.WriteString(string(target[:used]))
|
|
|
|
} else if cur == r.node {
|
|
|
|
target = target[used:]
|
|
|
|
builder.WriteRune(chars[i])
|
|
|
|
if jump != nil {
|
|
|
|
// cur already points to root, set start only
|
|
|
|
cur = jump
|
|
|
|
start = i + 1
|
|
|
|
nextStart = jump.depth
|
|
|
|
continue
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
curDepth := cur.depth
|
|
|
|
|
|
|
|
cur = cur.fail
|
|
|
|
|
|
|
|
child, ok = cur.children[chars[i]]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
// write this path
|
|
|
|
|
|
|
|
builder.WriteString(string(chars[i-curDepth : i+1]))
|
|
|
|
|
|
|
|
// go to root
|
|
|
|
|
|
|
|
cur = r.node
|
|
|
|
cur = r.node
|
|
|
|
start = i + 1
|
|
|
|
nextStart = 0
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
failDepth := cur.depth
|
|
|
|
|
|
|
|
// write path before jump
|
|
|
|
|
|
|
|
builder.WriteString(string(chars[start : start+curDepth-failDepth]))
|
|
|
|
|
|
|
|
start += curDepth - failDepth
|
|
|
|
|
|
|
|
cur = child
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if cur.end {
|
|
|
|
|
|
|
|
val := string(chars[i+1-cur.depth : i+1])
|
|
|
|
|
|
|
|
builder.WriteString(r.mapping[val])
|
|
|
|
|
|
|
|
builder.WriteString(string(chars[i+1:]))
|
|
|
|
|
|
|
|
// only matching this path, all previous paths are done
|
|
|
|
|
|
|
|
if start >= i+1-cur.depth && i+1 >= size {
|
|
|
|
|
|
|
|
return builder.String()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chars = []rune(builder.String())
|
|
|
|
|
|
|
|
size = len(chars)
|
|
|
|
|
|
|
|
builder.Reset()
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !cur.end {
|
|
|
|
|
|
|
|
builder.WriteString(string(chars[start:]))
|
|
|
|
|
|
|
|
return builder.String()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
return string(chars)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|