...
Run Format

Source file src/bytes/reader.go

     1	// Copyright 2012 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	package bytes
     6	
     7	import (
     8		"errors"
     9		"io"
    10		"unicode/utf8"
    11	)
    12	
    13	// A Reader implements the io.Reader, io.ReaderAt, io.WriterTo, io.Seeker,
    14	// io.ByteScanner, and io.RuneScanner interfaces by reading from
    15	// a byte slice.
    16	// Unlike a Buffer, a Reader is read-only and supports seeking.
    17	type Reader struct {
    18		s        []byte
    19		i        int64 // current reading index
    20		prevRune int   // index of previous rune; or < 0
    21	}
    22	
    23	// Len returns the number of bytes of the unread portion of the
    24	// slice.
    25	func (r *Reader) Len() int {
    26		if r.i >= int64(len(r.s)) {
    27			return 0
    28		}
    29		return int(int64(len(r.s)) - r.i)
    30	}
    31	
    32	func (r *Reader) Read(b []byte) (n int, err error) {
    33		if len(b) == 0 {
    34			return 0, nil
    35		}
    36		if r.i >= int64(len(r.s)) {
    37			return 0, io.EOF
    38		}
    39		r.prevRune = -1
    40		n = copy(b, r.s[r.i:])
    41		r.i += int64(n)
    42		return
    43	}
    44	
    45	func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {
    46		// cannot modify state - see io.ReaderAt
    47		if off < 0 {
    48			return 0, errors.New("bytes.Reader.ReadAt: negative offset")
    49		}
    50		if off >= int64(len(r.s)) {
    51			return 0, io.EOF
    52		}
    53		n = copy(b, r.s[off:])
    54		if n < len(b) {
    55			err = io.EOF
    56		}
    57		return
    58	}
    59	
    60	func (r *Reader) ReadByte() (b byte, err error) {
    61		r.prevRune = -1
    62		if r.i >= int64(len(r.s)) {
    63			return 0, io.EOF
    64		}
    65		b = r.s[r.i]
    66		r.i++
    67		return
    68	}
    69	
    70	func (r *Reader) UnreadByte() error {
    71		r.prevRune = -1
    72		if r.i <= 0 {
    73			return errors.New("bytes.Reader.UnreadByte: at beginning of slice")
    74		}
    75		r.i--
    76		return nil
    77	}
    78	
    79	func (r *Reader) ReadRune() (ch rune, size int, err error) {
    80		if r.i >= int64(len(r.s)) {
    81			r.prevRune = -1
    82			return 0, 0, io.EOF
    83		}
    84		r.prevRune = int(r.i)
    85		if c := r.s[r.i]; c < utf8.RuneSelf {
    86			r.i++
    87			return rune(c), 1, nil
    88		}
    89		ch, size = utf8.DecodeRune(r.s[r.i:])
    90		r.i += int64(size)
    91		return
    92	}
    93	
    94	func (r *Reader) UnreadRune() error {
    95		if r.prevRune < 0 {
    96			return errors.New("bytes.Reader.UnreadRune: previous operation was not ReadRune")
    97		}
    98		r.i = int64(r.prevRune)
    99		r.prevRune = -1
   100		return nil
   101	}
   102	
   103	// Seek implements the io.Seeker interface.
   104	func (r *Reader) Seek(offset int64, whence int) (int64, error) {
   105		r.prevRune = -1
   106		var abs int64
   107		switch whence {
   108		case 0:
   109			abs = offset
   110		case 1:
   111			abs = int64(r.i) + offset
   112		case 2:
   113			abs = int64(len(r.s)) + offset
   114		default:
   115			return 0, errors.New("bytes.Reader.Seek: invalid whence")
   116		}
   117		if abs < 0 {
   118			return 0, errors.New("bytes.Reader.Seek: negative position")
   119		}
   120		r.i = abs
   121		return abs, nil
   122	}
   123	
   124	// WriteTo implements the io.WriterTo interface.
   125	func (r *Reader) WriteTo(w io.Writer) (n int64, err error) {
   126		r.prevRune = -1
   127		if r.i >= int64(len(r.s)) {
   128			return 0, nil
   129		}
   130		b := r.s[r.i:]
   131		m, err := w.Write(b)
   132		if m > len(b) {
   133			panic("bytes.Reader.WriteTo: invalid Write count")
   134		}
   135		r.i += int64(m)
   136		n = int64(m)
   137		if m != len(b) && err == nil {
   138			err = io.ErrShortWrite
   139		}
   140		return
   141	}
   142	
   143	// NewReader returns a new Reader reading from b.
   144	func NewReader(b []byte) *Reader { return &Reader{b, 0, -1} }
   145	

View as plain text