From a6f4650873b0647f4a7562d1bc45c27d4da686d7 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Fri, 20 Jan 2023 14:39:19 +0100 Subject: [PATCH] windows: use UTF16FromString and UTF16ToString from syscall This CL updates the windows.UTF16FromString and windows.UTF16ToString implementation so they forward to their syscall counterparts. The standard library uses these particular syscall functions across the board as it can't depend on x/sys. This means that even if the syscall API is frozen, the implementation still receives improvements and bug fixes, such as CL 425054. Porting CL 425054 to x/sys is possible, but for this particular case I think it's better to just call the syscall package. This way both repos will benefit from future improvements (which I plan to do soon). Change-Id: I305c4409142d79a57008d7e6d0717fb8986fe906 Reviewed-on: https://go-review.googlesource.com/c/sys/+/462875 Run-TryBot: Ian Lance Taylor Auto-Submit: Ian Lance Taylor TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor Run-TryBot: Quim Muntal Reviewed-by: Bryan Mills --- windows/syscall_windows.go | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go index a49853e9..41cb3c01 100644 --- a/windows/syscall_windows.go +++ b/windows/syscall_windows.go @@ -10,7 +10,6 @@ import ( errorspkg "errors" "fmt" "runtime" - "strings" "sync" "syscall" "time" @@ -87,22 +86,13 @@ func StringToUTF16(s string) []uint16 { // s, with a terminating NUL added. If s contains a NUL byte at any // location, it returns (nil, syscall.EINVAL). func UTF16FromString(s string) ([]uint16, error) { - if strings.IndexByte(s, 0) != -1 { - return nil, syscall.EINVAL - } - return utf16.Encode([]rune(s + "\x00")), nil + return syscall.UTF16FromString(s) } // UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s, // with a terminating NUL and any bytes after the NUL removed. func UTF16ToString(s []uint16) string { - for i, v := range s { - if v == 0 { - s = s[:i] - break - } - } - return string(utf16.Decode(s)) + return syscall.UTF16ToString(s) } // StringToUTF16Ptr is deprecated. Use UTF16PtrFromString instead.