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