mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
all: remove unnecessary copying
Copying []byte to string should copy the data if necessary, as the Go specification assures that strings are immutable.
This commit is contained in:
parent
14041c61c0
commit
303965e1a9
@ -280,13 +280,7 @@ func (s NSString) InitWithUTF8String(utf8 string) NSString {
|
||||
}
|
||||
|
||||
func (s NSString) String() string {
|
||||
// The lifetime of the underlying C string is shorter than s [1].
|
||||
// Copy the bytes to ensure that the returned string's lifetime is unrelated to s.
|
||||
// [1] https://developer.apple.com/documentation/foundation/nsstring/1411189-utf8string?language=objc
|
||||
src := unsafe.Slice((*byte)(unsafe.Pointer(s.Send(sel_UTF8String))), s.Send(sel_length))
|
||||
dst := make([]byte, len(src))
|
||||
copy(dst, src)
|
||||
return string(dst)
|
||||
return string(unsafe.Slice((*byte)(unsafe.Pointer(s.Send(sel_UTF8String))), s.Send(sel_length)))
|
||||
}
|
||||
|
||||
type NSNotification struct {
|
||||
|
@ -2293,9 +2293,7 @@ func (i *_ID3DBlob) Release() uint32 {
|
||||
}
|
||||
|
||||
func (i *_ID3DBlob) String() string {
|
||||
bs := make([]byte, int(i.GetBufferSize()))
|
||||
copy(bs, unsafe.Slice((*byte)(unsafe.Pointer(i.GetBufferPointer())), i.GetBufferSize()))
|
||||
return string(bs)
|
||||
return string(unsafe.Slice((*byte)(unsafe.Pointer(i.GetBufferPointer())), i.GetBufferSize()))
|
||||
}
|
||||
|
||||
type _IDXGIAdapter struct {
|
||||
|
@ -18,9 +18,7 @@ func GoStr(cstr *byte) string {
|
||||
x := unsafe.Slice(cstr, 1e9)
|
||||
for i, c := range x {
|
||||
if c == 0 {
|
||||
str := make([]byte, i)
|
||||
copy(str, x[:i])
|
||||
return string(str)
|
||||
return string(x[:i])
|
||||
}
|
||||
}
|
||||
return ""
|
||||
|
Loading…
Reference in New Issue
Block a user