Skip to content

Commit

Permalink
syscall: optimize UTF16{,Ptr}FromString
Browse files Browse the repository at this point in the history
Use bytealg.IndexByteString in UTF16FromString instead of an open-coded
loop.

Change-Id: I366448382f2d0adeca6b254131e0087a1f489258
Reviewed-on: https://go-review.googlesource.com/c/go/+/393614
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>
  • Loading branch information
tklauser committed Mar 18, 2022
1 parent 3ea22cf commit 489102d
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/syscall/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package syscall

import (
errorspkg "errors"
"internal/bytealg"
"internal/itoa"
"internal/oserror"
"internal/race"
Expand Down Expand Up @@ -39,10 +40,8 @@ func StringToUTF16(s string) []uint16 {
// s, with a terminating NUL added. If s contains a NUL byte at any
// location, it returns (nil, EINVAL).
func UTF16FromString(s string) ([]uint16, error) {
for i := 0; i < len(s); i++ {
if s[i] == 0 {
return nil, EINVAL
}
if bytealg.IndexByteString(s, 0) != -1 {
return nil, EINVAL
}
return utf16.Encode([]rune(s + "\x00")), nil
}
Expand Down

0 comments on commit 489102d

Please sign in to comment.