]> Cypherpunks repositories - gostls13.git/commitdiff
strings: add IndexByte, for consistency with bytes package
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 1 Aug 2013 18:17:26 +0000 (11:17 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 1 Aug 2013 18:17:26 +0000 (11:17 -0700)
I always forget which package has it.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/12214044

src/pkg/strings/strings.go

index 986f6d61ebc94d264751f0cbb1411463c479ae4e..c7ec04b071a5c6c27b3a3396096cf6da540b7bd9 100644 (file)
@@ -130,14 +130,7 @@ func Index(s, sep string) int {
        case n == 0:
                return 0
        case n == 1:
-               c := sep[0]
-               // special case worth making fast
-               for i := 0; i < len(s); i++ {
-                       if s[i] == c {
-                               return i
-                       }
-               }
-               return -1
+               return IndexByte(s, sep[0])
        case n == len(s):
                if sep == s {
                        return 0
@@ -167,6 +160,16 @@ func Index(s, sep string) int {
        return -1
 }
 
+// IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.
+func IndexByte(s string, c byte) int {
+       for i := 0; i < len(s); i++ {
+               if s[i] == c {
+                       return i
+               }
+       }
+       return -1
+}
+
 // LastIndex returns the index of the last instance of sep in s, or -1 if sep is not present in s.
 func LastIndex(s, sep string) int {
        n := len(sep)