conversions_windows.go (1077B)
1 // SPDX-License-Identifier: MIT 2 3 package gl 4 5 import ( 6 "runtime" 7 "strings" 8 "unsafe" 9 ) 10 11 // GoStr takes a null-terminated string returned by OpenGL and constructs a 12 // corresponding Go string. 13 func GoStr(cstr *uint8) string { 14 str := "" 15 for { 16 if *cstr == 0 { 17 break 18 } 19 str += string(*cstr) 20 cstr = (*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(cstr)) + 1)) 21 } 22 return str 23 } 24 25 // Strs takes a list of Go strings (with or without null-termination) and 26 // returns their C counterpart. 27 // 28 // The returned free function must be called once you are done using the strings 29 // in order to free the memory. 30 // 31 // If no strings are provided as a parameter this function will panic. 32 func Strs(strs ...string) (cstrs **uint8, free func()) { 33 if len(strs) == 0 { 34 panic("Strs: expected at least 1 string") 35 } 36 37 var pinned []string 38 var ptrs []*uint8 39 for _, str := range strs { 40 if !strings.HasSuffix(str, "\x00") { 41 str += "\x00" 42 } 43 pinned = append(pinned, str) 44 ptrs = append(ptrs, Str(str)) 45 } 46 47 return &ptrs[0], func() { 48 runtime.KeepAlive(pinned) 49 pinned = nil 50 } 51 }