aboutsummaryrefslogtreecommitdiff
path: root/src/bytes
diff options
context:
space:
mode:
authorRick Hudson <rlh@golang.org>2016-04-27 18:19:16 -0400
committerRick Hudson <rlh@golang.org>2016-04-27 18:46:52 -0400
commit23aeb34df172b17b7bfaa85fb59ca64bef9073bb (patch)
treea8ab866f1e50f0059856ce628f036d93ab620155 /src/bytes
parent1354b32cd70f2702381764fd595dd2faa996840c (diff)
parentd3c79d324acd7300b6f705e66af8ca711af00d9f (diff)
downloadgo-23aeb34df172b17b7bfaa85fb59ca64bef9073bb.tar.xz
[dev.garbage] Merge remote-tracking branch 'origin/master' into HEAD
Change-Id: I282fd9ce9db435dfd35e882a9502ab1abc185297
Diffstat (limited to 'src/bytes')
-rw-r--r--src/bytes/bytes.go10
-rw-r--r--src/bytes/bytes_test.go51
-rw-r--r--src/bytes/reader.go5
-rw-r--r--src/bytes/reader_test.go41
4 files changed, 95 insertions, 12 deletions
diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go
index 8a4409cb6b..305c85d9f4 100644
--- a/src/bytes/bytes.go
+++ b/src/bytes/bytes.go
@@ -83,6 +83,16 @@ func Contains(b, subslice []byte) bool {
return Index(b, subslice) != -1
}
+// ContainsAny reports whether any of the UTF-8-encoded Unicode code points in chars are within b.
+func ContainsAny(b []byte, chars string) bool {
+ return IndexAny(b, chars) >= 0
+}
+
+// ContainsRune reports whether the Unicode code point r is within b.
+func ContainsRune(b []byte, r rune) bool {
+ return IndexRune(b, r) >= 0
+}
+
// Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
func Index(s, sep []byte) int {
n := len(sep)
diff --git a/src/bytes/bytes_test.go b/src/bytes/bytes_test.go
index 1be29d6cc6..620cfd1bce 100644
--- a/src/bytes/bytes_test.go
+++ b/src/bytes/bytes_test.go
@@ -1218,6 +1218,57 @@ func TestContains(t *testing.T) {
}
}
+var ContainsAnyTests = []struct {
+ b []byte
+ substr string
+ expected bool
+}{
+ {[]byte(""), "", false},
+ {[]byte(""), "a", false},
+ {[]byte(""), "abc", false},
+ {[]byte("a"), "", false},
+ {[]byte("a"), "a", true},
+ {[]byte("aaa"), "a", true},
+ {[]byte("abc"), "xyz", false},
+ {[]byte("abc"), "xcz", true},
+ {[]byte("a☺b☻c☹d"), "uvw☻xyz", true},
+ {[]byte("aRegExp*"), ".(|)*+?^$[]", true},
+ {[]byte(dots + dots + dots), " ", false},
+}
+
+func TestContainsAny(t *testing.T) {
+ for _, ct := range ContainsAnyTests {
+ if ContainsAny(ct.b, ct.substr) != ct.expected {
+ t.Errorf("ContainsAny(%s, %s) = %v, want %v",
+ ct.b, ct.substr, !ct.expected, ct.expected)
+ }
+ }
+}
+
+var ContainsRuneTests = []struct {
+ b []byte
+ r rune
+ expected bool
+}{
+ {[]byte(""), 'a', false},
+ {[]byte("a"), 'a', true},
+ {[]byte("aaa"), 'a', true},
+ {[]byte("abc"), 'y', false},
+ {[]byte("abc"), 'c', true},
+ {[]byte("a☺b☻c☹d"), 'x', false},
+ {[]byte("a☺b☻c☹d"), '☻', true},
+ {[]byte("aRegExp*"), '*', true},
+}
+
+func TestContainsRune(t *testing.T) {
+ for _, ct := range ContainsRuneTests {
+ if ContainsRune(ct.b, ct.r) != ct.expected {
+ t.Errorf("ContainsRune(%q, %q) = %v, want %v",
+ ct.b, ct.r, !ct.expected, ct.expected)
+ }
+ }
+}
+
var makeFieldsInput = func() []byte {
x := make([]byte, 1<<20)
// Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
diff --git a/src/bytes/reader.go b/src/bytes/reader.go
index 5941ebdab4..aa39890f3b 100644
--- a/src/bytes/reader.go
+++ b/src/bytes/reader.go
@@ -114,7 +114,7 @@ func (r *Reader) Seek(offset int64, whence int) (int64, error) {
case 0:
abs = offset
case 1:
- abs = int64(r.i) + offset
+ abs = r.i + offset
case 2:
abs = int64(len(r.s)) + offset
default:
@@ -146,5 +146,8 @@ func (r *Reader) WriteTo(w io.Writer) (n int64, err error) {
return
}
+// Reset resets the Reader to be reading from b.
+func (r *Reader) Reset(b []byte) { *r = Reader{b, 0, -1} }
+
// NewReader returns a new Reader reading from b.
func NewReader(b []byte) *Reader { return &Reader{b, 0, -1} }
diff --git a/src/bytes/reader_test.go b/src/bytes/reader_test.go
index b929a28260..9341cd5b45 100644
--- a/src/bytes/reader_test.go
+++ b/src/bytes/reader_test.go
@@ -9,7 +9,6 @@ import (
"fmt"
"io"
"io/ioutil"
- "os"
"sync"
"testing"
)
@@ -24,15 +23,15 @@ func TestReader(t *testing.T) {
wantpos int64
seekerr string
}{
- {seek: os.SEEK_SET, off: 0, n: 20, want: "0123456789"},
- {seek: os.SEEK_SET, off: 1, n: 1, want: "1"},
- {seek: os.SEEK_CUR, off: 1, wantpos: 3, n: 2, want: "34"},
- {seek: os.SEEK_SET, off: -1, seekerr: "bytes.Reader.Seek: negative position"},
- {seek: os.SEEK_SET, off: 1 << 33, wantpos: 1 << 33},
- {seek: os.SEEK_CUR, off: 1, wantpos: 1<<33 + 1},
- {seek: os.SEEK_SET, n: 5, want: "01234"},
- {seek: os.SEEK_CUR, n: 5, want: "56789"},
- {seek: os.SEEK_END, off: -1, n: 1, wantpos: 9, want: "9"},
+ {seek: io.SeekStart, off: 0, n: 20, want: "0123456789"},
+ {seek: io.SeekStart, off: 1, n: 1, want: "1"},
+ {seek: io.SeekCurrent, off: 1, wantpos: 3, n: 2, want: "34"},
+ {seek: io.SeekStart, off: -1, seekerr: "bytes.Reader.Seek: negative position"},
+ {seek: io.SeekStart, off: 1 << 33, wantpos: 1 << 33},
+ {seek: io.SeekCurrent, off: 1, wantpos: 1<<33 + 1},
+ {seek: io.SeekStart, n: 5, want: "01234"},
+ {seek: io.SeekCurrent, n: 5, want: "56789"},
+ {seek: io.SeekEnd, off: -1, n: 1, wantpos: 9, want: "9"},
}
for i, tt := range tests {
@@ -63,7 +62,7 @@ func TestReader(t *testing.T) {
func TestReadAfterBigSeek(t *testing.T) {
r := NewReader([]byte("0123456789"))
- if _, err := r.Seek(1<<31+5, os.SEEK_SET); err != nil {
+ if _, err := r.Seek(1<<31+5, io.SeekStart); err != nil {
t.Fatal(err)
}
if n, err := r.Read(make([]byte, 10)); n != 0 || err != io.EOF {
@@ -256,3 +255,23 @@ func TestReaderLenSize(t *testing.T) {
t.Errorf("Size = %d; want 3", r.Size())
}
}
+
+func TestReaderReset(t *testing.T) {
+ r := NewReader([]byte("世界"))
+ if _, _, err := r.ReadRune(); err != nil {
+ t.Errorf("ReadRune: unexpected error: %v", err)
+ }
+
+ const want = "abcdef"
+ r.Reset([]byte(want))
+ if err := r.UnreadRune(); err == nil {
+ t.Errorf("UnreadRune: expected error, got nil")
+ }
+ buf, err := ioutil.ReadAll(r)
+ if err != nil {
+ t.Errorf("ReadAll: unexpected error: %v", err)
+ }
+ if got := string(buf); got != want {
+ t.Errorf("ReadAll: got %q, want %q", got, want)
+ }
+}