diff options
| author | Brad Fitzpatrick <bradfitz@golang.org> | 2013-08-01 11:17:26 -0700 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2013-08-01 11:17:26 -0700 |
| commit | 9742003ffc7fd72ce2b433e9895ecbb6d9e4c720 (patch) | |
| tree | 508e060256e455bef28d4cdba1cbec61bd8675d5 /src/pkg | |
| parent | b99fa8155514d4a5dad366dde9be8ae76333e6a9 (diff) | |
| download | go-9742003ffc7fd72ce2b433e9895ecbb6d9e4c720.tar.xz | |
strings: add IndexByte, for consistency with bytes package
I always forget which package has it.
R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/12214044
Diffstat (limited to 'src/pkg')
| -rw-r--r-- | src/pkg/strings/strings.go | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/src/pkg/strings/strings.go b/src/pkg/strings/strings.go index 986f6d61eb..c7ec04b071 100644 --- a/src/pkg/strings/strings.go +++ b/src/pkg/strings/strings.go @@ -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) |
