]> Cypherpunks repositories - gostls13.git/commitdiff
strings: simplify code using unsafe.StringData
authorcuiweixie <cuiweixie@gmail.com>
Sat, 3 Sep 2022 06:35:41 +0000 (14:35 +0800)
committerGopher Robot <gobot@golang.org>
Wed, 7 Sep 2022 01:33:55 +0000 (01:33 +0000)
Updates #54854

Change-Id: I93396dc92bd2decba895f2d059e1aeffcd22312c
Reviewed-on: https://go-review.googlesource.com/c/go/+/428158
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
src/strings/builder.go
src/strings/clone.go
src/strings/clone_test.go
src/strings/compare_test.go
src/strings/strings_test.go

index 096e9c765e26e7c039ff0ae7e4be006ba346a6e1..7710464a0d6f3bdf9de7bff3e71453aec8595a69 100644 (file)
@@ -45,7 +45,7 @@ func (b *Builder) copyCheck() {
 
 // String returns the accumulated string.
 func (b *Builder) String() string {
-       return *(*string)(unsafe.Pointer(&b.buf))
+       return unsafe.String(unsafe.SliceData(b.buf), len(b.buf))
 }
 
 // Len returns the number of accumulated bytes; b.Len() == len(b.String()).
index edd1497d9e58811add83d1fd72378912a5396e87..d14df11d497918a15e7d63d2e0366096d1b16153 100644 (file)
@@ -24,5 +24,5 @@ func Clone(s string) string {
        }
        b := make([]byte, len(s))
        copy(b, s)
-       return *(*string)(unsafe.Pointer(&b))
+       return unsafe.String(&b[0], len(b))
 }
index a9ba8add2355245a532f57b7cefa93998b3ee4fa..77479cfacf6fa25972970d4e08299ea4aaa18ce8 100644 (file)
@@ -5,7 +5,6 @@
 package strings_test
 
 import (
-       "reflect"
        "strings"
        "testing"
        "unsafe"
@@ -27,15 +26,12 @@ func TestClone(t *testing.T) {
                        t.Errorf("Clone(%q) = %q; want %q", input, clone, input)
                }
 
-               inputHeader := (*reflect.StringHeader)(unsafe.Pointer(&input))
-               cloneHeader := (*reflect.StringHeader)(unsafe.Pointer(&clone))
-               if len(input) != 0 && cloneHeader.Data == inputHeader.Data {
+               if len(input) != 0 && unsafe.StringData(clone) == unsafe.StringData(input) {
                        t.Errorf("Clone(%q) return value should not reference inputs backing memory.", input)
                }
 
-               emptyHeader := (*reflect.StringHeader)(unsafe.Pointer(&emptyString))
-               if len(input) == 0 && cloneHeader.Data != emptyHeader.Data {
-                       t.Errorf("Clone(%#v) return value should be equal to empty string.", inputHeader)
+               if len(input) == 0 && unsafe.StringData(clone) != unsafe.StringData(emptyString) {
+                       t.Errorf("Clone(%#v) return value should be equal to empty string.", unsafe.StringData(input))
                }
        }
 }
index 94554e0af794c5582e51240dfc46682058781b7c..a43578423da013b5c198d2d15efd5f61039e2f2b 100644 (file)
@@ -57,7 +57,7 @@ func TestCompareStrings(t *testing.T) {
        // unsafeString converts a []byte to a string with no allocation.
        // The caller must not modify b while the result string is in use.
        unsafeString := func(b []byte) string {
-               return *(*string)(unsafe.Pointer(&b))
+               return unsafe.String(unsafe.SliceData(b), len(b))
        }
 
        lengths := make([]int, 0) // lengths to test in ascending order
index 6d394f47bed9c77d8d9461cfe450807f34474a5d..8af81a556bf476f87fd2c15fdb779ebe82f85ecd 100644 (file)
@@ -660,8 +660,7 @@ func TestMap(t *testing.T) {
        }
        orig := "Input string that we expect not to be copied."
        m = Map(identity, orig)
-       if (*reflect.StringHeader)(unsafe.Pointer(&orig)).Data !=
-               (*reflect.StringHeader)(unsafe.Pointer(&m)).Data {
+       if unsafe.StringData(orig) != unsafe.StringData(m) {
                t.Error("unexpected copy during identity map")
        }