mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
bytes, strings: change lastIndexFunc to use DecodeLastRune
R=r CC=golang-dev, rsc https://golang.org/cl/2271041
This commit is contained in:
parent
f11271b82e
commit
81ba399a6a
2 changed files with 8 additions and 52 deletions
|
|
@ -490,34 +490,12 @@ func indexFunc(s []byte, f func(r int) bool, truth bool) int {
|
||||||
// truth==false, the sense of the predicate function is
|
// truth==false, the sense of the predicate function is
|
||||||
// inverted.
|
// inverted.
|
||||||
func lastIndexFunc(s []byte, f func(r int) bool, truth bool) int {
|
func lastIndexFunc(s []byte, f func(r int) bool, truth bool) int {
|
||||||
end := len(s)
|
for i := len(s); i > 0; {
|
||||||
for end > 0 {
|
rune, size := utf8.DecodeLastRune(s[0:i])
|
||||||
start := end - 1
|
i -= size
|
||||||
rune := int(s[start])
|
|
||||||
if rune >= utf8.RuneSelf {
|
|
||||||
// Back up & look for beginning of rune. Mustn't pass start.
|
|
||||||
for start--; start >= 0; start-- {
|
|
||||||
if utf8.RuneStart(s[start]) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if start < 0 {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
var wid int
|
|
||||||
rune, wid = utf8.DecodeRune(s[start:end])
|
|
||||||
|
|
||||||
// If we've decoded fewer bytes than we expected,
|
|
||||||
// we've got some invalid UTF-8, so make sure we return
|
|
||||||
// the last possible index in s.
|
|
||||||
if start+wid < end && f(utf8.RuneError) == truth {
|
|
||||||
return end - 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if f(rune) == truth {
|
if f(rune) == truth {
|
||||||
return start
|
return i
|
||||||
}
|
}
|
||||||
end = start
|
|
||||||
}
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -451,34 +451,12 @@ func indexFunc(s string, f func(r int) bool, truth bool) int {
|
||||||
// truth==false, the sense of the predicate function is
|
// truth==false, the sense of the predicate function is
|
||||||
// inverted.
|
// inverted.
|
||||||
func lastIndexFunc(s string, f func(r int) bool, truth bool) int {
|
func lastIndexFunc(s string, f func(r int) bool, truth bool) int {
|
||||||
end := len(s)
|
for i := len(s); i > 0; {
|
||||||
for end > 0 {
|
rune, size := utf8.DecodeLastRuneInString(s[0:i])
|
||||||
start := end - 1
|
i -= size
|
||||||
rune := int(s[start])
|
|
||||||
if rune >= utf8.RuneSelf {
|
|
||||||
// Back up & look for beginning of rune. Mustn't pass start.
|
|
||||||
for start--; start >= 0; start-- {
|
|
||||||
if utf8.RuneStart(s[start]) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if start < 0 {
|
|
||||||
start = 0
|
|
||||||
}
|
|
||||||
var wid int
|
|
||||||
rune, wid = utf8.DecodeRuneInString(s[start:end])
|
|
||||||
|
|
||||||
// If we've decoded fewer bytes than we expected,
|
|
||||||
// we've got some invalid UTF-8, so make sure we return
|
|
||||||
// the last possible index in s.
|
|
||||||
if start+wid < end && f(utf8.RuneError) == truth {
|
|
||||||
return end - 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if f(rune) == truth {
|
if f(rune) == truth {
|
||||||
return start
|
return i
|
||||||
}
|
}
|
||||||
end = start
|
|
||||||
}
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue