jiahua wang has uploaded this change for review.
bytes: add Clone function
Fixes: #45038
Change-Id: Ia7408cba419022da69cc1953c29274cccdd13213
---
M src/bytes/bytes.go
M src/bytes/bytes_test.go
2 files changed, 67 insertions(+), 0 deletions(-)
diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go
index 41323ad..f01a0f3 100644
--- a/src/bytes/bytes.go
+++ b/src/bytes/bytes.go
@@ -27,6 +27,16 @@
return bytealg.Compare(a, b)
}
+// Clone returns a fresh copy of a.
+func Clone(a []byte) []byte {
+ if a == nil || len(a) == 0 {
+ return nil
+ }
+ b := make([]byte, len(a))
+ copy(b, a)
+ return b
+}
+
// explode splits s into a slice of UTF-8 sequences, one per Unicode code point (still slices of bytes),
// up to a maximum of n byte slices. Invalid UTF-8 sequences are chopped into individual bytes.
func explode(s []byte, n int) [][]byte {
diff --git a/src/bytes/bytes_test.go b/src/bytes/bytes_test.go
index 3bece6a..15f488f 100644
--- a/src/bytes/bytes_test.go
+++ b/src/bytes/bytes_test.go
@@ -14,6 +14,7 @@
"testing"
"unicode"
"unicode/utf8"
+ "unsafe"
)
func eq(a, b []string) bool {
@@ -117,6 +118,36 @@
}
}
+var emptyByte []byte = nil
+
+func TestClone(t *testing.T) {
+ var cloneTests = [][]byte{
+ nil,
+ []byte{},
+ []byte("short"),
+ []byte(strings.Repeat("a", 42)),
+ []byte(strings.Repeat("a", 42))[:0],
+ []byte(strings.Repeat("a", 42))[:0:0],
+ }
+ for _, input := range cloneTests {
+ clone := Clone(input)
+ if !Equal(input, clone) {
+ t.Errorf("Clone(%q) = %q; want %q", input, clone, input)
+ }
+
+ inputHeader := (*reflect.SliceHeader)(unsafe.Pointer(&input))
+ cloneHeader := (*reflect.SliceHeader)(unsafe.Pointer(&clone))
+ if input != nil && cloneHeader.Data == inputHeader.Data {
+ t.Errorf("Clone(%q) return value should not reference inputs backing memory.", input)
+ }
+
+ emptyHeader := (*reflect.SliceHeader)(unsafe.Pointer(&emptyByte))
+ if input == nil && cloneHeader.Data != emptyHeader.Data {
+ t.Errorf("Clone(%#v) return value should be equal to empty string.", inputHeader)
+ }
+ }
+}
+
var indexTests = []BinOpTest{
{"", "", 0},
{"", "a", -1},
@@ -2009,3 +2040,19 @@
})
}
}
+
+func BenchmarkClone(b *testing.B) {
+ var a = []byte(strings.Repeat("a", 42))
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ Clone(a)
+ }
+}
+
+// func BenchmarkClone(b *testing.B) {
+// var str = strings.Repeat("a", 42)
+// b.ReportAllocs()
+// for i := 0; i < b.N; i++ {
+// stringSink = strings.Clone(str)
+// }
+// }
To view, visit change 392194. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: jiahua wang, Brad Fitzpatrick, Ian Lance Taylor.
Patch set 1:Code-Review -1
1 comment:
Patchset:
There already is a CL for this: https://go-review.googlesource.com/c/go/+/359675
To view, visit change 392194. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: jiahua wang, Brad Fitzpatrick, Ian Lance Taylor.
2 comments:
File src/bytes/bytes.go:
Patch Set #1, Line 32: if a == nil || len(a) == 0
I think this is redundant nil also means len(a) == 0.
File src/bytes/bytes_test.go:
// func BenchmarkClone(b *testing.B) {
// var str = strings.Repeat("a", 42)
// b.ReportAllocs()
// for i := 0; i < b.N; i++ {
// stringSink = strings.Clone(str)
// }
// }
We should not leave uncommented code around.
To view, visit change 392194. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: jiahua wang, Brad Fitzpatrick, Ian Lance Taylor.
1 comment:
Patchset:
There already is a CL for this: https://go-review.googlesource. […]
unresolve
To view, visit change 392194. To unsubscribe, or for help writing mail filters, visit settings.