aboutsummaryrefslogtreecommitdiff
path: root/src/bytes/bytes.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2015-04-29 20:45:55 +0300
committerDmitry Vyukov <dvyukov@google.com>2015-04-30 07:13:18 +0000
commit0fb5475bdf0e5352d7aac67d2ec97c0513ee0af3 (patch)
treef929ea7874d05a660171feedf38458b799943591 /src/bytes/bytes.go
parent89454b1c390ce0659a4311f4a23642f952d9f574 (diff)
downloadgo-0fb5475bdf0e5352d7aac67d2ec97c0513ee0af3.tar.xz
bytes, strings: add LastIndexByte
Currently the packages have the following index functions: func Index(s, sep []byte) int func IndexAny(s []byte, chars string) int func IndexByte(s []byte, c byte) int func IndexFunc(s []byte, f func(r rune) bool) int func IndexRune(s []byte, r rune) int func LastIndex(s, sep []byte) int func LastIndexAny(s []byte, chars string) int func LastIndexFunc(s []byte, f func(r rune) bool) int Searching for the last occurrence of a byte is quite common for string parsing algorithms (e.g. find the last paren on a line). Also addition of LastIndexByte makes the set more orthogonal. Change-Id: Ida168849acacf8e78dd70c1354bef9eac5effafe Reviewed-on: https://go-review.googlesource.com/9500 Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/bytes/bytes.go')
-rw-r--r--src/bytes/bytes.go10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go
index 60de451504..b86824087e 100644
--- a/src/bytes/bytes.go
+++ b/src/bytes/bytes.go
@@ -138,6 +138,16 @@ func LastIndex(s, sep []byte) int {
return -1
}
+// LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.
+func LastIndexByte(s []byte, c byte) int {
+ for i := len(s) - 1; i >= 0; i-- {
+ if s[i] == c {
+ return i
+ }
+ }
+ return -1
+}
+
// IndexRune interprets s as a sequence of UTF-8-encoded Unicode code points.
// It returns the byte index of the first occurrence in s of the given rune.
// It returns -1 if rune is not present in s.