mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
303965e1a9
Copying []byte to string should copy the data if necessary, as the Go specification assures that strings are immutable.
42 lines
798 B
Go
42 lines
798 B
Go
// SPDX-License-Identifier: MIT
|
|
|
|
//go:build darwin || windows
|
|
|
|
package gl
|
|
|
|
import (
|
|
"runtime"
|
|
"unsafe"
|
|
)
|
|
|
|
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
|
// corresponding Go string.
|
|
func GoStr(cstr *byte) string {
|
|
if cstr == nil {
|
|
return ""
|
|
}
|
|
x := unsafe.Slice(cstr, 1e9)
|
|
for i, c := range x {
|
|
if c == 0 {
|
|
return string(x[:i])
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// CStr takes a Go string (with or without null-termination)
|
|
// and returns the C counterpart.
|
|
//
|
|
// The returned free function must be called once you are done using the string
|
|
// in order to free the memory.
|
|
func CStr(str string) (cstr *byte, free func()) {
|
|
bs := []byte(str)
|
|
if len(bs) == 0 || bs[len(bs)-1] != 0 {
|
|
bs = append(bs, 0)
|
|
}
|
|
return &bs[0], func() {
|
|
runtime.KeepAlive(bs)
|
|
bs = nil
|
|
}
|
|
}
|