mirror of
https://github.com/rwinkhart/sys.git
synced 2026-09-05 08:37:31 -04:00
windows: optimize UTF16{,Ptr}FromString
Use strings.IndexByte in UTF16FromString instead of an open-coded loop. Change-Id: Iac4374d9a58e40659147d35c9ab21bb89f5da629 Reviewed-on: https://go-review.googlesource.com/c/sys/+/393594 Trust: Tobias Klauser <tobias.klauser@gmail.com> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
committed by
Tobias Klauser
parent
51cd9980da
commit
2edf467146
@@ -10,6 +10,7 @@ import (
|
|||||||
errorspkg "errors"
|
errorspkg "errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
@@ -86,10 +87,8 @@ func StringToUTF16(s string) []uint16 {
|
|||||||
// s, with a terminating NUL added. If s contains a NUL byte at any
|
// s, with a terminating NUL added. If s contains a NUL byte at any
|
||||||
// location, it returns (nil, syscall.EINVAL).
|
// location, it returns (nil, syscall.EINVAL).
|
||||||
func UTF16FromString(s string) ([]uint16, error) {
|
func UTF16FromString(s string) ([]uint16, error) {
|
||||||
for i := 0; i < len(s); i++ {
|
if strings.IndexByte(s, 0) != -1 {
|
||||||
if s[i] == 0 {
|
return nil, syscall.EINVAL
|
||||||
return nil, syscall.EINVAL
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return utf16.Encode([]rune(s + "\x00")), nil
|
return utf16.Encode([]rune(s + "\x00")), nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user