diff options
| author | Benny Siegert <bsiegert@gmail.com> | 2010-11-12 12:47:50 -0800 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2010-11-12 12:47:50 -0800 |
| commit | 8530e8ef6566c2345866a1e42b484dbf24c64264 (patch) | |
| tree | 721f327556136368ce2d973c29e3e2063770e17a /src/pkg/strings/strings.go | |
| parent | f0d174b776fd0312c6e92193e6a27c516a0b2da6 (diff) | |
| download | go-8530e8ef6566c2345866a1e42b484dbf24c64264.tar.xz | |
strings: add LastIndexAny
The need for a LastIndexAny function has come up in the discussion
for https://golang.org/cl/3008041/. This function is
implemented analogously to lastIndexFunc, using functions from
the utf8 package.
R=r, rsc, PeterGo
CC=golang-dev
https://golang.org/cl/3057041
Diffstat (limited to 'src/pkg/strings/strings.go')
| -rw-r--r-- | src/pkg/strings/strings.go | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/pkg/strings/strings.go b/src/pkg/strings/strings.go index f08b855999..8bf86dadd0 100644 --- a/src/pkg/strings/strings.go +++ b/src/pkg/strings/strings.go @@ -142,6 +142,24 @@ func IndexAny(s, chars string) int { return -1 } +// LastIndexAny returns the index of the last instance of any Unicode code +// point from chars in s, or -1 if no Unicode code point from chars is +// present in s. +func LastIndexAny(s, chars string) int { + if len(chars) > 0 { + for i := len(s); i > 0; { + rune, size := utf8.DecodeLastRuneInString(s[0:i]) + i -= size + for _, m := range chars { + if rune == m { + return i + } + } + } + } + return -1 +} + // Generic split: splits after each instance of sep, // including sepSave bytes of sep in the subarrays. func genSplit(s, sep string, sepSave, n int) []string { |
