mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
strings: use fast path for IndexRune
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
This commit is contained in:
parent
f8f95590d9
commit
4731c382f6
1 changed files with 2 additions and 7 deletions
|
|
@ -225,13 +225,8 @@ func LastIndex(s, sep string) int {
|
||||||
// r, or -1 if rune is not present in s.
|
// r, or -1 if rune is not present in s.
|
||||||
func IndexRune(s string, r rune) int {
|
func IndexRune(s string, r rune) int {
|
||||||
switch {
|
switch {
|
||||||
case r < 0x80:
|
case r < utf8.RuneSelf:
|
||||||
b := byte(r)
|
return IndexByte(s, byte(r))
|
||||||
for i := 0; i < len(s); i++ {
|
|
||||||
if s[i] == b {
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
for i, c := range s {
|
for i, c := range s {
|
||||||
if c == r {
|
if c == r {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue