|
| 1 | +// Copyright 2017 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 | +package strings |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "io" |
| 10 | + "unicode/utf8" |
| 11 | + "unsafe" |
| 12 | +) |
| 13 | + |
| 14 | +// A Builder is used to efficiently build a string using Write methods. |
| 15 | +// It minimizes memory copying. The zero value is ready to use. |
| 16 | +type Builder struct { |
| 17 | + buf []byte |
| 18 | +} |
| 19 | + |
| 20 | +// String returns the accumulated string. |
| 21 | +func (b *Builder) String() string { |
| 22 | + return *(*string)(unsafe.Pointer(&b.buf)) |
| 23 | +} |
| 24 | + |
| 25 | +// Len returns the number of accumulated bytes; b.Len() == len(b.String()). |
| 26 | +func (b *Builder) Len() int { return len(b.buf) } |
| 27 | + |
| 28 | +// Reset resets the Builder to be empty. |
| 29 | +func (b *Builder) Reset() { b.buf = nil } |
| 30 | + |
| 31 | +const maxInt = int(^uint(0) >> 1) |
| 32 | + |
| 33 | +// grow copies the buffer to a new, larger buffer so that there are at least n |
| 34 | +// bytes of capacity beyond len(b.buf). |
| 35 | +func (b *Builder) grow(n int) { |
| 36 | + buf := make([]byte, len(b.buf), 2*cap(b.buf)+n) |
| 37 | + copy(buf, b.buf) |
| 38 | + b.buf = buf |
| 39 | +} |
| 40 | + |
| 41 | +// Grow grows b's capacity, if necessary, to guarantee space for |
| 42 | +// another n bytes. After Grow(n), at least n bytes can be written to b |
| 43 | +// without another allocation. If n is negative, Grow panics. |
| 44 | +func (b *Builder) Grow(n int) { |
| 45 | + if n < 0 { |
| 46 | + panic("strings.Builder.Grow: negative count") |
| 47 | + } |
| 48 | + if cap(b.buf)-len(b.buf) < n { |
| 49 | + b.grow(n) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// Write appends the contents of p to b's buffer. |
| 54 | +// Write always returns len(p), nil. |
| 55 | +func (b *Builder) Write(p []byte) (int, error) { |
| 56 | + b.buf = append(b.buf, p...) |
| 57 | + return len(p), nil |
| 58 | +} |
| 59 | + |
| 60 | +// WriteByte appends the byte c to b's buffer. |
| 61 | +// The returned error is always nil. |
| 62 | +func (b *Builder) WriteByte(c byte) error { |
| 63 | + b.buf = append(b.buf, c) |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +// WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer. |
| 68 | +// It returns the length of r and a nil error. |
| 69 | +func (b *Builder) WriteRune(r rune) (int, error) { |
| 70 | + if r < utf8.RuneSelf { |
| 71 | + b.buf = append(b.buf, byte(r)) |
| 72 | + return 1, nil |
| 73 | + } |
| 74 | + l := len(b.buf) |
| 75 | + if cap(b.buf)-l < utf8.UTFMax { |
| 76 | + b.grow(utf8.UTFMax) |
| 77 | + } |
| 78 | + n := utf8.EncodeRune(b.buf[l:l+utf8.UTFMax], r) |
| 79 | + b.buf = b.buf[:l+n] |
| 80 | + return n, nil |
| 81 | +} |
| 82 | + |
| 83 | +// WriteString appends the contents of s to b's buffer. |
| 84 | +// It returns the length of s and a nil error. |
| 85 | +func (b *Builder) WriteString(s string) (int, error) { |
| 86 | + b.buf = append(b.buf, s...) |
| 87 | + return len(s), nil |
| 88 | +} |
| 89 | + |
| 90 | +// minRead is the minimum slice passed to a Read call by Builder.ReadFrom. |
| 91 | +// It is the same as bytes.MinRead. |
| 92 | +const minRead = 512 |
| 93 | + |
| 94 | +// errNegativeRead is the panic value if the reader passed to Builder.ReadFrom |
| 95 | +// returns a negative count. |
| 96 | +var errNegativeRead = errors.New("strings.Builder: reader returned negative count from Read") |
| 97 | + |
| 98 | +// ReadFrom reads data from r until EOF and appends it to b's buffer. |
| 99 | +// The return value n is the number of bytes read. |
| 100 | +// Any error except io.EOF encountered during the read is also returned. |
| 101 | +func (b *Builder) ReadFrom(r io.Reader) (n int64, err error) { |
| 102 | + for { |
| 103 | + l := len(b.buf) |
| 104 | + if cap(b.buf)-l < minRead { |
| 105 | + b.grow(minRead) |
| 106 | + } |
| 107 | + m, e := r.Read(b.buf[l:cap(b.buf)]) |
| 108 | + if m < 0 { |
| 109 | + panic(errNegativeRead) |
| 110 | + } |
| 111 | + b.buf = b.buf[:l+m] |
| 112 | + n += int64(m) |
| 113 | + if e == io.EOF { |
| 114 | + return n, nil |
| 115 | + } |
| 116 | + if e != nil { |
| 117 | + return n, e |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments