mirror of
https://github.com/golang/go
synced 2024-11-13 15:20:22 -07:00
bytes, strings: more consistent error messages
LGTM=bradfitz R=bradfitz CC=golang-codereviews https://golang.org/cl/86060044
This commit is contained in:
parent
62bda120ef
commit
08d8eca968
@ -45,7 +45,7 @@ func (r *Reader) Read(b []byte) (n int, err error) {
|
|||||||
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {
|
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {
|
||||||
// cannot modify state - see io.ReaderAt
|
// cannot modify state - see io.ReaderAt
|
||||||
if off < 0 {
|
if off < 0 {
|
||||||
return 0, errors.New("bytes: invalid offset")
|
return 0, errors.New("bytes.Reader.ReadAt: negative offset")
|
||||||
}
|
}
|
||||||
if off >= int64(len(r.s)) {
|
if off >= int64(len(r.s)) {
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
@ -68,11 +68,11 @@ func (r *Reader) ReadByte() (b byte, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) UnreadByte() error {
|
func (r *Reader) UnreadByte() error {
|
||||||
|
r.prevRune = -1
|
||||||
if r.i <= 0 {
|
if r.i <= 0 {
|
||||||
return errors.New("bytes.Reader: at beginning of slice")
|
return errors.New("bytes.Reader.UnreadByte: at beginning of slice")
|
||||||
}
|
}
|
||||||
r.i--
|
r.i--
|
||||||
r.prevRune = -1
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,7 +93,7 @@ func (r *Reader) ReadRune() (ch rune, size int, err error) {
|
|||||||
|
|
||||||
func (r *Reader) UnreadRune() error {
|
func (r *Reader) UnreadRune() error {
|
||||||
if r.prevRune < 0 {
|
if r.prevRune < 0 {
|
||||||
return errors.New("bytes.Reader: previous operation was not ReadRune")
|
return errors.New("bytes.Reader.UnreadRune: previous operation was not ReadRune")
|
||||||
}
|
}
|
||||||
r.i = int64(r.prevRune)
|
r.i = int64(r.prevRune)
|
||||||
r.prevRune = -1
|
r.prevRune = -1
|
||||||
@ -112,10 +112,10 @@ func (r *Reader) Seek(offset int64, whence int) (int64, error) {
|
|||||||
case 2:
|
case 2:
|
||||||
abs = int64(len(r.s)) + offset
|
abs = int64(len(r.s)) + offset
|
||||||
default:
|
default:
|
||||||
return 0, errors.New("bytes: invalid whence")
|
return 0, errors.New("bytes.Reader.Seek: invalid whence")
|
||||||
}
|
}
|
||||||
if abs < 0 {
|
if abs < 0 {
|
||||||
return 0, errors.New("bytes: negative position")
|
return 0, errors.New("bytes.Reader.Seek: negative position")
|
||||||
}
|
}
|
||||||
r.i = abs
|
r.i = abs
|
||||||
return abs, nil
|
return abs, nil
|
||||||
|
@ -27,7 +27,7 @@ func TestReader(t *testing.T) {
|
|||||||
{seek: os.SEEK_SET, off: 0, n: 20, want: "0123456789"},
|
{seek: os.SEEK_SET, off: 0, n: 20, want: "0123456789"},
|
||||||
{seek: os.SEEK_SET, off: 1, n: 1, want: "1"},
|
{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_CUR, off: 1, wantpos: 3, n: 2, want: "34"},
|
||||||
{seek: os.SEEK_SET, off: -1, seekerr: "bytes: negative position"},
|
{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_SET, off: 1 << 33, wantpos: 1 << 33},
|
||||||
{seek: os.SEEK_CUR, off: 1, wantpos: 1<<33 + 1},
|
{seek: os.SEEK_CUR, off: 1, wantpos: 1<<33 + 1},
|
||||||
{seek: os.SEEK_SET, n: 5, want: "01234"},
|
{seek: os.SEEK_SET, n: 5, want: "01234"},
|
||||||
@ -84,7 +84,7 @@ func TestReaderAt(t *testing.T) {
|
|||||||
{1, 9, "123456789", nil},
|
{1, 9, "123456789", nil},
|
||||||
{11, 10, "", io.EOF},
|
{11, 10, "", io.EOF},
|
||||||
{0, 0, "", nil},
|
{0, 0, "", nil},
|
||||||
{-1, 0, "", "bytes: invalid offset"},
|
{-1, 0, "", "bytes.Reader.ReadAt: negative offset"},
|
||||||
}
|
}
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
b := make([]byte, tt.n)
|
b := make([]byte, tt.n)
|
||||||
|
@ -44,7 +44,7 @@ func (r *Reader) Read(b []byte) (n int, err error) {
|
|||||||
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {
|
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {
|
||||||
// cannot modify state - see io.ReaderAt
|
// cannot modify state - see io.ReaderAt
|
||||||
if off < 0 {
|
if off < 0 {
|
||||||
return 0, errors.New("strings: invalid offset")
|
return 0, errors.New("strings.Reader.ReadAt: negative offset")
|
||||||
}
|
}
|
||||||
if off >= int64(len(r.s)) {
|
if off >= int64(len(r.s)) {
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
@ -67,11 +67,11 @@ func (r *Reader) ReadByte() (b byte, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) UnreadByte() error {
|
func (r *Reader) UnreadByte() error {
|
||||||
|
r.prevRune = -1
|
||||||
if r.i <= 0 {
|
if r.i <= 0 {
|
||||||
return errors.New("strings.Reader: at beginning of string")
|
return errors.New("strings.Reader.UnreadByte: at beginning of string")
|
||||||
}
|
}
|
||||||
r.i--
|
r.i--
|
||||||
r.prevRune = -1
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,7 +92,7 @@ func (r *Reader) ReadRune() (ch rune, size int, err error) {
|
|||||||
|
|
||||||
func (r *Reader) UnreadRune() error {
|
func (r *Reader) UnreadRune() error {
|
||||||
if r.prevRune < 0 {
|
if r.prevRune < 0 {
|
||||||
return errors.New("strings.Reader: previous operation was not ReadRune")
|
return errors.New("strings.Reader.UnreadRune: previous operation was not ReadRune")
|
||||||
}
|
}
|
||||||
r.i = int64(r.prevRune)
|
r.i = int64(r.prevRune)
|
||||||
r.prevRune = -1
|
r.prevRune = -1
|
||||||
@ -111,10 +111,10 @@ func (r *Reader) Seek(offset int64, whence int) (int64, error) {
|
|||||||
case 2:
|
case 2:
|
||||||
abs = int64(len(r.s)) + offset
|
abs = int64(len(r.s)) + offset
|
||||||
default:
|
default:
|
||||||
return 0, errors.New("strings: invalid whence")
|
return 0, errors.New("strings.Reader.Seek: invalid whence")
|
||||||
}
|
}
|
||||||
if abs < 0 {
|
if abs < 0 {
|
||||||
return 0, errors.New("strings: negative position")
|
return 0, errors.New("strings.Reader.Seek: negative position")
|
||||||
}
|
}
|
||||||
r.i = abs
|
r.i = abs
|
||||||
return abs, nil
|
return abs, nil
|
||||||
|
@ -27,7 +27,7 @@ func TestReader(t *testing.T) {
|
|||||||
{seek: os.SEEK_SET, off: 0, n: 20, want: "0123456789"},
|
{seek: os.SEEK_SET, off: 0, n: 20, want: "0123456789"},
|
||||||
{seek: os.SEEK_SET, off: 1, n: 1, want: "1"},
|
{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_CUR, off: 1, wantpos: 3, n: 2, want: "34"},
|
||||||
{seek: os.SEEK_SET, off: -1, seekerr: "strings: negative position"},
|
{seek: os.SEEK_SET, off: -1, seekerr: "strings.Reader.Seek: negative position"},
|
||||||
{seek: os.SEEK_SET, off: 1 << 33, wantpos: 1 << 33},
|
{seek: os.SEEK_SET, off: 1 << 33, wantpos: 1 << 33},
|
||||||
{seek: os.SEEK_CUR, off: 1, wantpos: 1<<33 + 1},
|
{seek: os.SEEK_CUR, off: 1, wantpos: 1<<33 + 1},
|
||||||
{seek: os.SEEK_SET, n: 5, want: "01234"},
|
{seek: os.SEEK_SET, n: 5, want: "01234"},
|
||||||
@ -84,7 +84,7 @@ func TestReaderAt(t *testing.T) {
|
|||||||
{1, 9, "123456789", nil},
|
{1, 9, "123456789", nil},
|
||||||
{11, 10, "", io.EOF},
|
{11, 10, "", io.EOF},
|
||||||
{0, 0, "", nil},
|
{0, 0, "", nil},
|
||||||
{-1, 0, "", "strings: invalid offset"},
|
{-1, 0, "", "strings.Reader.ReadAt: negative offset"},
|
||||||
}
|
}
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
b := make([]byte, tt.n)
|
b := make([]byte, tt.n)
|
||||||
|
Loading…
Reference in New Issue
Block a user