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