// overuse of Clone can make programs use more memory.
// Clone should typically be used only rarely, and only when
// profiling indicates that it is needed.
+// For strings of length zero the string "" will be returned
+// and no allocation is made.
func Clone(s string) string {
+ if len(s) == 0 {
+ return ""
+ }
b := make([]byte, len(s))
copy(b, s)
return *(*string)(unsafe.Pointer(&b))
"unsafe"
)
+var emptyString string
+
func TestClone(t *testing.T) {
var cloneTests = []string{
"",
+ strings.Clone(""),
+ strings.Repeat("a", 42)[:0],
"short",
strings.Repeat("a", 42),
}
inputHeader := (*reflect.StringHeader)(unsafe.Pointer(&input))
cloneHeader := (*reflect.StringHeader)(unsafe.Pointer(&clone))
- if inputHeader.Data == cloneHeader.Data {
+ if len(input) != 0 && cloneHeader.Data == inputHeader.Data {
t.Errorf("Clone(%q) return value should not reference inputs backing memory.", input)
}
+
+ emptyHeader := (*reflect.StringHeader)(unsafe.Pointer(&emptyString))
+ if len(input) == 0 && cloneHeader.Data != emptyHeader.Data {
+ t.Errorf("Clone(%#v) return value should be equal to empty string.", inputHeader)
+ }
}
}