summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2009-10-12 10:09:35 -0700
committerRuss Cox <rsc@golang.org>2009-10-12 10:09:35 -0700
commitf2fcd2eb6650a2ee092139dfe0faca587576bdcd (patch)
tree8fb20b6f0ebab58fce3d0ad6363f4c844e1654cd
parent1ec7f0285bddd3bc1e8534fea6077a8b6f841f15 (diff)
downloadgolang-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
-rw-r--r--src/pkg/Make.deps4
-rw-r--r--src/pkg/bytes/bytes.go15
-rw-r--r--src/pkg/strings/Makefile1
-rw-r--r--src/pkg/strings/reader.go40
-rw-r--r--src/pkg/strings/strings.go2
5 files changed, 59 insertions, 3 deletions
diff --git a/src/pkg/Make.deps b/src/pkg/Make.deps
index 9f95da540..1f3978d33 100644
--- a/src/pkg/Make.deps
+++ b/src/pkg/Make.deps
@@ -41,7 +41,7 @@ hash/adler32.install: hash.install os.install
hash/crc32.install: hash.install os.install
http.install: bufio.install bytes.install container/vector.install fmt.install io.install log.install net.install os.install path.install strconv.install strings.install utf8.install
image.install:
-image/png.install: compress/zlib.install hash.install hash/crc32.install image.install io.install os.install
+image/png.install: bufio.install compress/zlib.install hash.install hash/crc32.install image.install io.install os.install strconv.install
io.install: bytes.install os.install strings.install sync.install
json.install: bytes.install container/vector.install fmt.install math.install reflect.install strconv.install strings.install utf8.install
log.install: fmt.install io.install os.install runtime.install time.install
@@ -58,7 +58,7 @@ rpc.install: bufio.install fmt.install gob.install http.install io.install log.i
runtime.install:
sort.install:
strconv.install: bytes.install math.install os.install unicode.install utf8.install
-strings.install: unicode.install utf8.install
+strings.install: os.install unicode.install utf8.install
sync.install:
syscall.install: sync.install
tabwriter.install: bytes.install container/vector.install io.install os.install utf8.install
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.
diff --git a/src/pkg/strings/Makefile b/src/pkg/strings/Makefile
index dcfa6066c..3f0e429f6 100644
--- a/src/pkg/strings/Makefile
+++ b/src/pkg/strings/Makefile
@@ -6,6 +6,7 @@ include $(GOROOT)/src/Make.$(GOARCH)
TARG=strings
GOFILES=\
+ reader.go\
strings.go\
include $(GOROOT)/src/Make.pkg
diff --git a/src/pkg/strings/reader.go b/src/pkg/strings/reader.go
new file mode 100644
index 000000000..d742c4964
--- /dev/null
+++ b/src/pkg/strings/reader.go
@@ -0,0 +1,40 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strings
+
+import "os"
+
+// A Reader satisfies calls to Read and ReadByte by
+// reading from a string.
+type Reader string
+
+func (r *Reader) Read(b []byte) (n int, err os.Error) {
+ s := *r;
+ if len(s) == 0 {
+ return 0, os.EOF;
+ }
+ for n < len(s) && n < len(b) {
+ b[n] = s[n];
+ n++;
+ }
+ *r = s[n:len(s)];
+ return;
+}
+
+func (r *Reader) ReadByte() (b byte, err os.Error) {
+ s := *r;
+ if len(s) == 0 {
+ return 0, os.EOF;
+ }
+ b = s[0];
+ *r = s[1:len(s)];
+ return;
+}
+
+// NewReader returns a new Reader reading from s.
+// It is similar to bytes.NewBufferString but more efficient and read-only.
+func NewReader(s string) *Reader {
+ return (*Reader)(&s);
+}
diff --git a/src/pkg/strings/strings.go b/src/pkg/strings/strings.go
index bb1b8b231..f4b969b42 100644
--- a/src/pkg/strings/strings.go
+++ b/src/pkg/strings/strings.go
@@ -64,7 +64,7 @@ func Index(s, sep string) int {
return -1
}
-// Index returns the index of the last instance of sep in s, or -1 if sep is not present in s.
+// 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 string) int {
n := len(sep);
if n == 0 {