2019-09-22 17:42:51 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-12-08 18:35:13 +01:00
|
|
|
|
2022-05-27 11:26:53 +02:00
|
|
|
//go:build darwin || windows
|
|
|
|
|
2018-12-08 18:35:13 +01:00
|
|
|
package gl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
|
|
|
// corresponding Go string.
|
2022-11-09 04:51:02 +01:00
|
|
|
func GoStr(cstr *byte) string {
|
|
|
|
if cstr == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
x := unsafe.Slice(cstr, 1e9)
|
|
|
|
for i, c := range x {
|
|
|
|
if c == 0 {
|
|
|
|
str := make([]byte, i)
|
|
|
|
copy(str, x[:i])
|
|
|
|
return string(str)
|
2018-12-08 18:35:13 +01:00
|
|
|
}
|
|
|
|
}
|
2022-11-09 04:51:02 +01:00
|
|
|
return ""
|
2018-12-08 18:35:13 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 08:07:30 +01:00
|
|
|
// CStr takes a Go string (with or without null-termination)
|
|
|
|
// and returns the C counterpart.
|
2018-12-08 18:35:13 +01:00
|
|
|
//
|
2022-11-09 08:07:30 +01:00
|
|
|
// The returned free function must be called once you are done using the string
|
2018-12-08 18:35:13 +01:00
|
|
|
// in order to free the memory.
|
2022-11-09 08:07:30 +01:00
|
|
|
func CStr(str string) (cstr *byte, free func()) {
|
|
|
|
bs := []byte(str)
|
|
|
|
if len(bs) == 0 || bs[len(bs)-1] != 0 {
|
|
|
|
bs = append(bs, 0)
|
2018-12-08 18:35:13 +01:00
|
|
|
}
|
2022-11-09 08:07:30 +01:00
|
|
|
return &bs[0], func() {
|
|
|
|
runtime.KeepAlive(bs)
|
|
|
|
bs = nil
|
2018-12-08 18:35:13 +01:00
|
|
|
}
|
|
|
|
}
|