-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringbuilder.go
80 lines (67 loc) · 2.06 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
78
79
80
package stringx
import (
"bytes"
"fmt"
"strconv"
)
// StringBuilder encapsulates the buffer to use for the builder
type StringBuilder struct {
buffer bytes.Buffer
}
// Builder Creates a new StringBuilder
func Builder() *StringBuilder {
return &StringBuilder{}
}
// Append Appends the given string(s) to the builder
func (s *StringBuilder) Append(args ...string) *StringBuilder {
for _, v := range args {
s.buffer.WriteString(v)
}
return s
}
// Appendf processes a string format based on the format and arguments passed and appends the result to the builder
func (s *StringBuilder) Appendf(format string, args ...interface{}) *StringBuilder {
return s.Append(fmt.Sprintf(format, args...))
}
// AppendObj attempts to obtain a string representation of the interface{} (s) and appends it/them to the builder
func (s *StringBuilder) AppendObj(objs ...interface{}) *StringBuilder {
for _, v := range objs {
s.Append(fmt.Sprintf("%+v", v))
}
return s
}
// AppendInt Appends the given number(s) to the builder
func (s *StringBuilder) AppendInt(i ...int) *StringBuilder {
for _, v := range i {
s.Append(strconv.Itoa(v))
}
return s
}
// AppendLine Appends the given string(s) to the builder.
func (s *StringBuilder) AppendLine(lines ...string) *StringBuilder {
for _, v := range lines {
s.Append(v).Br()
}
return s
}
// AppendLinef processes a string format based on the format and arguments passed, appends the result to the builder and a new line at the end
func (s *StringBuilder) AppendLinef(format string, args ...interface{}) *StringBuilder {
return s.AppendLine(fmt.Sprintf(format, args...))
}
// AppendRune Appends a single character to the builder
func (s *StringBuilder) AppendRune(char rune) *StringBuilder {
s.buffer.WriteRune(char)
return s
}
// Br Breaks to the next line
func (s *StringBuilder) Br() *StringBuilder {
return s.Append(LineBreak)
}
// IsEmpty Indicates whether the builder is empty
func (s *StringBuilder) IsEmpty() bool {
return s.buffer.Len() == 0
}
// Build Builds the string
func (s *StringBuilder) Build() string {
return s.buffer.String()
}