From a1dec403e8dbcfb4efe93c34eadbb2b2e0b17dc3 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Wed, 18 Nov 2009 19:23:08 -0800 Subject: 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 --- src/pkg/bytes/bytes.go | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/pkg/bytes/bytes.go') 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); -- cgit v1.2.3