-
Notifications
You must be signed in to change notification settings - Fork 122
/
stringbuilder.go
77 lines (61 loc) · 1.4 KB
/
stringbuilder.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
// Copyright 2023 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package sqlbuilder
import (
"io"
"strings"
)
type stringBuilder struct {
builder *strings.Builder
}
var _ io.Writer = new(stringBuilder)
func newStringBuilder() *stringBuilder {
return &stringBuilder{
builder: &strings.Builder{},
}
}
// WriteLeadingString writes s to internal buffer.
// If it's not the first time to write the string, a blank (" ") will be written before s.
func (sb *stringBuilder) WriteLeadingString(s string) {
if sb.builder.Len() > 0 {
sb.builder.WriteString(" ")
}
sb.builder.WriteString(s)
}
func (sb *stringBuilder) WriteString(s string) {
sb.builder.WriteString(s)
}
func (sb *stringBuilder) WriteStrings(ss []string, sep string) {
if len(ss) == 0 {
return
}
firstAdded := false
if len(ss[0]) != 0 {
sb.WriteString(ss[0])
firstAdded = true
}
for _, s := range ss[1:] {
if len(s) != 0 {
if firstAdded {
sb.WriteString(sep)
}
sb.WriteString(s)
firstAdded = true
}
}
}
func (sb *stringBuilder) WriteRune(r rune) {
sb.builder.WriteRune(r)
}
func (sb *stringBuilder) Write(data []byte) (int, error) {
return sb.builder.Write(data)
}
func (sb *stringBuilder) String() string {
return sb.builder.String()
}
func (sb *stringBuilder) Reset() {
sb.builder.Reset()
}
func (sb *stringBuilder) Grow(n int) {
sb.builder.Grow(n)
}