]> Cypherpunks repositories - gostls13.git/commitdiff
strings: move Clone to stringslite
authorqiulaidongfeng <2645477756@qq.com>
Sat, 4 May 2024 00:40:18 +0000 (00:40 +0000)
committerGopher Robot <gobot@golang.org>
Sat, 4 May 2024 01:23:42 +0000 (01:23 +0000)
Follow-up CL help package like unique use Clone.

Change-Id: Ie64adf7e1a331f47c3cfe178c368d72fc72493ff
GitHub-Last-Rev: 499476cc4acdf58ecf0fec9f7281bfb90edc7c82
GitHub-Pull-Request: golang/go#67106
Reviewed-on: https://go-review.googlesource.com/c/go/+/581956
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>

src/internal/stringslite/strings.go
src/strings/clone.go

index c0c6e2dce56e416889a7cb0926fda178f82827ec..4114b86130006648953105967cd4691b04a50a76 100644 (file)
@@ -8,7 +8,10 @@
 // Tests for these functions are in the strings package.
 package stringslite
 
-import "internal/bytealg"
+import (
+       "internal/bytealg"
+       "unsafe"
+)
 
 func HasPrefix(s, prefix string) bool {
        return len(s) >= len(prefix) && s[0:len(prefix)] == prefix
@@ -136,3 +139,12 @@ func TrimSuffix(s, suffix string) string {
        }
        return s
 }
+
+func Clone(s string) string {
+       if len(s) == 0 {
+               return ""
+       }
+       b := make([]byte, len(s))
+       copy(b, s)
+       return unsafe.String(&b[0], len(b))
+}
index d14df11d497918a15e7d63d2e0366096d1b16153..f965b5963a3b7c3bfd8fdc0b675d61a8a7102e49 100644 (file)
@@ -5,7 +5,7 @@
 package strings
 
 import (
-       "unsafe"
+       "internal/stringslite"
 )
 
 // Clone returns a fresh copy of s.
@@ -19,10 +19,5 @@ import (
 // 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 unsafe.String(&b[0], len(b))
+       return stringslite.Clone(s)
 }