1
0
mirror of https://github.com/golang/go synced 2024-11-25 13:27:57 -07:00

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
This commit is contained in:
Russ Cox 2009-10-12 10:09:35 -07:00
parent d0aac0ace1
commit 10c7d19e07
5 changed files with 59 additions and 3 deletions

View File

@ -41,7 +41,7 @@ hash/adler32.install: hash.install os.install
hash/crc32.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 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.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 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 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 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: runtime.install:
sort.install: sort.install:
strconv.install: bytes.install math.install os.install unicode.install utf8.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: sync.install:
syscall.install: sync.install syscall.install: sync.install
tabwriter.install: bytes.install container/vector.install io.install os.install utf8.install tabwriter.install: bytes.install container/vector.install io.install os.install utf8.install

View File

@ -112,6 +112,21 @@ func Index(s, sep []byte) int {
return -1; 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. // 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 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. // If n > 0, split Splits s into at most n subarrays; the last subarray will contain an unsplit remainder.

View File

@ -6,6 +6,7 @@ include $(GOROOT)/src/Make.$(GOARCH)
TARG=strings TARG=strings
GOFILES=\ GOFILES=\
reader.go\
strings.go\ strings.go\
include $(GOROOT)/src/Make.pkg include $(GOROOT)/src/Make.pkg

40
src/pkg/strings/reader.go Normal file
View File

@ -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);
}

View File

@ -64,7 +64,7 @@ func Index(s, sep string) int {
return -1 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 { func LastIndex(s, sep string) int {
n := len(sep); n := len(sep);
if n == 0 { if n == 0 {