fix: replace shoud replace the longest match

This commit is contained in:
hudahai
2023-02-04 15:31:16 +08:00
committed by kevin
parent aaa974e1ad
commit 687a1d15da
3 changed files with 73 additions and 61 deletions

View File

@@ -51,6 +51,15 @@ func TestReplacer_ReplaceMultiMatches(t *testing.T) {
assert.Equal(t, "零一23四五一23四五", NewReplacer(mapping).Replace("零一二三四五一二三四五"))
}
func TestReplacer_ReplaceLongestMatching(t *testing.T) {
keywords := map[string]string{
"日本": "japan",
"日本的首都": "东京",
}
replacer := NewReplacer(keywords)
assert.Equal(t, "东京在japan", replacer.Replace("日本的首都在日本"))
}
func TestReplacer_ReplaceJumpToFail(t *testing.T) {
mapping := map[string]string{
"bcdf": "1235",