forked from diamondburned/gotk4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper.go
91 lines (71 loc) · 2.16 KB
/
paper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package pen
import (
"bytes"
"strings"
)
// PaperString wraps a Pen and its own buffer.
type PaperString struct {
Pen
}
// NewPaperString creates a new Paper that preallocates 1KB.
func NewPaperString() *PaperString {
return NewPaperStringSize(10240) // 10KB
}
// NewPaperStringSize creates a new Paper with the given size to preallocate.
func NewPaperStringSize(size int) *PaperString {
builder := strings.Builder{}
builder.Grow(size)
return &PaperString{Pen{&builder}}
}
func (p *PaperString) builder() *strings.Builder { return p.PenWriter.(*strings.Builder) }
// Reset clears the internal buffer.
func (p *PaperString) Reset() {
p.builder().Reset()
}
// Len returns the internal length of the buffer.
func (p *PaperString) Len() int {
return p.builder().Len()
}
// String returns the final string written from the Pen.
func (p *PaperString) String() string {
return strings.TrimSuffix(p.builder().String(), "\n")
}
// IsEmpty returns true if the buffer is empty.
func (p *PaperString) IsEmpty() bool {
return p.builder().Len() == 0
}
// PaperBuffer wraps a Pen and its own buffer.
type PaperBuffer struct {
Pen
}
// NewPaperBuffer creates a new Paper that preallocates 1KB.
func NewPaperBuffer() *PaperBuffer {
return NewPaperBufferSize(10240) // 10KB
}
// NewPaperBufferSize creates a new Paper with the given size to preallocate.
func NewPaperBufferSize(size int) *PaperBuffer {
builder := bytes.Buffer{}
builder.Grow(size)
return &PaperBuffer{Pen{&builder}}
}
func (p *PaperBuffer) builder() *bytes.Buffer { return p.PenWriter.(*bytes.Buffer) }
// Reset clears the internal buffer.
func (p *PaperBuffer) Reset() {
p.builder().Reset()
}
// Len returns the internal length of the buffer.
func (p *PaperBuffer) Len() int {
return p.builder().Len()
}
// String returns the copied final string written from the Pen.
func (p *PaperBuffer) String() string {
return strings.TrimSuffix(p.builder().String(), "\n")
}
// Bytes returns the internal byte slice.
func (p *PaperBuffer) Bytes() []byte {
return bytes.TrimSuffix(p.builder().Bytes(), []byte("\n"))
}
// IsEmpty returns true if the buffer is empty.
func (p *PaperBuffer) IsEmpty() bool {
return p.builder().Len() == 0
}