]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/bufio/bufio.go
Add Go frontend, libgo library, and Go testsuite.
[thirdparty/gcc.git] / libgo / go / bufio / bufio.go
1 // Copyright 2009 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 // This package implements buffered I/O. It wraps an io.Reader or io.Writer
6 // object, creating another object (Reader or Writer) that also implements
7 // the interface but provides buffering and some help for textual I/O.
8 package bufio
9
10 import (
11 "bytes"
12 "io"
13 "os"
14 "strconv"
15 "utf8"
16 )
17
18
19 const (
20 defaultBufSize = 4096
21 )
22
23 // Errors introduced by this package.
24 type Error struct {
25 os.ErrorString
26 }
27
28 var (
29 ErrInvalidUnreadByte os.Error = &Error{"bufio: invalid use of UnreadByte"}
30 ErrInvalidUnreadRune os.Error = &Error{"bufio: invalid use of UnreadRune"}
31 ErrBufferFull os.Error = &Error{"bufio: buffer full"}
32 ErrNegativeCount os.Error = &Error{"bufio: negative count"}
33 errInternal os.Error = &Error{"bufio: internal error"}
34 )
35
36 // BufSizeError is the error representing an invalid buffer size.
37 type BufSizeError int
38
39 func (b BufSizeError) String() string {
40 return "bufio: bad buffer size " + strconv.Itoa(int(b))
41 }
42
43
44 // Buffered input.
45
46 // Reader implements buffering for an io.Reader object.
47 type Reader struct {
48 buf []byte
49 rd io.Reader
50 r, w int
51 err os.Error
52 lastByte int
53 lastRuneSize int
54 }
55
56 // NewReaderSize creates a new Reader whose buffer has the specified size,
57 // which must be greater than zero. If the argument io.Reader is already a
58 // Reader with large enough size, it returns the underlying Reader.
59 // It returns the Reader and any error.
60 func NewReaderSize(rd io.Reader, size int) (*Reader, os.Error) {
61 if size <= 0 {
62 return nil, BufSizeError(size)
63 }
64 // Is it already a Reader?
65 b, ok := rd.(*Reader)
66 if ok && len(b.buf) >= size {
67 return b, nil
68 }
69 b = new(Reader)
70 b.buf = make([]byte, size)
71 b.rd = rd
72 b.lastByte = -1
73 b.lastRuneSize = -1
74 return b, nil
75 }
76
77 // NewReader returns a new Reader whose buffer has the default size.
78 func NewReader(rd io.Reader) *Reader {
79 b, err := NewReaderSize(rd, defaultBufSize)
80 if err != nil {
81 // cannot happen - defaultBufSize is a valid size
82 panic(err)
83 }
84 return b
85 }
86
87 // fill reads a new chunk into the buffer.
88 func (b *Reader) fill() {
89 // Slide existing data to beginning.
90 if b.r > 0 {
91 copy(b.buf, b.buf[b.r:b.w])
92 b.w -= b.r
93 b.r = 0
94 }
95
96 // Read new data.
97 n, e := b.rd.Read(b.buf[b.w:])
98 b.w += n
99 if e != nil {
100 b.err = e
101 }
102 }
103
104 // Peek returns the next n bytes without advancing the reader. The bytes stop
105 // being valid at the next read call. If Peek returns fewer than n bytes, it
106 // also returns an error explaining why the read is short. The error is
107 // ErrBufferFull if n is larger than b's buffer size.
108 func (b *Reader) Peek(n int) ([]byte, os.Error) {
109 if n < 0 {
110 return nil, ErrNegativeCount
111 }
112 if n > len(b.buf) {
113 return nil, ErrBufferFull
114 }
115 for b.w-b.r < n && b.err == nil {
116 b.fill()
117 }
118 m := b.w - b.r
119 if m > n {
120 m = n
121 }
122 err := b.err
123 if m < n && err == nil {
124 err = ErrBufferFull
125 }
126 return b.buf[b.r : b.r+m], err
127 }
128
129 // Read reads data into p.
130 // It returns the number of bytes read into p.
131 // If nn < len(p), also returns an error explaining
132 // why the read is short. At EOF, the count will be
133 // zero and err will be os.EOF.
134 func (b *Reader) Read(p []byte) (nn int, err os.Error) {
135 nn = 0
136 for len(p) > 0 {
137 n := len(p)
138 if b.w == b.r {
139 if b.err != nil {
140 return nn, b.err
141 }
142 if len(p) >= len(b.buf) {
143 // Large read, empty buffer.
144 // Read directly into p to avoid copy.
145 n, b.err = b.rd.Read(p)
146 if n > 0 {
147 b.lastByte = int(p[n-1])
148 b.lastRuneSize = -1
149 }
150 p = p[n:]
151 nn += n
152 continue
153 }
154 b.fill()
155 continue
156 }
157 if n > b.w-b.r {
158 n = b.w - b.r
159 }
160 copy(p[0:n], b.buf[b.r:])
161 p = p[n:]
162 b.r += n
163 b.lastByte = int(b.buf[b.r-1])
164 b.lastRuneSize = -1
165 nn += n
166 }
167 return nn, nil
168 }
169
170 // ReadByte reads and returns a single byte.
171 // If no byte is available, returns an error.
172 func (b *Reader) ReadByte() (c byte, err os.Error) {
173 b.lastRuneSize = -1
174 for b.w == b.r {
175 if b.err != nil {
176 return 0, b.err
177 }
178 b.fill()
179 }
180 c = b.buf[b.r]
181 b.r++
182 b.lastByte = int(c)
183 return c, nil
184 }
185
186 // UnreadByte unreads the last byte. Only the most recently read byte can be unread.
187 func (b *Reader) UnreadByte() os.Error {
188 b.lastRuneSize = -1
189 if b.r == b.w && b.lastByte >= 0 {
190 b.w = 1
191 b.r = 0
192 b.buf[0] = byte(b.lastByte)
193 b.lastByte = -1
194 return nil
195 }
196 if b.r <= 0 {
197 return ErrInvalidUnreadByte
198 }
199 b.r--
200 b.lastByte = -1
201 return nil
202 }
203
204 // ReadRune reads a single UTF-8 encoded Unicode character and returns the
205 // rune and its size in bytes.
206 func (b *Reader) ReadRune() (rune int, size int, err os.Error) {
207 for b.r+utf8.UTFMax > b.w && !utf8.FullRune(b.buf[b.r:b.w]) && b.err == nil {
208 b.fill()
209 }
210 b.lastRuneSize = -1
211 if b.r == b.w {
212 return 0, 0, b.err
213 }
214 rune, size = int(b.buf[b.r]), 1
215 if rune >= 0x80 {
216 rune, size = utf8.DecodeRune(b.buf[b.r:b.w])
217 }
218 b.r += size
219 b.lastByte = int(b.buf[b.r-1])
220 b.lastRuneSize = size
221 return rune, size, nil
222 }
223
224 // UnreadRune unreads the last rune. If the most recent read operation on
225 // the buffer was not a ReadRune, UnreadRune returns an error. (In this
226 // regard it is stricter than UnreadByte, which will unread the last byte
227 // from any read operation.)
228 func (b *Reader) UnreadRune() os.Error {
229 if b.lastRuneSize < 0 || b.r == 0 {
230 return ErrInvalidUnreadRune
231 }
232 b.r -= b.lastRuneSize
233 b.lastByte = -1
234 b.lastRuneSize = -1
235 return nil
236 }
237
238 // Buffered returns the number of bytes that can be read from the current buffer.
239 func (b *Reader) Buffered() int { return b.w - b.r }
240
241 // ReadSlice reads until the first occurrence of delim in the input,
242 // returning a slice pointing at the bytes in the buffer.
243 // The bytes stop being valid at the next read call.
244 // If ReadSlice encounters an error before finding a delimiter,
245 // it returns all the data in the buffer and the error itself (often os.EOF).
246 // ReadSlice fails with error ErrBufferFull if the buffer fills without a delim.
247 // Because the data returned from ReadSlice will be overwritten
248 // by the next I/O operation, most clients should use
249 // ReadBytes or ReadString instead.
250 // ReadSlice returns err != nil if and only if line does not end in delim.
251 func (b *Reader) ReadSlice(delim byte) (line []byte, err os.Error) {
252 // Look in buffer.
253 if i := bytes.IndexByte(b.buf[b.r:b.w], delim); i >= 0 {
254 line1 := b.buf[b.r : b.r+i+1]
255 b.r += i + 1
256 return line1, nil
257 }
258
259 // Read more into buffer, until buffer fills or we find delim.
260 for {
261 if b.err != nil {
262 line := b.buf[b.r:b.w]
263 b.r = b.w
264 return line, b.err
265 }
266
267 n := b.Buffered()
268 b.fill()
269
270 // Search new part of buffer
271 if i := bytes.IndexByte(b.buf[n:b.w], delim); i >= 0 {
272 line := b.buf[0 : n+i+1]
273 b.r = n + i + 1
274 return line, nil
275 }
276
277 // Buffer is full?
278 if b.Buffered() >= len(b.buf) {
279 b.r = b.w
280 return b.buf, ErrBufferFull
281 }
282 }
283 panic("not reached")
284 }
285
286 // ReadBytes reads until the first occurrence of delim in the input,
287 // returning a slice containing the data up to and including the delimiter.
288 // If ReadBytes encounters an error before finding a delimiter,
289 // it returns the data read before the error and the error itself (often os.EOF).
290 // ReadBytes returns err != nil if and only if line does not end in delim.
291 func (b *Reader) ReadBytes(delim byte) (line []byte, err os.Error) {
292 // Use ReadSlice to look for array,
293 // accumulating full buffers.
294 var frag []byte
295 var full [][]byte
296 err = nil
297
298 for {
299 var e os.Error
300 frag, e = b.ReadSlice(delim)
301 if e == nil { // got final fragment
302 break
303 }
304 if e != ErrBufferFull { // unexpected error
305 err = e
306 break
307 }
308
309 // Make a copy of the buffer.
310 buf := make([]byte, len(frag))
311 copy(buf, frag)
312 full = append(full, buf)
313 }
314
315 // Allocate new buffer to hold the full pieces and the fragment.
316 n := 0
317 for i := range full {
318 n += len(full[i])
319 }
320 n += len(frag)
321
322 // Copy full pieces and fragment in.
323 buf := make([]byte, n)
324 n = 0
325 for i := range full {
326 n += copy(buf[n:], full[i])
327 }
328 copy(buf[n:], frag)
329 return buf, err
330 }
331
332 // ReadString reads until the first occurrence of delim in the input,
333 // returning a string containing the data up to and including the delimiter.
334 // If ReadString encounters an error before finding a delimiter,
335 // it returns the data read before the error and the error itself (often os.EOF).
336 // ReadString returns err != nil if and only if line does not end in delim.
337 func (b *Reader) ReadString(delim byte) (line string, err os.Error) {
338 bytes, e := b.ReadBytes(delim)
339 return string(bytes), e
340 }
341
342
343 // buffered output
344
345 // Writer implements buffering for an io.Writer object.
346 type Writer struct {
347 err os.Error
348 buf []byte
349 n int
350 wr io.Writer
351 }
352
353 // NewWriterSize creates a new Writer whose buffer has the specified size,
354 // which must be greater than zero. If the argument io.Writer is already a
355 // Writer with large enough size, it returns the underlying Writer.
356 // It returns the Writer and any error.
357 func NewWriterSize(wr io.Writer, size int) (*Writer, os.Error) {
358 if size <= 0 {
359 return nil, BufSizeError(size)
360 }
361 // Is it already a Writer?
362 b, ok := wr.(*Writer)
363 if ok && len(b.buf) >= size {
364 return b, nil
365 }
366 b = new(Writer)
367 b.buf = make([]byte, size)
368 b.wr = wr
369 return b, nil
370 }
371
372 // NewWriter returns a new Writer whose buffer has the default size.
373 func NewWriter(wr io.Writer) *Writer {
374 b, err := NewWriterSize(wr, defaultBufSize)
375 if err != nil {
376 // cannot happen - defaultBufSize is valid size
377 panic(err)
378 }
379 return b
380 }
381
382 // Flush writes any buffered data to the underlying io.Writer.
383 func (b *Writer) Flush() os.Error {
384 if b.err != nil {
385 return b.err
386 }
387 n, e := b.wr.Write(b.buf[0:b.n])
388 if n < b.n && e == nil {
389 e = io.ErrShortWrite
390 }
391 if e != nil {
392 if n > 0 && n < b.n {
393 copy(b.buf[0:b.n-n], b.buf[n:b.n])
394 }
395 b.n -= n
396 b.err = e
397 return e
398 }
399 b.n = 0
400 return nil
401 }
402
403 // Available returns how many bytes are unused in the buffer.
404 func (b *Writer) Available() int { return len(b.buf) - b.n }
405
406 // Buffered returns the number of bytes that have been written into the current buffer.
407 func (b *Writer) Buffered() int { return b.n }
408
409 // Write writes the contents of p into the buffer.
410 // It returns the number of bytes written.
411 // If nn < len(p), it also returns an error explaining
412 // why the write is short.
413 func (b *Writer) Write(p []byte) (nn int, err os.Error) {
414 if b.err != nil {
415 return 0, b.err
416 }
417 nn = 0
418 for len(p) > 0 {
419 n := b.Available()
420 if n <= 0 {
421 if b.Flush(); b.err != nil {
422 break
423 }
424 n = b.Available()
425 }
426 if b.Buffered() == 0 && len(p) >= len(b.buf) {
427 // Large write, empty buffer.
428 // Write directly from p to avoid copy.
429 n, b.err = b.wr.Write(p)
430 nn += n
431 p = p[n:]
432 if b.err != nil {
433 break
434 }
435 continue
436 }
437 if n > len(p) {
438 n = len(p)
439 }
440 copy(b.buf[b.n:b.n+n], p[0:n])
441 b.n += n
442 nn += n
443 p = p[n:]
444 }
445 return nn, b.err
446 }
447
448 // WriteByte writes a single byte.
449 func (b *Writer) WriteByte(c byte) os.Error {
450 if b.err != nil {
451 return b.err
452 }
453 if b.Available() <= 0 && b.Flush() != nil {
454 return b.err
455 }
456 b.buf[b.n] = c
457 b.n++
458 return nil
459 }
460
461 // WriteRune writes a single Unicode code point, returning
462 // the number of bytes written and any error.
463 func (b *Writer) WriteRune(rune int) (size int, err os.Error) {
464 if rune < utf8.RuneSelf {
465 err = b.WriteByte(byte(rune))
466 if err != nil {
467 return 0, err
468 }
469 return 1, nil
470 }
471 if b.err != nil {
472 return 0, b.err
473 }
474 n := b.Available()
475 if n < utf8.UTFMax {
476 if b.Flush(); b.err != nil {
477 return 0, b.err
478 }
479 n = b.Available()
480 if n < utf8.UTFMax {
481 // Can only happen if buffer is silly small.
482 return b.WriteString(string(rune))
483 }
484 }
485 size = utf8.EncodeRune(rune, b.buf[b.n:])
486 b.n += size
487 return size, nil
488 }
489
490 // WriteString writes a string.
491 // It returns the number of bytes written.
492 // If the count is less than len(s), it also returns an error explaining
493 // why the write is short.
494 func (b *Writer) WriteString(s string) (int, os.Error) {
495 if b.err != nil {
496 return 0, b.err
497 }
498 // Common case, worth making fast.
499 if b.Available() >= len(s) || len(b.buf) >= len(s) && b.Flush() == nil {
500 for i := 0; i < len(s); i++ { // loop over bytes, not runes.
501 b.buf[b.n] = s[i]
502 b.n++
503 }
504 return len(s), nil
505 }
506 for i := 0; i < len(s); i++ { // loop over bytes, not runes.
507 b.WriteByte(s[i])
508 if b.err != nil {
509 return i, b.err
510 }
511 }
512 return len(s), nil
513 }
514
515 // buffered input and output
516
517 // ReadWriter stores pointers to a Reader and a Writer.
518 // It implements io.ReadWriter.
519 type ReadWriter struct {
520 *Reader
521 *Writer
522 }
523
524 // NewReadWriter allocates a new ReadWriter that dispatches to r and w.
525 func NewReadWriter(r *Reader, w *Writer) *ReadWriter {
526 return &ReadWriter{r, w}
527 }