mirror of
https://github.com/golang/sys.git
synced 2026-01-29 15:12:09 +03:00
all: simplify unsafe.Slice usage in {Byte,UTF16}PtrToString
On windows, use unsafe.Slice instead of unsafeheader as already the case for unix and plan9. The pointers are already *byte/*uint16, so the type conversion can be omitted as well. Change-Id: Ida7264cc0c1948bf563ed91d51e637edcdafb77a Reviewed-on: https://go-review.googlesource.com/c/sys/+/430515 Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com>
This commit is contained in:
committed by
Tobias Klauser
parent
3275c407ce
commit
76c7481b51
@@ -80,8 +80,7 @@ func BytePtrToString(p *byte) string {
|
||||
ptr = unsafe.Pointer(uintptr(ptr) + 1)
|
||||
}
|
||||
|
||||
s := unsafe.Slice((*byte)(unsafe.Pointer(p)), n)
|
||||
return string(s)
|
||||
return string(unsafe.Slice(p, n))
|
||||
}
|
||||
|
||||
// Single-word zero for use when we need a valid pointer to 0 bytes.
|
||||
|
||||
@@ -80,8 +80,7 @@ func BytePtrToString(p *byte) string {
|
||||
ptr = unsafe.Pointer(uintptr(ptr) + 1)
|
||||
}
|
||||
|
||||
s := unsafe.Slice((*byte)(unsafe.Pointer(p)), n)
|
||||
return string(s)
|
||||
return string(unsafe.Slice(p, n))
|
||||
}
|
||||
|
||||
// Single-word zero for use when we need a valid pointer to 0 bytes.
|
||||
|
||||
@@ -30,8 +30,6 @@ import (
|
||||
"strings"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/internal/unsafeheader"
|
||||
)
|
||||
|
||||
// ByteSliceFromString returns a NUL-terminated slice of bytes
|
||||
@@ -83,13 +81,7 @@ func BytePtrToString(p *byte) string {
|
||||
ptr = unsafe.Pointer(uintptr(ptr) + 1)
|
||||
}
|
||||
|
||||
var s []byte
|
||||
h := (*unsafeheader.Slice)(unsafe.Pointer(&s))
|
||||
h.Data = unsafe.Pointer(p)
|
||||
h.Len = n
|
||||
h.Cap = n
|
||||
|
||||
return string(s)
|
||||
return string(unsafe.Slice(p, n))
|
||||
}
|
||||
|
||||
// Single-word zero for use when we need a valid pointer to 0 bytes.
|
||||
|
||||
@@ -138,13 +138,7 @@ func UTF16PtrToString(p *uint16) string {
|
||||
ptr = unsafe.Pointer(uintptr(ptr) + unsafe.Sizeof(*p))
|
||||
}
|
||||
|
||||
var s []uint16
|
||||
h := (*unsafeheader.Slice)(unsafe.Pointer(&s))
|
||||
h.Data = unsafe.Pointer(p)
|
||||
h.Len = n
|
||||
h.Cap = n
|
||||
|
||||
return string(utf16.Decode(s))
|
||||
return string(utf16.Decode(unsafe.Slice(p, n)))
|
||||
}
|
||||
|
||||
func Getpagesize() int { return 4096 }
|
||||
|
||||
Reference in New Issue
Block a user