~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/bytes/buffer.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
87
87
                var buf []byte
88
88
                if b.buf == nil && n <= len(b.bootstrap) {
89
89
                        buf = b.bootstrap[0:]
 
90
                } else if m+n <= cap(b.buf)/2 {
 
91
                        // We can slide things down instead of allocating a new
 
92
                        // slice. We only need m+n <= cap(b.buf) to slide, but
 
93
                        // we instead let capacity get twice as large so we
 
94
                        // don't spend all our time copying.
 
95
                        copy(b.buf[:], b.buf[b.off:])
 
96
                        buf = b.buf[:m]
90
97
                } else {
91
98
                        // not enough space anywhere
92
99
                        buf = makeSlice(2*cap(b.buf) + n)
99
106
        return b.off + m
100
107
}
101
108
 
102
 
// Write appends the contents of p to the buffer.  The return
103
 
// value n is the length of p; err is always nil.
104
 
// If the buffer becomes too large, Write will panic with
105
 
// ErrTooLarge.
 
109
// Grow grows the buffer's capacity, if necessary, to guarantee space for
 
110
// another n bytes. After Grow(n), at least n bytes can be written to the
 
111
// buffer without another allocation.
 
112
// If n is negative, Grow will panic.
 
113
// If the buffer can't grow it will panic with ErrTooLarge.
 
114
func (b *Buffer) Grow(n int) {
 
115
        if n < 0 {
 
116
                panic("bytes.Buffer.Grow: negative count")
 
117
        }
 
118
        m := b.grow(n)
 
119
        b.buf = b.buf[0:m]
 
120
}
 
121
 
 
122
// Write appends the contents of p to the buffer, growing the buffer as
 
123
// needed. The return value n is the length of p; err is always nil. If the
 
124
// buffer becomes too large, Write will panic with ErrTooLarge.
106
125
func (b *Buffer) Write(p []byte) (n int, err error) {
107
126
        b.lastRead = opInvalid
108
127
        m := b.grow(len(p))
109
128
        return copy(b.buf[m:], p), nil
110
129
}
111
130
 
112
 
// WriteString appends the contents of s to the buffer.  The return
113
 
// value n is the length of s; err is always nil.
114
 
// If the buffer becomes too large, WriteString will panic with
115
 
// ErrTooLarge.
 
131
// WriteString appends the contents of s to the buffer, growing the buffer as
 
132
// needed. The return value n is the length of s; err is always nil. If the
 
133
// buffer becomes too large, WriteString will panic with ErrTooLarge.
116
134
func (b *Buffer) WriteString(s string) (n int, err error) {
117
135
        b.lastRead = opInvalid
118
136
        m := b.grow(len(s))
125
143
// underlying buffer.
126
144
const MinRead = 512
127
145
 
128
 
// ReadFrom reads data from r until EOF and appends it to the buffer.
129
 
// The return value n is the number of bytes read.
130
 
// Any error except io.EOF encountered during the read
131
 
// is also returned.
132
 
// If the buffer becomes too large, ReadFrom will panic with
133
 
// ErrTooLarge.
 
146
// ReadFrom reads data from r until EOF and appends it to the buffer, growing
 
147
// the buffer as needed. The return value n is the number of bytes read. Any
 
148
// error except io.EOF encountered during the read is also returned. If the
 
149
// buffer becomes too large, ReadFrom will panic with ErrTooLarge.
134
150
func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
135
151
        b.lastRead = opInvalid
136
152
        // If buffer is empty, reset to recover space.
175
191
        return make([]byte, n)
176
192
}
177
193
 
178
 
// WriteTo writes data to w until the buffer is drained or an error
179
 
// occurs. The return value n is the number of bytes written; it always
180
 
// fits into an int, but it is int64 to match the io.WriterTo interface.
181
 
// Any error encountered during the write is also returned.
 
194
// WriteTo writes data to w until the buffer is drained or an error occurs.
 
195
// The return value n is the number of bytes written; it always fits into an
 
196
// int, but it is int64 to match the io.WriterTo interface. Any error
 
197
// encountered during the write is also returned.
182
198
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
183
199
        b.lastRead = opInvalid
184
200
        if b.off < len(b.buf) {
203
219
        return
204
220
}
205
221
 
206
 
// WriteByte appends the byte c to the buffer.
207
 
// The returned error is always nil, but is included
208
 
// to match bufio.Writer's WriteByte.
209
 
// If the buffer becomes too large, WriteByte will panic with
 
222
// WriteByte appends the byte c to the buffer, growing the buffer as needed.
 
223
// The returned error is always nil, but is included to match bufio.Writer's
 
224
// WriteByte. If the buffer becomes too large, WriteByte will panic with
210
225
// ErrTooLarge.
211
226
func (b *Buffer) WriteByte(c byte) error {
212
227
        b.lastRead = opInvalid
215
230
        return nil
216
231
}
217
232
 
218
 
// WriteRune appends the UTF-8 encoding of Unicode
219
 
// code point r to the buffer, returning its length and
220
 
// an error, which is always nil but is included
221
 
// to match bufio.Writer's WriteRune.
222
 
// If the buffer becomes too large, WriteRune will panic with
223
 
// ErrTooLarge.
 
233
// WriteRune appends the UTF-8 encoding of Unicode code point r to the
 
234
// buffer, returning its length and an error, which is always nil but is
 
235
// included to match bufio.Writer's WriteRune. The buffer is grown as needed;
 
236
// if it becomes too large, WriteRune will panic with ErrTooLarge.
224
237
func (b *Buffer) WriteRune(r rune) (n int, err error) {
225
238
        if r < utf8.RuneSelf {
226
239
                b.WriteByte(byte(r))
347
360
// ReadBytes returns err != nil if and only if the returned data does not end in
348
361
// delim.
349
362
func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) {
 
363
        slice, err := b.readSlice(delim)
 
364
        // return a copy of slice. The buffer's backing array may
 
365
        // be overwritten by later calls.
 
366
        line = append(line, slice...)
 
367
        return
 
368
}
 
369
 
 
370
// readSlice is like ReadBytes but returns a reference to internal buffer data.
 
371
func (b *Buffer) readSlice(delim byte) (line []byte, err error) {
350
372
        i := IndexByte(b.buf[b.off:], delim)
351
 
        size := i + 1
 
373
        end := b.off + i + 1
352
374
        if i < 0 {
353
 
                size = len(b.buf) - b.off
 
375
                end = len(b.buf)
354
376
                err = io.EOF
355
377
        }
356
 
        line = make([]byte, size)
357
 
        copy(line, b.buf[b.off:])
358
 
        b.off += size
359
 
        return
 
378
        line = b.buf[b.off:end]
 
379
        b.off = end
 
380
        b.lastRead = opRead
 
381
        return line, err
360
382
}
361
383
 
362
384
// ReadString reads until the first occurrence of delim in the input,
366
388
// ReadString returns err != nil if and only if the returned data does not end
367
389
// in delim.
368
390
func (b *Buffer) ReadString(delim byte) (line string, err error) {
369
 
        bytes, err := b.ReadBytes(delim)
370
 
        return string(bytes), err
 
391
        slice, err := b.readSlice(delim)
 
392
        return string(slice), err
371
393
}
372
394
 
373
395
// NewBuffer creates and initializes a new Buffer using buf as its initial