Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2ee075d

Browse files
cuiweixiegopherbot
authored andcommittedSep 7, 2022
strings: simplify code using unsafe.StringData
Updates #54854 Change-Id: I93396dc92bd2decba895f2d059e1aeffcd22312c Reviewed-on: https://go-review.googlesource.com/c/go/+/428158 Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Bryan Mills <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 92b8f4e commit 2ee075d

File tree

5 files changed

+7
-12
lines changed

5 files changed

+7
-12
lines changed
 

‎src/strings/builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (b *Builder) copyCheck() {
4545

4646
// String returns the accumulated string.
4747
func (b *Builder) String() string {
48-
return *(*string)(unsafe.Pointer(&b.buf))
48+
return unsafe.String(unsafe.SliceData(b.buf), len(b.buf))
4949
}
5050

5151
// Len returns the number of accumulated bytes; b.Len() == len(b.String()).

‎src/strings/clone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ func Clone(s string) string {
2424
}
2525
b := make([]byte, len(s))
2626
copy(b, s)
27-
return *(*string)(unsafe.Pointer(&b))
27+
return unsafe.String(&b[0], len(b))
2828
}

‎src/strings/clone_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package strings_test
66

77
import (
8-
"reflect"
98
"strings"
109
"testing"
1110
"unsafe"
@@ -27,15 +26,12 @@ func TestClone(t *testing.T) {
2726
t.Errorf("Clone(%q) = %q; want %q", input, clone, input)
2827
}
2928

30-
inputHeader := (*reflect.StringHeader)(unsafe.Pointer(&input))
31-
cloneHeader := (*reflect.StringHeader)(unsafe.Pointer(&clone))
32-
if len(input) != 0 && cloneHeader.Data == inputHeader.Data {
29+
if len(input) != 0 && unsafe.StringData(clone) == unsafe.StringData(input) {
3330
t.Errorf("Clone(%q) return value should not reference inputs backing memory.", input)
3431
}
3532

36-
emptyHeader := (*reflect.StringHeader)(unsafe.Pointer(&emptyString))
37-
if len(input) == 0 && cloneHeader.Data != emptyHeader.Data {
38-
t.Errorf("Clone(%#v) return value should be equal to empty string.", inputHeader)
33+
if len(input) == 0 && unsafe.StringData(clone) != unsafe.StringData(emptyString) {
34+
t.Errorf("Clone(%#v) return value should be equal to empty string.", unsafe.StringData(input))
3935
}
4036
}
4137
}

‎src/strings/compare_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestCompareStrings(t *testing.T) {
5757
// unsafeString converts a []byte to a string with no allocation.
5858
// The caller must not modify b while the result string is in use.
5959
unsafeString := func(b []byte) string {
60-
return *(*string)(unsafe.Pointer(&b))
60+
return unsafe.String(unsafe.SliceData(b), len(b))
6161
}
6262

6363
lengths := make([]int, 0) // lengths to test in ascending order

‎src/strings/strings_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,7 @@ func TestMap(t *testing.T) {
660660
}
661661
orig := "Input string that we expect not to be copied."
662662
m = Map(identity, orig)
663-
if (*reflect.StringHeader)(unsafe.Pointer(&orig)).Data !=
664-
(*reflect.StringHeader)(unsafe.Pointer(&m)).Data {
663+
if unsafe.StringData(orig) != unsafe.StringData(m) {
665664
t.Error("unexpected copy during identity map")
666665
}
667666

0 commit comments

Comments
 (0)
Please sign in to comment.