From 8530e8ef6566c2345866a1e42b484dbf24c64264 Mon Sep 17 00:00:00 2001 From: Benny Siegert Date: Fri, 12 Nov 2010 12:47:50 -0800 Subject: 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 --- src/pkg/bytes/bytes.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/pkg/bytes/bytes.go') 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 { -- cgit v1.3-5-g9baa