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/bytes/bytes.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/bytes/bytes.go')
| -rw-r--r-- | src/pkg/bytes/bytes.go | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/pkg/bytes/bytes.go b/src/pkg/bytes/bytes.go index 1939fd5678..e26b29fb55 100644 --- a/src/pkg/bytes/bytes.go +++ b/src/pkg/bytes/bytes.go @@ -165,6 +165,25 @@ func IndexAny(s []byte, chars string) int { return -1 } +// LastIndexAny interprets s as a sequence of UTF-8-encoded Unicode code +// points. It returns the byte index of the last occurrence in s of any of +// the Unicode code points in chars. It returns -1 if chars is empty or if +// there is no code point in common. +func LastIndexAny(s []byte, chars string) int { + if len(chars) > 0 { + for i := len(s); i > 0; { + rune, size := utf8.DecodeLastRune(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 []byte, sepSave, n int) [][]byte { |
