bytes, strings: allow Reader.Seek past 1<<31
Fixes #7654
LGTM=rsc
R=rsc, dan.kortschak
CC=golang-codereviews
https://golang.org/cl/81530043
diff --git a/src/pkg/strings/reader.go b/src/pkg/strings/reader.go
index 75bf07b..93ff804 100644
--- a/src/pkg/strings/reader.go
+++ b/src/pkg/strings/reader.go
@@ -15,17 +15,17 @@
// from a string.
type Reader struct {
s string
- i int // current reading index
- prevRune int // index of previous rune; or < 0
+ i int64 // current reading index
+ prevRune int // index of previous rune; or < 0
}
// Len returns the number of bytes of the unread portion of the
// string.
func (r *Reader) Len() int {
- if r.i >= len(r.s) {
+ if r.i >= int64(len(r.s)) {
return 0
}
- return len(r.s) - r.i
+ return int(int64(len(r.s)) - r.i)
}
func (r *Reader) Read(b []byte) (n int, err error) {
@@ -33,11 +33,11 @@
if len(b) == 0 {
return 0, nil
}
- if r.i >= len(r.s) {
+ if r.i >= int64(len(r.s)) {
return 0, io.EOF
}
n = copy(b, r.s[r.i:])
- r.i += n
+ r.i += int64(n)
return
}
@@ -48,7 +48,7 @@
if off >= int64(len(r.s)) {
return 0, io.EOF
}
- n = copy(b, r.s[int(off):])
+ n = copy(b, r.s[off:])
if n < len(b) {
err = io.EOF
}
@@ -57,7 +57,7 @@
func (r *Reader) ReadByte() (b byte, err error) {
r.prevRune = -1
- if r.i >= len(r.s) {
+ if r.i >= int64(len(r.s)) {
return 0, io.EOF
}
b = r.s[r.i]
@@ -75,17 +75,17 @@
}
func (r *Reader) ReadRune() (ch rune, size int, err error) {
- if r.i >= len(r.s) {
+ if r.i >= int64(len(r.s)) {
r.prevRune = -1
return 0, 0, io.EOF
}
- r.prevRune = r.i
+ r.prevRune = int(r.i)
if c := r.s[r.i]; c < utf8.RuneSelf {
r.i++
return rune(c), 1, nil
}
ch, size = utf8.DecodeRuneInString(r.s[r.i:])
- r.i += size
+ r.i += int64(size)
return
}
@@ -93,7 +93,7 @@
if r.prevRune < 0 {
return errors.New("strings.Reader: previous operation was not ReadRune")
}
- r.i = r.prevRune
+ r.i = int64(r.prevRune)
r.prevRune = -1
return nil
}
@@ -115,17 +115,14 @@
if abs < 0 {
return 0, errors.New("strings: negative position")
}
- if abs >= 1<<31 {
- return 0, errors.New("strings: position out of range")
- }
- r.i = int(abs)
+ r.i = abs
return abs, nil
}
// WriteTo implements the io.WriterTo interface.
func (r *Reader) WriteTo(w io.Writer) (n int64, err error) {
r.prevRune = -1
- if r.i >= len(r.s) {
+ if r.i >= int64(len(r.s)) {
return 0, nil
}
s := r.s[r.i:]
@@ -133,7 +130,7 @@
if m > len(s) {
panic("strings.Reader.WriteTo: invalid WriteString count")
}
- r.i += m
+ r.i += int64(m)
n = int64(m)
if m != len(s) && err == nil {
err = io.ErrShortWrite