From 489102de18cff38d1b12d09eeb7e60af42492d63 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Thu, 17 Mar 2022 11:41:24 +0100 Subject: [PATCH] syscall: optimize UTF16{,Ptr}FromString 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 Run-TryBot: Tobias Klauser TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor --- src/syscall/syscall_windows.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/syscall/syscall_windows.go b/src/syscall/syscall_windows.go index aba6c3f5fb8b8..adc865fd5ffbe 100644 --- a/src/syscall/syscall_windows.go +++ b/src/syscall/syscall_windows.go @@ -8,6 +8,7 @@ package syscall import ( errorspkg "errors" + "internal/bytealg" "internal/itoa" "internal/oserror" "internal/race" @@ -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 }