strings: add IndexRune tests, ASCII fast path

$ gotest -test.v -test.run=IndexRune -test.bench=.*
=== RUN  strings_test.TestIndexRune
--- PASS: strings_test.TestIndexRune (0.0 seconds)
PASS
strings_test.BenchmarkIndexRune	20000000   105 ns/op
strings_test.BenchmarkIndexByte	50000000    48 ns/op

R=rsc, dsymonds
CC=golang-dev
https://golang.org/cl/4267050
This commit is contained in:
Brad Fitzpatrick 2011-03-08 09:41:12 -08:00
parent 87aa93457e
commit 145108ed36
2 changed files with 53 additions and 3 deletions

View file

@ -119,9 +119,19 @@ func LastIndex(s, sep string) int {
// IndexRune returns the index of the first instance of the Unicode code point
// rune, or -1 if rune is not present in s.
func IndexRune(s string, rune int) int {
for i, c := range s {
if c == rune {
return i
switch {
case rune < 0x80:
b := byte(rune)
for i := 0; i < len(s); i++ {
if s[i] == b {
return i
}
}
default:
for i, c := range s {
if c == rune {
return i
}
}
}
return -1