Source file
src/bufio/bufio.go
1
2
3
4
5
6
7
8 package bufio
9
10 import (
11 "bytes"
12 "errors"
13 "io"
14 "strings"
15 "unicode/utf8"
16 )
17
18 const (
19 defaultBufSize = 4096
20 )
21
22 var (
23 ErrInvalidUnreadByte = errors.New("bufio: invalid use of UnreadByte")
24 ErrInvalidUnreadRune = errors.New("bufio: invalid use of UnreadRune")
25 ErrBufferFull = errors.New("bufio: buffer full")
26 ErrNegativeCount = errors.New("bufio: negative count")
27 )
28
29
30
31
32 type Reader struct {
33 buf []byte
34 rd io.Reader
35 r, w int
36 err error
37 lastByte int
38 lastRuneSize int
39 }
40
41 const minReadBufferSize = 16
42 const maxConsecutiveEmptyReads = 100
43
44
45
46
47 func NewReaderSize(rd io.Reader, size int) *Reader {
48
49 b, ok := rd.(*Reader)
50 if ok && len(b.buf) >= size {
51 return b
52 }
53 if size < minReadBufferSize {
54 size = minReadBufferSize
55 }
56 r := new(Reader)
57 r.reset(make([]byte, size), rd)
58 return r
59 }
60
61
62 func NewReader(rd io.Reader) *Reader {
63 return NewReaderSize(rd, defaultBufSize)
64 }
65
66
67 func (b *Reader) Size() int { return len(b.buf) }
68
69
70
71
72
73 func (b *Reader) Reset(r io.Reader) {
74 if b.buf == nil {
75 b.buf = make([]byte, defaultBufSize)
76 }
77 b.reset(b.buf, r)
78 }
79
80 func (b *Reader) reset(buf []byte, r io.Reader) {
81 *b = Reader{
82 buf: buf,
83 rd: r,
84 lastByte: -1,
85 lastRuneSize: -1,
86 }
87 }
88
89 var errNegativeRead = errors.New("bufio: reader returned negative count from Read")
90
91
92 func (b *Reader) fill() {
93
94 if b.r > 0 {
95 copy(b.buf, b.buf[b.r:b.w])
96 b.w -= b.r
97 b.r = 0
98 }
99
100 if b.w >= len(b.buf) {
101 panic("bufio: tried to fill full buffer")
102 }
103
104
105 for i := maxConsecutiveEmptyReads; i > 0; i-- {
106 n, err := b.rd.Read(b.buf[b.w:])
107 if n < 0 {
108 panic(errNegativeRead)
109 }
110 b.w += n
111 if err != nil {
112 b.err = err
113 return
114 }
115 if n > 0 {
116 return
117 }
118 }
119 b.err = io.ErrNoProgress
120 }
121
122 func (b *Reader) readErr() error {
123 err := b.err
124 b.err = nil
125 return err
126 }
127
128
129
130
131
132
133
134
135 func (b *Reader) Peek(n int) ([]byte, error) {
136 if n < 0 {
137 return nil, ErrNegativeCount
138 }
139
140 b.lastByte = -1
141 b.lastRuneSize = -1
142
143 for b.w-b.r < n && b.w-b.r < len(b.buf) && b.err == nil {
144 b.fill()
145 }
146
147 if n > len(b.buf) {
148 return b.buf[b.r:b.w], ErrBufferFull
149 }
150
151
152 var err error
153 if avail := b.w - b.r; avail < n {
154
155 n = avail
156 err = b.readErr()
157 if err == nil {
158 err = ErrBufferFull
159 }
160 }
161 return b.buf[b.r : b.r+n], err
162 }
163
164
165
166
167
168
169 func (b *Reader) Discard(n int) (discarded int, err error) {
170 if n < 0 {
171 return 0, ErrNegativeCount
172 }
173 if n == 0 {
174 return
175 }
176
177 b.lastByte = -1
178 b.lastRuneSize = -1
179
180 remain := n
181 for {
182 skip := b.Buffered()
183 if skip == 0 {
184 b.fill()
185 skip = b.Buffered()
186 }
187 if skip > remain {
188 skip = remain
189 }
190 b.r += skip
191 remain -= skip
192 if remain == 0 {
193 return n, nil
194 }
195 if b.err != nil {
196 return n - remain, b.readErr()
197 }
198 }
199 }
200
201
202
203
204
205
206
207 func (b *Reader) Read(p []byte) (n int, err error) {
208 n = len(p)
209 if n == 0 {
210 if b.Buffered() > 0 {
211 return 0, nil
212 }
213 return 0, b.readErr()
214 }
215 if b.r == b.w {
216 if b.err != nil {
217 return 0, b.readErr()
218 }
219 if len(p) >= len(b.buf) {
220
221
222 n, b.err = b.rd.Read(p)
223 if n < 0 {
224 panic(errNegativeRead)
225 }
226 if n > 0 {
227 b.lastByte = int(p[n-1])
228 b.lastRuneSize = -1
229 }
230 return n, b.readErr()
231 }
232
233
234 b.r = 0
235 b.w = 0
236 n, b.err = b.rd.Read(b.buf)
237 if n < 0 {
238 panic(errNegativeRead)
239 }
240 if n == 0 {
241 return 0, b.readErr()
242 }
243 b.w += n
244 }
245
246
247
248
249 n = copy(p, b.buf[b.r:b.w])
250 b.r += n
251 b.lastByte = int(b.buf[b.r-1])
252 b.lastRuneSize = -1
253 return n, nil
254 }
255
256
257
258 func (b *Reader) ReadByte() (byte, error) {
259 b.lastRuneSize = -1
260 for b.r == b.w {
261 if b.err != nil {
262 return 0, b.readErr()
263 }
264 b.fill()
265 }
266 c := b.buf[b.r]
267 b.r++
268 b.lastByte = int(c)
269 return c, nil
270 }
271
272
273
274
275
276
277 func (b *Reader) UnreadByte() error {
278 if b.lastByte < 0 || b.r == 0 && b.w > 0 {
279 return ErrInvalidUnreadByte
280 }
281
282 if b.r > 0 {
283 b.r--
284 } else {
285
286 b.w = 1
287 }
288 b.buf[b.r] = byte(b.lastByte)
289 b.lastByte = -1
290 b.lastRuneSize = -1
291 return nil
292 }
293
294
295
296
297 func (b *Reader) ReadRune() (r rune, size int, err error) {
298 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) {
299 b.fill()
300 }
301 b.lastRuneSize = -1
302 if b.r == b.w {
303 return 0, 0, b.readErr()
304 }
305 r, size = rune(b.buf[b.r]), 1
306 if r >= utf8.RuneSelf {
307 r, size = utf8.DecodeRune(b.buf[b.r:b.w])
308 }
309 b.r += size
310 b.lastByte = int(b.buf[b.r-1])
311 b.lastRuneSize = size
312 return r, size, nil
313 }
314
315
316
317
318
319 func (b *Reader) UnreadRune() error {
320 if b.lastRuneSize < 0 || b.r < b.lastRuneSize {
321 return ErrInvalidUnreadRune
322 }
323 b.r -= b.lastRuneSize
324 b.lastByte = -1
325 b.lastRuneSize = -1
326 return nil
327 }
328
329
330 func (b *Reader) Buffered() int { return b.w - b.r }
331
332
333
334
335
336
337
338
339
340
341
342 func (b *Reader) ReadSlice(delim byte) (line []byte, err error) {
343 s := 0
344 for {
345
346 if i := bytes.IndexByte(b.buf[b.r+s:b.w], delim); i >= 0 {
347 i += s
348 line = b.buf[b.r : b.r+i+1]
349 b.r += i + 1
350 break
351 }
352
353
354 if b.err != nil {
355 line = b.buf[b.r:b.w]
356 b.r = b.w
357 err = b.readErr()
358 break
359 }
360
361
362 if b.Buffered() >= len(b.buf) {
363 b.r = b.w
364 line = b.buf
365 err = ErrBufferFull
366 break
367 }
368
369 s = b.w - b.r
370
371 b.fill()
372 }
373
374
375 if i := len(line) - 1; i >= 0 {
376 b.lastByte = int(line[i])
377 b.lastRuneSize = -1
378 }
379
380 return
381 }
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399 func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error) {
400 line, err = b.ReadSlice('\n')
401 if err == ErrBufferFull {
402
403 if len(line) > 0 && line[len(line)-1] == '\r' {
404
405
406 if b.r == 0 {
407
408 panic("bufio: tried to rewind past start of buffer")
409 }
410 b.r--
411 line = line[:len(line)-1]
412 }
413 return line, true, nil
414 }
415
416 if len(line) == 0 {
417 if err != nil {
418 line = nil
419 }
420 return
421 }
422 err = nil
423
424 if line[len(line)-1] == '\n' {
425 drop := 1
426 if len(line) > 1 && line[len(line)-2] == '\r' {
427 drop = 2
428 }
429 line = line[:len(line)-drop]
430 }
431 return
432 }
433
434
435
436
437
438
439
440
441 func (b *Reader) collectFragments(delim byte) (fullBuffers [][]byte, finalFragment []byte, totalLen int, err error) {
442 var frag []byte
443
444 for {
445 var e error
446 frag, e = b.ReadSlice(delim)
447 if e == nil {
448 break
449 }
450 if e != ErrBufferFull {
451 err = e
452 break
453 }
454
455
456 buf := make([]byte, len(frag))
457 copy(buf, frag)
458 fullBuffers = append(fullBuffers, buf)
459 totalLen += len(buf)
460 }
461
462 totalLen += len(frag)
463 return fullBuffers, frag, totalLen, err
464 }
465
466
467
468
469
470
471
472
473 func (b *Reader) ReadBytes(delim byte) ([]byte, error) {
474 full, frag, n, err := b.collectFragments(delim)
475
476 buf := make([]byte, n)
477 n = 0
478
479 for i := range full {
480 n += copy(buf[n:], full[i])
481 }
482 copy(buf[n:], frag)
483 return buf, err
484 }
485
486
487
488
489
490
491
492
493 func (b *Reader) ReadString(delim byte) (string, error) {
494 full, frag, n, err := b.collectFragments(delim)
495
496 var buf strings.Builder
497 buf.Grow(n)
498
499 for _, fb := range full {
500 buf.Write(fb)
501 }
502 buf.Write(frag)
503 return buf.String(), err
504 }
505
506
507
508
509
510 func (b *Reader) WriteTo(w io.Writer) (n int64, err error) {
511 b.lastByte = -1
512 b.lastRuneSize = -1
513
514 n, err = b.writeBuf(w)
515 if err != nil {
516 return
517 }
518
519 if r, ok := b.rd.(io.WriterTo); ok {
520 m, err := r.WriteTo(w)
521 n += m
522 return n, err
523 }
524
525 if w, ok := w.(io.ReaderFrom); ok {
526 m, err := w.ReadFrom(b.rd)
527 n += m
528 return n, err
529 }
530
531 if b.w-b.r < len(b.buf) {
532 b.fill()
533 }
534
535 for b.r < b.w {
536
537 m, err := b.writeBuf(w)
538 n += m
539 if err != nil {
540 return n, err
541 }
542 b.fill()
543 }
544
545 if b.err == io.EOF {
546 b.err = nil
547 }
548
549 return n, b.readErr()
550 }
551
552 var errNegativeWrite = errors.New("bufio: writer returned negative count from Write")
553
554
555 func (b *Reader) writeBuf(w io.Writer) (int64, error) {
556 n, err := w.Write(b.buf[b.r:b.w])
557 if n < 0 {
558 panic(errNegativeWrite)
559 }
560 b.r += n
561 return int64(n), err
562 }
563
564
565
566
567
568
569
570
571
572 type Writer struct {
573 err error
574 buf []byte
575 n int
576 wr io.Writer
577 }
578
579
580
581
582 func NewWriterSize(w io.Writer, size int) *Writer {
583
584 b, ok := w.(*Writer)
585 if ok && len(b.buf) >= size {
586 return b
587 }
588 if size <= 0 {
589 size = defaultBufSize
590 }
591 return &Writer{
592 buf: make([]byte, size),
593 wr: w,
594 }
595 }
596
597
598
599
600 func NewWriter(w io.Writer) *Writer {
601 return NewWriterSize(w, defaultBufSize)
602 }
603
604
605 func (b *Writer) Size() int { return len(b.buf) }
606
607
608
609
610
611 func (b *Writer) Reset(w io.Writer) {
612 if b.buf == nil {
613 b.buf = make([]byte, defaultBufSize)
614 }
615 b.err = nil
616 b.n = 0
617 b.wr = w
618 }
619
620
621 func (b *Writer) Flush() error {
622 if b.err != nil {
623 return b.err
624 }
625 if b.n == 0 {
626 return nil
627 }
628 n, err := b.wr.Write(b.buf[0:b.n])
629 if n < b.n && err == nil {
630 err = io.ErrShortWrite
631 }
632 if err != nil {
633 if n > 0 && n < b.n {
634 copy(b.buf[0:b.n-n], b.buf[n:b.n])
635 }
636 b.n -= n
637 b.err = err
638 return err
639 }
640 b.n = 0
641 return nil
642 }
643
644
645 func (b *Writer) Available() int { return len(b.buf) - b.n }
646
647
648
649
650
651 func (b *Writer) AvailableBuffer() []byte {
652 return b.buf[b.n:][:0]
653 }
654
655
656 func (b *Writer) Buffered() int { return b.n }
657
658
659
660
661
662 func (b *Writer) Write(p []byte) (nn int, err error) {
663 for len(p) > b.Available() && b.err == nil {
664 var n int
665 if b.Buffered() == 0 {
666
667
668 n, b.err = b.wr.Write(p)
669 } else {
670 n = copy(b.buf[b.n:], p)
671 b.n += n
672 b.Flush()
673 }
674 nn += n
675 p = p[n:]
676 }
677 if b.err != nil {
678 return nn, b.err
679 }
680 n := copy(b.buf[b.n:], p)
681 b.n += n
682 nn += n
683 return nn, nil
684 }
685
686
687 func (b *Writer) WriteByte(c byte) error {
688 if b.err != nil {
689 return b.err
690 }
691 if b.Available() <= 0 && b.Flush() != nil {
692 return b.err
693 }
694 b.buf[b.n] = c
695 b.n++
696 return nil
697 }
698
699
700
701 func (b *Writer) WriteRune(r rune) (size int, err error) {
702
703 if uint32(r) < utf8.RuneSelf {
704 err = b.WriteByte(byte(r))
705 if err != nil {
706 return 0, err
707 }
708 return 1, nil
709 }
710 if b.err != nil {
711 return 0, b.err
712 }
713 n := b.Available()
714 if n < utf8.UTFMax {
715 if b.Flush(); b.err != nil {
716 return 0, b.err
717 }
718 n = b.Available()
719 if n < utf8.UTFMax {
720
721 return b.WriteString(string(r))
722 }
723 }
724 size = utf8.EncodeRune(b.buf[b.n:], r)
725 b.n += size
726 return size, nil
727 }
728
729
730
731
732
733 func (b *Writer) WriteString(s string) (int, error) {
734 nn := 0
735 for len(s) > b.Available() && b.err == nil {
736 n := copy(b.buf[b.n:], s)
737 b.n += n
738 nn += n
739 s = s[n:]
740 b.Flush()
741 }
742 if b.err != nil {
743 return nn, b.err
744 }
745 n := copy(b.buf[b.n:], s)
746 b.n += n
747 nn += n
748 return nn, nil
749 }
750
751
752
753
754
755 func (b *Writer) ReadFrom(r io.Reader) (n int64, err error) {
756 if b.err != nil {
757 return 0, b.err
758 }
759 readerFrom, readerFromOK := b.wr.(io.ReaderFrom)
760 var m int
761 for {
762 if b.Available() == 0 {
763 if err1 := b.Flush(); err1 != nil {
764 return n, err1
765 }
766 }
767 if readerFromOK && b.Buffered() == 0 {
768 nn, err := readerFrom.ReadFrom(r)
769 b.err = err
770 n += nn
771 return n, err
772 }
773 nr := 0
774 for nr < maxConsecutiveEmptyReads {
775 m, err = r.Read(b.buf[b.n:])
776 if m != 0 || err != nil {
777 break
778 }
779 nr++
780 }
781 if nr == maxConsecutiveEmptyReads {
782 return n, io.ErrNoProgress
783 }
784 b.n += m
785 n += int64(m)
786 if err != nil {
787 break
788 }
789 }
790 if err == io.EOF {
791
792 if b.Available() == 0 {
793 err = b.Flush()
794 } else {
795 err = nil
796 }
797 }
798 return n, err
799 }
800
801
802
803
804
805 type ReadWriter struct {
806 *Reader
807 *Writer
808 }
809
810
811 func NewReadWriter(r *Reader, w *Writer) *ReadWriter {
812 return &ReadWriter{r, w}
813 }
814
View as plain text