diff options
| author | Gustavo Niemeyer <gustavo@niemeyer.net> | 2012-07-20 16:04:22 -0300 |
|---|---|---|
| committer | Gustavo Niemeyer <gustavo@niemeyer.net> | 2012-07-20 16:04:22 -0300 |
| commit | c0efcac6a97588f7013b7ec09dd56cb780bdce64 (patch) | |
| tree | 3a63a95aa0b9dc88d89c46fbc9e6f70e5e843d95 /src/pkg/bytes | |
| parent | 7bf8355dc7d1a185cb96f66011a3217b99e85f69 (diff) | |
| download | go-c0efcac6a97588f7013b7ec09dd56cb780bdce64.tar.xz | |
bytes: make Join return a new buffer on len(a) == 1
Fixes #3844.
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/6432054
Diffstat (limited to 'src/pkg/bytes')
| -rw-r--r-- | src/pkg/bytes/bytes.go | 11 | ||||
| -rw-r--r-- | src/pkg/bytes/bytes_test.go | 6 |
2 files changed, 11 insertions, 6 deletions
diff --git a/src/pkg/bytes/bytes.go b/src/pkg/bytes/bytes.go index 09b3c1a270..c3980bb2ab 100644 --- a/src/pkg/bytes/bytes.go +++ b/src/pkg/bytes/bytes.go @@ -333,14 +333,15 @@ func FieldsFunc(s []byte, f func(rune) bool) [][]byte { return a[0:na] } -// Join concatenates the elements of a to create a single byte array. The separator +// Join concatenates the elements of a to create a new byte array. The separator // sep is placed between elements in the resulting array. func Join(a [][]byte, sep []byte) []byte { if len(a) == 0 { return []byte{} } if len(a) == 1 { - return a[0] + // Just return a copy. + return append([]byte(nil), a[0]...) } n := len(sep) * (len(a) - 1) for i := 0; i < len(a); i++ { @@ -619,10 +620,8 @@ func Replace(s, old, new []byte, n int) []byte { m = Count(s, old) } if m == 0 { - // Nothing to do. Just copy. - t := make([]byte, len(s)) - copy(t, s) - return t + // Just return a copy. + return append([]byte(nil), s...) } if n < 0 || m < n { n = m diff --git a/src/pkg/bytes/bytes_test.go b/src/pkg/bytes/bytes_test.go index 000f235176..0e2ef504cf 100644 --- a/src/pkg/bytes/bytes_test.go +++ b/src/pkg/bytes/bytes_test.go @@ -490,6 +490,12 @@ func TestSplit(t *testing.T) { t.Errorf("Split disagrees withSplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a) } } + if len(a) > 0 { + in, out := a[0], s + if cap(in) == cap(out) && &in[:1][0] == &out[:1][0] { + t.Errorf("Join(%#v, %q) didn't copy", a, tt.sep) + } + } } } |
