refactor: simplify stringx.Replacer, and avoid potential infinite loops (#2877)

* simplify replace

* backup

* refactor: simplify stringx.Replacer

* chore: add comments and const

* chore: add more tests

* chore: rename variable
This commit is contained in:
Kevin Wan
2023-02-14 10:18:02 +08:00
committed by GitHub
parent 9be17a2d28
commit cddf3875cf
4 changed files with 56 additions and 354 deletions

View File

@@ -219,3 +219,13 @@ func TestReplacer_ReplaceLongestMatch(t *testing.T) {
})
assert.Equal(t, "东京是东京", replacer.Replace("日本的首都是东京"))
}
func TestReplacer_ReplaceIndefinitely(t *testing.T) {
mapping := map[string]string{
"日本的首都": "东京",
"东京": "日本的首都",
}
assert.NotPanics(t, func() {
NewReplacer(mapping).Replace("日本的首都是东京")
})
}