Skip to content

Commit

Permalink
strings: move Clone to stringslite
Browse files Browse the repository at this point in the history
This help package like unique use Clone.

Change-Id: I073125b7ba0e3b87e9cb78c3fc0b5bada78ba4cd
  • Loading branch information
qiulaidongfeng committed Apr 29, 2024
1 parent 99ee616 commit 8f1dae6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
14 changes: 13 additions & 1 deletion src/internal/stringslite/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -122,3 +125,12 @@ func CutSuffix(s, suffix string) (before string, found bool) {
}
return s[:len(s)-len(suffix)], true
}

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))
}
9 changes: 2 additions & 7 deletions src/strings/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package strings

import (
"unsafe"
"internal/stringslite"
)

// Clone returns a fresh copy of s.
Expand All @@ -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)
}

0 comments on commit 8f1dae6

Please sign in to comment.