summaryrefslogtreecommitdiff
path: root/src/pkg/bytes/bytes.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2009-11-18 19:23:08 -0800
committerRob Pike <r@golang.org>2009-11-18 19:23:08 -0800
commita1dec403e8dbcfb4efe93c34eadbb2b2e0b17dc3 (patch)
tree1fd5ff4b844c7f44464318cc686f3dce4ac76e3f /src/pkg/bytes/bytes.go
parent44b150139b3597809cf41ae7a2eb9491e10254e9 (diff)
downloadgolang-a1dec403e8dbcfb4efe93c34eadbb2b2e0b17dc3.tar.gz
add bytes.IndexByte; common case we can make fast later.
also pick off the special case in strings.Index. don't want strings.IndexByte because the call site will very rarely need to allocate and we can handle the test in the code itself. bytes.IndexByte can avoid a common allocation. R=rsc CC=golang-dev http://codereview.appspot.com/156091
Diffstat (limited to 'src/pkg/bytes/bytes.go')
-rw-r--r--src/pkg/bytes/bytes.go10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/pkg/bytes/bytes.go b/src/pkg/bytes/bytes.go
index 2739c5a3f..171fa3d1b 100644
--- a/src/pkg/bytes/bytes.go
+++ b/src/pkg/bytes/bytes.go
@@ -98,6 +98,16 @@ func Index(s, sep []byte) 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 []byte, c byte) int {
+ for i, b := range s {
+ if b == 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 []byte) int {
n := len(sep);