Skip to content

Commit

Permalink
unique: use stringslite.Clone
Browse files Browse the repository at this point in the history
Change-Id: Icb1ba7df1f0414090632e663b6a92b492546070d
GitHub-Last-Rev: 5169d26
GitHub-Pull-Request: #67092
Reviewed-on: https://go-review.googlesource.com/c/go/+/581940
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Reviewed-by: Keith Randall <khr@google.com>
  • Loading branch information
qiulaidongfeng authored and gopherbot committed May 5, 2024
1 parent 20130cc commit 4dccb78
Showing 1 changed file with 2 additions and 13 deletions.
15 changes: 2 additions & 13 deletions src/unique/clone.go
Expand Up @@ -6,6 +6,7 @@ package unique

import (
"internal/abi"
"internal/stringslite"
"unsafe"
)

Expand All @@ -20,7 +21,7 @@ import (
func clone[T comparable](value T, seq *cloneSeq) T {
for _, offset := range seq.stringOffsets {
ps := (*string)(unsafe.Pointer(uintptr(unsafe.Pointer(&value)) + offset))
*ps = cloneString(*ps)
*ps = stringslite.Clone(*ps)
}
return value
}
Expand Down Expand Up @@ -86,15 +87,3 @@ func buildArrayCloneSeq(typ *abi.Type, seq *cloneSeq, baseOffset uintptr) {
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))
}

0 comments on commit 4dccb78

Please sign in to comment.