-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Specialize the conversion between wchar_t* and Go string on Windows o…
…nly.
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package hid | ||
|
||
/* | ||
#include <wchar.h> | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"unsafe" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
// gotowcs converts a Go string to a C wide string. The returned string must | ||
// be freed by the caller by calling C.free. | ||
func gotowcs(s string) *C.wchar_t { | ||
u16s, err := windows.UTF16FromString(s) | ||
if err != nil { | ||
panic(err) | ||
} | ||
n := C.size_t(len(u16s)) | ||
wcs := (*C.wchar_t)(calloc(n+1, C.sizeof_wchar_t)) | ||
return C.wmemcpy(wcs, (*C.wchar_t)(unsafe.Pointer(&u16s[0])), n) | ||
} | ||
|
||
// wcstogo converts a C wide string to a Go string. | ||
func wcstogo(wcs *C.wchar_t) string { | ||
return windows.UTF16PtrToString((*uint16)(unsafe.Pointer(wcs))) | ||
} |