mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 01:45:36 +00:00
ui/app: merge goString implementations and add test
Fixes gio#30 Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+1
-24
@@ -4,7 +4,6 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
syscall "golang.org/x/sys/windows"
|
syscall "golang.org/x/sys/windows"
|
||||||
@@ -135,27 +134,5 @@ func eglTerminate(disp _EGLDisplay) bool {
|
|||||||
|
|
||||||
func eglQueryString(disp _EGLDisplay, name _EGLint) string {
|
func eglQueryString(disp _EGLDisplay, name _EGLint) string {
|
||||||
r, _, _ := _eglQueryString.Call(uintptr(disp), uintptr(name))
|
r, _, _ := _eglQueryString.Call(uintptr(disp), uintptr(name))
|
||||||
return goString(r)
|
return gl.GoString(gl.SliceOf(r))
|
||||||
}
|
|
||||||
|
|
||||||
func goString(s uintptr) string {
|
|
||||||
if s == 0 {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
sh := reflect.SliceHeader{
|
|
||||||
Data: s,
|
|
||||||
Len: 1 << 30,
|
|
||||||
Cap: 1 << 30,
|
|
||||||
}
|
|
||||||
sl := *(*[]byte)(unsafe.Pointer(&sh))
|
|
||||||
var v string
|
|
||||||
for i, c := range sl {
|
|
||||||
if c == 0 {
|
|
||||||
if i > 0 {
|
|
||||||
v = string(sl[:i-1])
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return v
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ package gl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
"reflect"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
@@ -284,7 +283,7 @@ func (c *Functions) GetShaderInfoLog(s Shader) string {
|
|||||||
}
|
}
|
||||||
func (c *Functions) GetString(pname Enum) string {
|
func (c *Functions) GetString(pname Enum) string {
|
||||||
s, _, _ := syscall.Syscall(_glGetString.Addr(), 1, uintptr(pname), 0, 0)
|
s, _, _ := syscall.Syscall(_glGetString.Addr(), 1, uintptr(pname), 0, 0)
|
||||||
return goString(s)
|
return GoString(SliceOf(s))
|
||||||
}
|
}
|
||||||
func (c *Functions) GetUniformLocation(p Program, name string) Uniform {
|
func (c *Functions) GetUniformLocation(p Program, name string) Uniform {
|
||||||
cname := cString(name)
|
cname := cString(name)
|
||||||
@@ -365,25 +364,3 @@ func cString(s string) []byte {
|
|||||||
copy(b, s)
|
copy(b, s)
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func goString(s uintptr) string {
|
|
||||||
if s == 0 {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
sh := reflect.SliceHeader{
|
|
||||||
Data: s,
|
|
||||||
Len: 1 << 30,
|
|
||||||
Cap: 1 << 30,
|
|
||||||
}
|
|
||||||
sl := *(*[]byte)(unsafe.Pointer(&sh))
|
|
||||||
var v string
|
|
||||||
for i, c := range sl {
|
|
||||||
if c == 0 {
|
|
||||||
if i > 0 {
|
|
||||||
v = string(sl[:i-1])
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -87,3 +87,28 @@ func ParseGLVersion(glVer string) ([2]int, error) {
|
|||||||
}
|
}
|
||||||
return ver, fmt.Errorf("failed to parse OpenGL ES version (%s)", glVer)
|
return ver, fmt.Errorf("failed to parse OpenGL ES version (%s)", glVer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SliceOf(s uintptr) []byte {
|
||||||
|
if s == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
sh := reflect.SliceHeader{
|
||||||
|
Data: s,
|
||||||
|
Len: 1 << 30,
|
||||||
|
Cap: 1 << 30,
|
||||||
|
}
|
||||||
|
return *(*[]byte)(unsafe.Pointer(&sh))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GoString convert a NUL-terminated C string
|
||||||
|
// to a Go string.
|
||||||
|
func GoString(s []byte) string {
|
||||||
|
i := 0
|
||||||
|
for {
|
||||||
|
if s[i] == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
return string(s[:i])
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package gl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGoString(t *testing.T) {
|
||||||
|
tests := [][2]string{
|
||||||
|
{"Hello\x00", "Hello"},
|
||||||
|
{"\x00", ""},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
got := GoString([]byte(test[0]))
|
||||||
|
if exp := test[1]; exp != got {
|
||||||
|
t.Errorf("expected %q got %q", exp, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user