From 91ee8cde435411ca3f1cd365e8f20131aed4d0a1 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Thu, 22 Mar 2018 15:10:41 +0100 Subject: [PATCH] unix: use strings.IndexByte instead of for loops Change-Id: I8d91f4f959b03f71a8f2effdf7f1c6d1308f2217 Reviewed-on: https://go-review.googlesource.com/102135 Run-TryBot: Tobias Klauser TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- unix/syscall.go | 8 ++++---- unix/syscall_freebsd.go | 14 +++++--------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/unix/syscall.go b/unix/syscall.go index 14690792..ef35fce8 100644 --- a/unix/syscall.go +++ b/unix/syscall.go @@ -24,14 +24,14 @@ // holds a value of type syscall.Errno. package unix // import "golang.org/x/sys/unix" +import "strings" + // ByteSliceFromString returns a NUL-terminated slice of bytes // containing the text of s. If s contains a NUL byte at any // location, it returns (nil, EINVAL). func ByteSliceFromString(s string) ([]byte, error) { - for i := 0; i < len(s); i++ { - if s[i] == 0 { - return nil, EINVAL - } + if strings.IndexByte(s, 0) != -1 { + return nil, EINVAL } a := make([]byte, len(s)+1) copy(a, s) diff --git a/unix/syscall_freebsd.go b/unix/syscall_freebsd.go index 79869856..ba9df4ac 100644 --- a/unix/syscall_freebsd.go +++ b/unix/syscall_freebsd.go @@ -12,7 +12,10 @@ package unix -import "unsafe" +import ( + "strings" + "unsafe" +) // SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets. type SockaddrDatalink struct { @@ -134,14 +137,7 @@ func setattrlistTimes(path string, times []Timespec, flags int) error { // Derive extattr namespace and attribute name func xattrnamespace(fullattr string) (ns int, attr string, err error) { - s := -1 - for idx, val := range fullattr { - if val == '.' { - s = idx - break - } - } - + s := strings.IndexByte(fullattr, '.') if s == -1 { return -1, "", ENOATTR }