1
0
mirror of https://github.com/golang/go synced 2024-11-18 09:04:49 -07:00

all: use unsafe.{Slice, SliceData, String, StringData} to simplify code

Because most of these APIs are recently supported, we can only do some
advancement work as much as possible under the premise of compatibility.

For #54854.

Change-Id: Id15d11288bf23902570d54eaf2704a5264210b2e
Reviewed-on: https://go-review.googlesource.com/c/go/+/429115
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: hopehook <hopehook@golangcn.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
This commit is contained in:
hopehook 2022-09-07 22:34:48 +08:00 committed by Gopher Robot
parent 2c3187cd42
commit a9a398220f
3 changed files with 5 additions and 22 deletions

View File

@ -14,7 +14,6 @@ import (
"go/token"
"internal/godebug"
"internal/goroot"
"internal/unsafeheader"
"path"
"path/filepath"
"runtime"
@ -948,14 +947,7 @@ func (sf *sourceFile) embeds() []embed {
}
func asString(b []byte) string {
p := (*unsafeheader.Slice)(unsafe.Pointer(&b)).Data
var s string
hdr := (*unsafeheader.String)(unsafe.Pointer(&s))
hdr.Data = p
hdr.Len = len(b)
return s
return unsafe.String(unsafe.SliceData(b), len(b))
}
// A decoder helps decode the index format.

View File

@ -13,7 +13,6 @@
package maphash
import (
"internal/unsafeheader"
"unsafe"
)
@ -72,11 +71,11 @@ func String(seed Seed, s string) uint64 {
panic("maphash: use of uninitialized Seed")
}
for len(s) > bufSize {
p := (*byte)((*unsafeheader.String)(unsafe.Pointer(&s)).Data)
p := (*byte)(unsafe.StringData(s))
state = rthash(p, bufSize, state)
s = s[bufSize:]
}
p := (*byte)((*unsafeheader.String)(unsafe.Pointer(&s)).Data)
p := (*byte)(unsafe.StringData(s))
return rthash(p, len(s), state)
}
@ -190,7 +189,7 @@ func (h *Hash) WriteString(s string) (int, error) {
if len(s) > bufSize {
h.initSeed()
for len(s) > bufSize {
ptr := (*byte)((*unsafeheader.String)(unsafe.Pointer(&s)).Data)
ptr := (*byte)(unsafe.StringData(s))
h.state.s = rthash(ptr, bufSize, h.state.s)
s = s[bufSize:]
}

View File

@ -7,7 +7,6 @@
package fuzz
import (
"internal/unsafeheader"
"unsafe"
)
@ -18,12 +17,5 @@ import (
func coverage() []byte {
addr := unsafe.Pointer(&_counters)
size := uintptr(unsafe.Pointer(&_ecounters)) - uintptr(addr)
var res []byte
*(*unsafeheader.Slice)(unsafe.Pointer(&res)) = unsafeheader.Slice{
Data: addr,
Len: int(size),
Cap: int(size),
}
return res
return unsafe.Slice((*byte)(addr), int(size))
}