]> Cypherpunks repositories - gostls13.git/commitdiff
bytes, strings: add ReplaceAll
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 26 Sep 2018 20:19:11 +0000 (20:19 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 26 Sep 2018 20:51:23 +0000 (20:51 +0000)
Credit to Harald Nordgren for the proposal in
https://golang.org/cl/137456 and #27864.

Fixes #27864

Change-Id: I80546683b0623124fe4627a71af88add2f6c1c27
Reviewed-on: https://go-review.googlesource.com/137855
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/bytes/bytes.go
src/bytes/bytes_test.go
src/strings/strings.go
src/strings/strings_test.go

index 876fa3c1edda659d6960acd942b05f0f81db4365..6492db088a056c439d4f7d8ff67525a8069edf14 100644 (file)
@@ -774,6 +774,15 @@ func Replace(s, old, new []byte, n int) []byte {
        return t[0:w]
 }
 
+// ReplaceAll returns a copy of the slice s with all
+// non-overlapping instances of old replaced by new.
+// If old is empty, it matches at the beginning of the slice
+// and after each UTF-8 sequence, yielding up to k+1 replacements
+// for a k-rune slice.
+func ReplaceAll(s, old, new []byte) []byte {
+       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 []byte) bool {
index 55a22bae22a1d4eee458bef0b0dfb390e4dcbc5b..f4c0ffd2a91f7d709e2417a45e80e08a70f5c36e 100644 (file)
@@ -1362,6 +1362,12 @@ func TestReplace(t *testing.T) {
                if cap(in) == cap(out) && &in[:1][0] == &out[:1][0] {
                        t.Errorf("Replace(%q, %q, %q, %d) didn't copy", tt.in, tt.old, tt.new, tt.n)
                }
+               if tt.n == -1 {
+                       out := ReplaceAll(in, []byte(tt.old), []byte(tt.new))
+                       if s := string(out); s != tt.out {
+                               t.Errorf("ReplaceAll(%q, %q, %q) = %q, want %q", tt.in, tt.old, tt.new, s, tt.out)
+                       }
+               }
        }
 }
 
index b033c38e91e3558d61ccc482f096636cc6ff80c0..00200e4e24df07ea23b936b6561b3a94e0737831 100644 (file)
@@ -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 {
index 20bc484f3950bcb50c137625f2bff0486eb5f712..bb6a5b931b85d12f1fb2da136c38c0e8618406cc 100644 (file)
@@ -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)
+                       }
+               }
        }
 }