1
0
mirror of https://github.com/golang/go synced 2024-11-15 05:20:21 -07:00

unique: not duplication implement cloneString

Change-Id: I3af3bc01be3730172838a2fd4992e9db11cb6f37
This commit is contained in:
qiulaidongfeng 2024-04-28 15:27:03 +08:00
parent ae6af9b3d8
commit 5169d26813

View File

@ -6,6 +6,7 @@ package unique
import ( import (
"internal/abi" "internal/abi"
"internal/stringslite"
"unsafe" "unsafe"
) )
@ -20,7 +21,7 @@ import (
func clone[T comparable](value T, seq *cloneSeq) T { func clone[T comparable](value T, seq *cloneSeq) T {
for _, offset := range seq.stringOffsets { for _, offset := range seq.stringOffsets {
ps := (*string)(unsafe.Pointer(uintptr(unsafe.Pointer(&value)) + offset)) ps := (*string)(unsafe.Pointer(uintptr(unsafe.Pointer(&value)) + offset))
*ps = cloneString(*ps) *ps = stringslite.Clone(*ps)
} }
return value return value
} }
@ -86,15 +87,3 @@ func buildArrayCloneSeq(typ *abi.Type, seq *cloneSeq, baseOffset uintptr) {
offset = (offset + align - 1) &^ (align - 1) offset = (offset + align - 1) &^ (align - 1)
} }
} }
// cloneString is a copy of strings.Clone, because we can't depend on the strings
// package here. Several packages that might make use of unique, like net, explicitly
// forbid depending on unicode, which strings depends on.
func cloneString(s string) string {
if len(s) == 0 {
return ""
}
b := make([]byte, len(s))
copy(b, s)
return unsafe.String(&b[0], len(b))
}