]> Cypherpunks repositories - gostls13.git/commit
bytes, strings: fix regression in IndexRune
authorJoe Tsai <joetsai@digital-static.net>
Wed, 26 Oct 2016 21:18:37 +0000 (14:18 -0700)
committerJoe Tsai <thebrokentoaster@gmail.com>
Wed, 26 Oct 2016 23:02:27 +0000 (23:02 +0000)
commit4b2665786ec13c82ab751cd2d4312772b80cef12
treeb3e0cc0d9677b3bc8400c6280e054eb144ecb1cb
parent4f1e7be51f401a5374c0def2df0773abc924b03c
bytes, strings: fix regression in IndexRune

In all previous versions of Go, the behavior of IndexRune(s, r)
where r was utf.RuneError was that it would effectively return the
index of any invalid UTF-8 byte sequence (include RuneError).
Optimizations made in http://golang.org/cl/28537 and
http://golang.org/cl/28546 altered this undocumented behavior such
that RuneError would only match on the RuneError rune itself.

Although, the new behavior is arguably reasonable, it did break code
that depended on the previous behavior. Thus, we add special checks
to ensure that we preserve the old behavior.

There is a slight performance hit for correctness:
benchmark                   old ns/op     new ns/op     delta
BenchmarkIndexRune/10-4     19.3          21.6          +11.92%
BenchmarkIndexRune/32-4     33.6          35.2          +4.76%
This only occurs on small strings. The performance hit for larger strings
is neglible and not shown.

Fixes #17611

Change-Id: I1d863a741213d46c40b2e1724c41245df52502a5
Reviewed-on: https://go-review.googlesource.com/32123
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/bytes/bytes.go
src/bytes/bytes_test.go
src/strings/strings.go
src/strings/strings_test.go