예제 #1
0
파일: string.go 프로젝트: crazy2be/gojs
// Bytes returns a byte slice with the bytes of this string.
func (ref *String) Bytes() []byte {
	// Conversion 1, null-terminate UTF-8 string
	len := C.JSStringGetMaximumUTF8CStringSize((C.JSStringRef)(unsafe.Pointer(ref)))
	buffer := C.malloc(len)
	if buffer == nil {
		panic(syscall.ENOMEM)
	}
	defer C.free(buffer)
	strlenWithNul := C.JSStringGetUTF8CString((C.JSStringRef)(unsafe.Pointer(ref)), (*C.char)(buffer), len)

	// Conversion 2, Go []byte
	return C.GoBytes(buffer, C.int(strlenWithNul-1))
}
예제 #2
0
파일: string.go 프로젝트: crazy2be/gojs
func (ref *String) String() string {
	// Conversion 1, null-terminate UTF-8 string
	len := C.JSStringGetMaximumUTF8CStringSize((C.JSStringRef)(unsafe.Pointer(ref)))
	buffer := C.malloc(len)
	if buffer == nil {
		panic(syscall.ENOMEM)
	}
	defer C.free(buffer)
	C.JSStringGetUTF8CString((C.JSStringRef)(unsafe.Pointer(ref)), (*C.char)(buffer), len)

	// Conversion 2, Go string
	ret := C.GoString((*C.char)(buffer))
	return ret
}