Procházet zdrojové kódy

unix: use strings.IndexByte instead of for loops

Change-Id: I8d91f4f959b03f71a8f2effdf7f1c6d1308f2217
Reviewed-on: https://go-review.googlesource.com/102135
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Tobias Klauser před 7 roky
rodič
revize
91ee8cde43
2 změnil soubory, kde provedl 9 přidání a 13 odebrání
  1. 4 4
      unix/syscall.go
  2. 5 9
      unix/syscall_freebsd.go

+ 4 - 4
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)

+ 5 - 9
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
 	}