123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728 |
- package bufio
- import (
- "bytes"
- "errors"
- "io"
- "unicode/utf8"
- )
- const (
- defaultBufSize = 4096
- )
- var (
- ErrInvalidUnreadByte = errors.New("bufio: invalid use of UnreadByte")
- ErrInvalidUnreadRune = errors.New("bufio: invalid use of UnreadRune")
- ErrBufferFull = errors.New("bufio: buffer full")
- ErrNegativeCount = errors.New("bufio: negative count")
- )
- type Reader struct {
- buf []byte
- rd io.Reader
- r, w int
- err error
- lastByte int
- lastRuneSize int
- }
- const minReadBufferSize = 16
- const maxConsecutiveEmptyReads = 100
- func NewReaderSize(rd io.Reader, size int) *Reader {
-
- b, ok := rd.(*Reader)
- if ok && len(b.buf) >= size {
- return b
- }
- if size < minReadBufferSize {
- size = minReadBufferSize
- }
- r := new(Reader)
- r.reset(make([]byte, size), rd)
- return r
- }
- func NewReader(rd io.Reader) *Reader {
- return NewReaderSize(rd, defaultBufSize)
- }
- func (b *Reader) Reset(r io.Reader) {
- b.reset(b.buf, r)
- }
- func (b *Reader) reset(buf []byte, r io.Reader) {
- *b = Reader{
- buf: buf,
- rd: r,
- lastByte: -1,
- lastRuneSize: -1,
- }
- }
- var errNegativeRead = errors.New("bufio: reader returned negative count from Read")
- func (b *Reader) fill() {
-
- if b.r > 0 {
- copy(b.buf, b.buf[b.r:b.w])
- b.w -= b.r
- b.r = 0
- }
- if b.w >= len(b.buf) {
- panic("bufio: tried to fill full buffer")
- }
-
- for i := maxConsecutiveEmptyReads; i > 0; i-- {
- n, err := b.rd.Read(b.buf[b.w:])
- if n < 0 {
- panic(errNegativeRead)
- }
- b.w += n
- if err != nil {
- b.err = err
- return
- }
- if n > 0 {
- return
- }
- }
- b.err = io.ErrNoProgress
- }
- func (b *Reader) readErr() error {
- err := b.err
- b.err = nil
- return err
- }
- func (b *Reader) Peek(n int) ([]byte, error) {
- if n < 0 {
- return nil, ErrNegativeCount
- }
- if n > len(b.buf) {
- return nil, ErrBufferFull
- }
-
- for b.w-b.r < n && b.err == nil {
- b.fill()
- }
- m := b.w - b.r
- if m > n {
- m = n
- }
- var err error
- if m < n {
- err = b.readErr()
- if err == nil {
- err = ErrBufferFull
- }
- }
- return b.buf[b.r : b.r+m], err
- }
- func (b *Reader) Read(p []byte) (n int, err error) {
- n = len(p)
- if n == 0 {
- return 0, b.readErr()
- }
- if b.r == b.w {
- if b.err != nil {
- return 0, b.readErr()
- }
- if len(p) >= len(b.buf) {
-
-
- n, b.err = b.rd.Read(p)
- if n < 0 {
- panic(errNegativeRead)
- }
- if n > 0 {
- b.lastByte = int(p[n-1])
- b.lastRuneSize = -1
- }
- return n, b.readErr()
- }
- b.fill()
- if b.w == b.r {
- return 0, b.readErr()
- }
- }
- if n > b.w-b.r {
- n = b.w - b.r
- }
- copy(p[0:n], b.buf[b.r:])
- b.r += n
- b.lastByte = int(b.buf[b.r-1])
- b.lastRuneSize = -1
- return n, nil
- }
- func (b *Reader) ReadByte() (c byte, err error) {
- b.lastRuneSize = -1
- for b.r == b.w {
- if b.err != nil {
- return 0, b.readErr()
- }
- b.fill()
- }
- c = b.buf[b.r]
- b.r++
- b.lastByte = int(c)
- return c, nil
- }
- func (b *Reader) UnreadByte() error {
- if b.lastByte < 0 || b.r == 0 && b.w > 0 {
- return ErrInvalidUnreadByte
- }
-
- if b.r > 0 {
- b.r--
- } else {
-
- b.w = 1
- }
- b.buf[b.r] = byte(b.lastByte)
- b.lastByte = -1
- b.lastRuneSize = -1
- return nil
- }
- func (b *Reader) ReadRune() (r rune, size int, err error) {
- for b.r+utf8.UTFMax > b.w && !utf8.FullRune(b.buf[b.r:b.w]) && b.err == nil && b.w-b.r < len(b.buf) {
- b.fill()
- }
- b.lastRuneSize = -1
- if b.r == b.w {
- return 0, 0, b.readErr()
- }
- r, size = rune(b.buf[b.r]), 1
- if r >= 0x80 {
- r, size = utf8.DecodeRune(b.buf[b.r:b.w])
- }
- b.r += size
- b.lastByte = int(b.buf[b.r-1])
- b.lastRuneSize = size
- return r, size, nil
- }
- func (b *Reader) UnreadRune() error {
- if b.lastRuneSize < 0 || b.r < b.lastRuneSize {
- return ErrInvalidUnreadRune
- }
- b.r -= b.lastRuneSize
- b.lastByte = -1
- b.lastRuneSize = -1
- return nil
- }
- func (b *Reader) Buffered() int { return b.w - b.r }
- func (b *Reader) ReadSlice(delim byte) (line []byte, err error) {
- for {
-
- if i := bytes.IndexByte(b.buf[b.r:b.w], delim); i >= 0 {
- line = b.buf[b.r : b.r+i+1]
- b.r += i + 1
- break
- }
-
- if b.err != nil {
- line = b.buf[b.r:b.w]
- b.r = b.w
- err = b.readErr()
- break
- }
-
- if n := b.Buffered(); n >= len(b.buf) {
- b.r = b.w
- line = b.buf
- err = ErrBufferFull
- break
- }
- b.fill()
- }
-
- if i := len(line) - 1; i >= 0 {
- b.lastByte = int(line[i])
- }
- return
- }
- func (b *Reader) ReadN(n int) ([]byte, error) {
- for b.Buffered() < n {
- if b.err != nil {
- buf := b.buf[b.r:b.w]
- b.r = b.w
- return buf, b.readErr()
- }
-
- if b.Buffered() >= len(b.buf) {
- b.r = b.w
- return b.buf, ErrBufferFull
- }
- b.fill()
- }
- buf := b.buf[b.r : b.r+n]
- b.r += n
- return buf, nil
- }
- func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error) {
- line, err = b.ReadSlice('\n')
- if err == ErrBufferFull {
-
- if len(line) > 0 && line[len(line)-1] == '\r' {
-
-
- if b.r == 0 {
-
- panic("bufio: tried to rewind past start of buffer")
- }
- b.r--
- line = line[:len(line)-1]
- }
- return line, true, nil
- }
- if len(line) == 0 {
- if err != nil {
- line = nil
- }
- return
- }
- err = nil
- if line[len(line)-1] == '\n' {
- drop := 1
- if len(line) > 1 && line[len(line)-2] == '\r' {
- drop = 2
- }
- line = line[:len(line)-drop]
- }
- return
- }
- func (b *Reader) ReadBytes(delim byte) (line []byte, err error) {
-
-
- var frag []byte
- var full [][]byte
- err = nil
- for {
- var e error
- frag, e = b.ReadSlice(delim)
- if e == nil {
- break
- }
- if e != ErrBufferFull {
- err = e
- break
- }
-
- buf := make([]byte, len(frag))
- copy(buf, frag)
- full = append(full, buf)
- }
-
- n := 0
- for i := range full {
- n += len(full[i])
- }
- n += len(frag)
-
- buf := make([]byte, n)
- n = 0
- for i := range full {
- n += copy(buf[n:], full[i])
- }
- copy(buf[n:], frag)
- return buf, err
- }
- func (b *Reader) ReadString(delim byte) (line string, err error) {
- bytes, err := b.ReadBytes(delim)
- line = string(bytes)
- return line, err
- }
- func (b *Reader) WriteTo(w io.Writer) (n int64, err error) {
- n, err = b.writeBuf(w)
- if err != nil {
- return
- }
- if r, ok := b.rd.(io.WriterTo); ok {
- m, err := r.WriteTo(w)
- n += m
- return n, err
- }
- if w, ok := w.(io.ReaderFrom); ok {
- m, err := w.ReadFrom(b.rd)
- n += m
- return n, err
- }
- if b.w-b.r < len(b.buf) {
- b.fill()
- }
- for b.r < b.w {
-
- m, err := b.writeBuf(w)
- n += m
- if err != nil {
- return n, err
- }
- b.fill()
- }
- if b.err == io.EOF {
- b.err = nil
- }
- return n, b.readErr()
- }
- func (b *Reader) writeBuf(w io.Writer) (int64, error) {
- n, err := w.Write(b.buf[b.r:b.w])
- if n < b.r-b.w {
- panic(errors.New("bufio: writer did not write all data"))
- }
- b.r += n
- return int64(n), err
- }
- type Writer struct {
- err error
- buf []byte
- n int
- wr io.Writer
- }
- func NewWriterSize(w io.Writer, size int) *Writer {
-
- b, ok := w.(*Writer)
- if ok && len(b.buf) >= size {
- return b
- }
- if size <= 0 {
- size = defaultBufSize
- }
- return &Writer{
- buf: make([]byte, size),
- wr: w,
- }
- }
- func NewWriter(w io.Writer) *Writer {
- return NewWriterSize(w, defaultBufSize)
- }
- func (b *Writer) Reset(w io.Writer) {
- b.err = nil
- b.n = 0
- b.wr = w
- }
- func (b *Writer) Flush() error {
- err := b.flush()
- return err
- }
- func (b *Writer) flush() error {
- if b.err != nil {
- return b.err
- }
- if b.n == 0 {
- return nil
- }
- n, err := b.wr.Write(b.buf[0:b.n])
- if n < b.n && err == nil {
- err = io.ErrShortWrite
- }
- if err != nil {
- if n > 0 && n < b.n {
- copy(b.buf[0:b.n-n], b.buf[n:b.n])
- }
- b.n -= n
- b.err = err
- return err
- }
- b.n = 0
- return nil
- }
- func (b *Writer) Available() int { return len(b.buf) - b.n }
- func (b *Writer) Buffered() int { return b.n }
- func (b *Writer) Write(p []byte) (nn int, err error) {
- for len(p) > b.Available() && b.err == nil {
- var n int
- if b.Buffered() == 0 {
-
-
- n, b.err = b.wr.Write(p)
- } else {
- n = copy(b.buf[b.n:], p)
- b.n += n
- b.flush()
- }
- nn += n
- p = p[n:]
- }
- if b.err != nil {
- return nn, b.err
- }
- n := copy(b.buf[b.n:], p)
- b.n += n
- nn += n
- return nn, nil
- }
- func (b *Writer) WriteByte(c byte) error {
- if b.err != nil {
- return b.err
- }
- if b.Available() <= 0 && b.flush() != nil {
- return b.err
- }
- b.buf[b.n] = c
- b.n++
- return nil
- }
- func (b *Writer) WriteRune(r rune) (size int, err error) {
- if r < utf8.RuneSelf {
- err = b.WriteByte(byte(r))
- if err != nil {
- return 0, err
- }
- return 1, nil
- }
- if b.err != nil {
- return 0, b.err
- }
- n := b.Available()
- if n < utf8.UTFMax {
- if b.flush(); b.err != nil {
- return 0, b.err
- }
- n = b.Available()
- if n < utf8.UTFMax {
-
- return b.WriteString(string(r))
- }
- }
- size = utf8.EncodeRune(b.buf[b.n:], r)
- b.n += size
- return size, nil
- }
- func (b *Writer) WriteString(s string) (int, error) {
- nn := 0
- for len(s) > b.Available() && b.err == nil {
- n := copy(b.buf[b.n:], s)
- b.n += n
- nn += n
- s = s[n:]
- b.flush()
- }
- if b.err != nil {
- return nn, b.err
- }
- n := copy(b.buf[b.n:], s)
- b.n += n
- nn += n
- return nn, nil
- }
- func (b *Writer) ReadFrom(r io.Reader) (n int64, err error) {
- if b.Buffered() == 0 {
- if w, ok := b.wr.(io.ReaderFrom); ok {
- return w.ReadFrom(r)
- }
- }
- var m int
- for {
- if b.Available() == 0 {
- if err1 := b.flush(); err1 != nil {
- return n, err1
- }
- }
- nr := 0
- for nr < maxConsecutiveEmptyReads {
- m, err = r.Read(b.buf[b.n:])
- if m != 0 || err != nil {
- break
- }
- nr++
- }
- if nr == maxConsecutiveEmptyReads {
- return n, io.ErrNoProgress
- }
- b.n += m
- n += int64(m)
- if err != nil {
- break
- }
- }
- if err == io.EOF {
-
- if b.Available() == 0 {
- err = b.flush()
- } else {
- err = nil
- }
- }
- return n, err
- }
- type ReadWriter struct {
- *Reader
- *Writer
- }
- func NewReadWriter(r *Reader, w *Writer) *ReadWriter {
- return &ReadWriter{r, w}
- }
|