-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "replace" function and documentation
This commit adds a `replace` function which takes three arguments - an original string, the substring to replace, and the string with which to replace it. This is of particular use when generating node names from IP addresses where the node name may not contain "." characters. The `strings.Replace` function in the Go standard library seems like it would be better named `strings.ReplaceN`, so we wrap it to replace all occurences of the string. If necessary, a gomplate function named `replaceN` could be introduced calling `strings.Replace` directly.
- Loading branch information
Showing
4 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
import "strings" | ||
|
||
// StringFunc - string manipulation function wrappers | ||
type StringFunc struct {} | ||
|
||
func (t StringFunc) Replace(s, old, new string) string { | ||
return strings.Replace(s, old, new, -1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReplace(t *testing.T) { | ||
sf := &StringFunc{} | ||
|
||
assert.Equal(t, "Replaced", | ||
sf.Replace("Orig", "Orig", "Replaced")) | ||
assert.Equal(t, "ReplacedReplaced", | ||
sf.Replace("OrigOrig", "Orig", "Replaced")) | ||
} |