• Joe Tsai's avatar
    bytes, strings: fix regression in IndexRune · 4b266578
    Joe Tsai authored
    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: default avatarBrad Fitzpatrick <bradfitz@golang.org>
    4b266578
strings.go 19 KB