]> Cypherpunks repositories - gostls13.git/commitdiff
strings: use fast path for IndexRune
authorBrad Fitzpatrick <bradfitz@golang.org>
Mon, 6 Oct 2014 22:10:51 +0000 (15:10 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 6 Oct 2014 22:10:51 +0000 (15:10 -0700)
Noticed while reviewing https://golang.org/cl/147690043/

I'd never seen anybody use IndexRune before, and
unsurprisingly it doesn't use the other fast paths in the
strings/bytes packages. IndexByte uses assembly.

Also, less code this way.

LGTM=r
R=r
CC=golang-codereviews
https://golang.org/cl/147700043

src/strings/strings.go

index 1b9df2e757fa1044febe8f2b92b56b08fda90166..27d384983efb3626f62a4fd9ad60beebb106ef83 100644 (file)
@@ -225,13 +225,8 @@ func LastIndex(s, sep string) int {
 // r, or -1 if rune is not present in s.
 func IndexRune(s string, r rune) int {
        switch {
-       case r < 0x80:
-               b := byte(r)
-               for i := 0; i < len(s); i++ {
-                       if s[i] == b {
-                               return i
-                       }
-               }
+       case r < utf8.RuneSelf:
+               return IndexByte(s, byte(r))
        default:
                for i, c := range s {
                        if c == r {