From 7b45edb45016307151266731ccd158e14504598f Mon Sep 17 00:00:00 2001 From: Martin Möhrmann Date: Sun, 31 Oct 2021 10:04:03 +0100 Subject: bytes: add Clone function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new Clone function returns a copy of b[:len(b)] for the input byte slice b. The result may have additional unused capacity. Clone(nil) returns nil. Fixes #45038 Change-Id: I0469a202d77a7b491f1341c08915d07ddd1f0300 Reviewed-on: https://go-review.googlesource.com/c/go/+/359675 Run-TryBot: Martin Möhrmann TryBot-Result: Gopher Robot Reviewed-by: Joseph Tsai Reviewed-by: Martin Möhrmann Reviewed-by: Ian Lance Taylor --- src/bytes/bytes.go | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/bytes/bytes.go') diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go index 659a82bcc8..27834fc6db 100644 --- a/src/bytes/bytes.go +++ b/src/bytes/bytes.go @@ -1299,3 +1299,13 @@ func Cut(s, sep []byte) (before, after []byte, found bool) { } return s, nil, false } + +// Clone returns a copy of b[:len(b)]. +// The result may have additional unused capacity. +// Clone(nil) returns nil. +func Clone(b []byte) []byte { + if b == nil { + return nil + } + return append([]byte{}, b...) +} -- cgit v1.3-6-g1900