diff --git a/src/pkg/Make.deps b/src/pkg/Make.deps index 9f95da54090..1f3978d33af 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 103a896740a..564c42d4a89 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 dcfa6066cda..3f0e429f6a7 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 00000000000..d742c49641d --- /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 bb1b8b23118..f4b969b42b9 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 {