From c3f1bc39d706d39bb3a33e7edcab5a813724603e Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 10 May 2023 10:29:26 +0800 Subject: [PATCH] feat: add ReplaceWithMap --- strutil/string.go | 4 ++-- strutil/string_example_test.go | 4 ++-- strutil/string_test.go | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/strutil/string.go b/strutil/string.go index 628ad11..95e983a 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -446,10 +446,10 @@ func IndexOffset(str string, substr string, idxFrom int) int { return strings.Index(str[idxFrom:], substr) + idxFrom } -// ReplaceByMap returns a copy of `origin`, +// ReplaceWithMap returns a copy of `str`, // which is replaced by a map in unordered way, case-sensitively. // Play: todo -func ReplaceByMap(str string, replaces map[string]string) string { +func ReplaceWithMap(str string, replaces map[string]string) string { for k, v := range replaces { str = strings.ReplaceAll(str, k, v) } diff --git a/strutil/string_example_test.go b/strutil/string_example_test.go index dd43755..7cda3e8 100644 --- a/strutil/string_example_test.go +++ b/strutil/string_example_test.go @@ -526,14 +526,14 @@ func ExampleIndexOffset() { // -1 } -func ExampleReplaceByMap() { +func ExampleReplaceWithMap() { str := "ac ab ab ac" replaces := map[string]string{ "a": "1", "b": "2", } - result := ReplaceByMap(str, replaces) + result := ReplaceWithMap(str, replaces) fmt.Println(result) // Output: diff --git a/strutil/string_test.go b/strutil/string_test.go index f80cf61..78a174e 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -398,8 +398,8 @@ func TestIndexOffset(t *testing.T) { assert.Equal(IndexOffset(str, "f", -1), -1) } -func TestReplaceByMap(t *testing.T) { - assert := internal.NewAssert(t, "TestReplaceByMap") +func TestReplaceWithMap(t *testing.T) { + assert := internal.NewAssert(t, "TestReplaceWithMap") str := "ac ab ab ac" replaces := map[string]string{ @@ -408,5 +408,5 @@ func TestReplaceByMap(t *testing.T) { } assert.Equal(str, "ac ab ab ac") - assert.Equal(ReplaceByMap(str, replaces), "1c 12 12 1c") + assert.Equal(ReplaceWithMap(str, replaces), "1c 12 12 1c") }