mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-14 01:32:27 +08:00
feat: add ReplaceWithMap
This commit is contained in:
@@ -446,10 +446,10 @@ func IndexOffset(str string, substr string, idxFrom int) int {
|
|||||||
return strings.Index(str[idxFrom:], substr) + idxFrom
|
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.
|
// which is replaced by a map in unordered way, case-sensitively.
|
||||||
// Play: todo
|
// 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 {
|
for k, v := range replaces {
|
||||||
str = strings.ReplaceAll(str, k, v)
|
str = strings.ReplaceAll(str, k, v)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -526,14 +526,14 @@ func ExampleIndexOffset() {
|
|||||||
// -1
|
// -1
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleReplaceByMap() {
|
func ExampleReplaceWithMap() {
|
||||||
str := "ac ab ab ac"
|
str := "ac ab ab ac"
|
||||||
replaces := map[string]string{
|
replaces := map[string]string{
|
||||||
"a": "1",
|
"a": "1",
|
||||||
"b": "2",
|
"b": "2",
|
||||||
}
|
}
|
||||||
|
|
||||||
result := ReplaceByMap(str, replaces)
|
result := ReplaceWithMap(str, replaces)
|
||||||
|
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
// Output:
|
// Output:
|
||||||
|
|||||||
@@ -398,8 +398,8 @@ func TestIndexOffset(t *testing.T) {
|
|||||||
assert.Equal(IndexOffset(str, "f", -1), -1)
|
assert.Equal(IndexOffset(str, "f", -1), -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReplaceByMap(t *testing.T) {
|
func TestReplaceWithMap(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestReplaceByMap")
|
assert := internal.NewAssert(t, "TestReplaceWithMap")
|
||||||
|
|
||||||
str := "ac ab ab ac"
|
str := "ac ab ab ac"
|
||||||
replaces := map[string]string{
|
replaces := map[string]string{
|
||||||
@@ -408,5 +408,5 @@ func TestReplaceByMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(str, "ac ab ab ac")
|
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")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user