aboutsummaryrefslogtreecommitdiff
path: root/src/strings
diff options
context:
space:
mode:
Diffstat (limited to 'src/strings')
-rw-r--r--src/strings/strings.go9
-rw-r--r--src/strings/strings_test.go6
2 files changed, 15 insertions, 0 deletions
diff --git a/src/strings/strings.go b/src/strings/strings.go
index b033c38e91..00200e4e24 100644
--- a/src/strings/strings.go
+++ b/src/strings/strings.go
@@ -874,6 +874,15 @@ func Replace(s, old, new string, n int) string {
return string(t[0:w])
}
+// ReplaceAll returns a copy of the string s with all
+// non-overlapping instances of old replaced by new.
+// If old is empty, it matches at the beginning of the string
+// and after each UTF-8 sequence, yielding up to k+1 replacements
+// for a k-rune string.
+func ReplaceAll(s, old, new string) string {
+ return Replace(s, old, new, -1)
+}
+
// EqualFold reports whether s and t, interpreted as UTF-8 strings,
// are equal under Unicode case-folding.
func EqualFold(s, t string) bool {
diff --git a/src/strings/strings_test.go b/src/strings/strings_test.go
index 20bc484f39..bb6a5b931b 100644
--- a/src/strings/strings_test.go
+++ b/src/strings/strings_test.go
@@ -1243,6 +1243,12 @@ func TestReplace(t *testing.T) {
if s := Replace(tt.in, tt.old, tt.new, tt.n); s != tt.out {
t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
}
+ if tt.n == -1 {
+ s := ReplaceAll(tt.in, tt.old, tt.new)
+ if s != tt.out {
+ t.Errorf("ReplaceAll(%q, %q, %q) = %q, want %q", tt.in, tt.old, tt.new, s, tt.out)
+ }
+ }
}
}