From ae6af9b3d8c88c29c81230abbe697cf61e2ae594 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Sat, 4 May 2024 00:40:18 +0000 Subject: strings: move Clone to stringslite 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 LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase Auto-Submit: Ian Lance Taylor --- src/internal/stringslite/strings.go | 14 +++++++++++++- src/strings/clone.go | 9 ++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/internal/stringslite/strings.go b/src/internal/stringslite/strings.go index c0c6e2dce5..4114b86130 100644 --- a/src/internal/stringslite/strings.go +++ b/src/internal/stringslite/strings.go @@ -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)) +} diff --git a/src/strings/clone.go b/src/strings/clone.go index d14df11d49..f965b5963a 100644 --- a/src/strings/clone.go +++ b/src/strings/clone.go @@ -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) } -- cgit v1.2.3-54-g00ecf