Skip to content

Commit 76c7481

Browse files
committed
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>
1 parent 3275c40 commit 76c7481

File tree

4 files changed

+4
-20
lines changed

4 files changed

+4
-20
lines changed

plan9/syscall.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ func BytePtrToString(p *byte) string {
8080
ptr = unsafe.Pointer(uintptr(ptr) + 1)
8181
}
8282

83-
s := unsafe.Slice((*byte)(unsafe.Pointer(p)), n)
84-
return string(s)
83+
return string(unsafe.Slice(p, n))
8584
}
8685

8786
// Single-word zero for use when we need a valid pointer to 0 bytes.

unix/syscall.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ func BytePtrToString(p *byte) string {
8080
ptr = unsafe.Pointer(uintptr(ptr) + 1)
8181
}
8282

83-
s := unsafe.Slice((*byte)(unsafe.Pointer(p)), n)
84-
return string(s)
83+
return string(unsafe.Slice(p, n))
8584
}
8685

8786
// Single-word zero for use when we need a valid pointer to 0 bytes.

windows/syscall.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ import (
3030
"strings"
3131
"syscall"
3232
"unsafe"
33-
34-
"golang.org/x/sys/internal/unsafeheader"
3533
)
3634

3735
// ByteSliceFromString returns a NUL-terminated slice of bytes
@@ -83,13 +81,7 @@ func BytePtrToString(p *byte) string {
8381
ptr = unsafe.Pointer(uintptr(ptr) + 1)
8482
}
8583

86-
var s []byte
87-
h := (*unsafeheader.Slice)(unsafe.Pointer(&s))
88-
h.Data = unsafe.Pointer(p)
89-
h.Len = n
90-
h.Cap = n
91-
92-
return string(s)
84+
return string(unsafe.Slice(p, n))
9385
}
9486

9587
// Single-word zero for use when we need a valid pointer to 0 bytes.

windows/syscall_windows.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,7 @@ func UTF16PtrToString(p *uint16) string {
138138
ptr = unsafe.Pointer(uintptr(ptr) + unsafe.Sizeof(*p))
139139
}
140140

141-
var s []uint16
142-
h := (*unsafeheader.Slice)(unsafe.Pointer(&s))
143-
h.Data = unsafe.Pointer(p)
144-
h.Len = n
145-
h.Cap = n
146-
147-
return string(utf16.Decode(s))
141+
return string(utf16.Decode(unsafe.Slice(p, n)))
148142
}
149143

150144
func Getpagesize() int { return 4096 }

0 commit comments

Comments
 (0)