aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/strings/strings.go
diff options
context:
space:
mode:
authorRoger Peppe <rogpeppe@gmail.com>2010-09-23 20:40:11 +1000
committerRob Pike <r@golang.org>2010-09-23 20:40:11 +1000
commit81ba399a6ad45e6dcc995b83dfdd7bf17a657285 (patch)
tree72ca5db2ab48682d08cb042f413e2676ca9cc819 /src/pkg/strings/strings.go
parentf11271b82e123239e9263749b18b3ea9ad5c0610 (diff)
downloadgo-81ba399a6ad45e6dcc995b83dfdd7bf17a657285.tar.xz
bytes, strings: change lastIndexFunc to use DecodeLastRune
R=r CC=golang-dev, rsc https://golang.org/cl/2271041
Diffstat (limited to 'src/pkg/strings/strings.go')
-rw-r--r--src/pkg/strings/strings.go30
1 files changed, 4 insertions, 26 deletions
diff --git a/src/pkg/strings/strings.go b/src/pkg/strings/strings.go
index e3989c7582..6fbf67b3e6 100644
--- a/src/pkg/strings/strings.go
+++ b/src/pkg/strings/strings.go
@@ -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
// inverted.
func lastIndexFunc(s string, f func(r int) bool, truth bool) int {
- end := len(s)
- for end > 0 {
- start := end - 1
- 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
- }
- }
+ for i := len(s); i > 0; {
+ rune, size := utf8.DecodeLastRuneInString(s[0:i])
+ i -= size
if f(rune) == truth {
- return start
+ return i
}
- end = start
}
return -1
}