diff options
| author | Brad Fitzpatrick <bradfitz@golang.org> | 2018-09-26 20:19:11 +0000 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2018-09-26 20:51:23 +0000 |
| commit | ebdc0b8d68e04ad383088c8b3ab963de4a9b5c5d (patch) | |
| tree | c6ccca03228b99d0042fd0d18ac91c9380355d40 /src/bytes/bytes.go | |
| parent | 5a8c11ce3e7a87485defafb78c7bcf14f9d7b5a2 (diff) | |
| download | go-ebdc0b8d68e04ad383088c8b3ab963de4a9b5c5d.tar.xz | |
bytes, strings: add ReplaceAll
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>
Diffstat (limited to 'src/bytes/bytes.go')
| -rw-r--r-- | src/bytes/bytes.go | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go index 876fa3c1ed..6492db088a 100644 --- a/src/bytes/bytes.go +++ b/src/bytes/bytes.go @@ -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 { |
