mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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
This commit is contained in:
parent
f0d174b776
commit
8530e8ef65
4 changed files with 79 additions and 9 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue