diff options
author | Russ Cox <rsc@golang.org> | 2009-10-12 10:09:35 -0700 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2009-10-12 10:09:35 -0700 |
commit | f2fcd2eb6650a2ee092139dfe0faca587576bdcd (patch) | |
tree | 8fb20b6f0ebab58fce3d0ad6363f4c844e1654cd /src/pkg/bytes/bytes.go | |
parent | 1ec7f0285bddd3bc1e8534fea6077a8b6f841f15 (diff) | |
download | golang-f2fcd2eb6650a2ee092139dfe0faca587576bdcd.tar.gz |
fix comment on strings.LastIndex.
add bytes.LastIndex.
add strings.Reader.
R=r
DELTA=59 (56 added, 0 deleted, 3 changed)
OCL=35585
CL=35601
Diffstat (limited to 'src/pkg/bytes/bytes.go')
-rw-r--r-- | src/pkg/bytes/bytes.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/pkg/bytes/bytes.go b/src/pkg/bytes/bytes.go index 103a89674..564c42d4a 100644 --- a/src/pkg/bytes/bytes.go +++ b/src/pkg/bytes/bytes.go @@ -112,6 +112,21 @@ func Index(s, sep []byte) int { return -1; } +// LastIndex returns the index of the last instance of sep in s, or -1 if sep is not present in s. +func LastIndex(s, sep []byte) int { + n := len(sep); + if n == 0 { + return len(s); + } + c := sep[0]; + for i := len(s)-n; i >= 0; i-- { + if s[i] == c && (n == 1 || Equal(s[i : i+n], sep)) { + return i; + } + } + return -1; +} + // Split splits the array s around each instance of sep, returning an array of subarrays of s. // If sep is empty, Split splits s after each UTF-8 sequence. // If n > 0, split Splits s into at most n subarrays; the last subarray will contain an unsplit remainder. |