]> Cypherpunks repositories - gostls13.git/commitdiff
fix comment on strings.LastIndex.
authorRuss Cox <rsc@golang.org>
Mon, 12 Oct 2009 17:09:35 +0000 (10:09 -0700)
committerRuss Cox <rsc@golang.org>
Mon, 12 Oct 2009 17:09:35 +0000 (10:09 -0700)
add bytes.LastIndex.
add strings.Reader.

R=r
DELTA=59  (56 added, 0 deleted, 3 changed)
OCL=35585
CL=35601

src/pkg/Make.deps
src/pkg/bytes/bytes.go
src/pkg/strings/Makefile
src/pkg/strings/reader.go [new file with mode: 0644]
src/pkg/strings/strings.go

index 9f95da540908c0cb29676a9b8620ba8434a0bdc1..1f3978d33af3febacdae4c33b6993681d43908d6 100644 (file)
@@ -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
index 103a896740ab66b427995d85db893c8685f83992..564c42d4a893eee4e1744f444929487360f56e96 100644 (file)
@@ -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.
index dcfa6066cdae8660626ce9aa114a9a64cc3efed3..3f0e429f6a765715a5e63d51d7ed07882ef37ba2 100644 (file)
@@ -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 (file)
index 0000000..d742c49
--- /dev/null
@@ -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);
+}
index bb1b8b2311868fb5cb81b75ab99f95de985a1a4e..f4b969b42b925ab773b08a1804d3f260041bf224 100644 (file)
@@ -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 {