-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrbuffer.go
117 lines (98 loc) · 1.98 KB
/
strbuffer.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package toolkit
import (
"fmt"
"strings"
)
type StrBuffer struct {
builder strings.Builder
hash int
}
// check if it implements Base interface
var _ Base = (*StrBuffer)(nil)
func NewStrBuffer(str ...interface{}) *StrBuffer {
s := new(StrBuffer)
s.Add(str...)
return s
}
func (s *StrBuffer) Addf(template string, a ...interface{}) *StrBuffer {
s.builder.WriteString(fmt.Sprintf(template, a...))
return s
}
func (s *StrBuffer) Add(a ...interface{}) *StrBuffer {
for _, v := range a {
switch t := v.(type) {
case string:
s.builder.WriteString(t)
case fmt.Stringer:
s.builder.WriteString(t.String())
default:
s.builder.WriteString(fmt.Sprintf("%v", v))
}
}
return s
}
func (s *StrBuffer) Size() int {
return s.builder.Len()
}
func (s *StrBuffer) IsEmpty() bool {
return s.builder.Len() == 0
}
func (s *StrBuffer) Clear() {
s.builder.Reset()
}
func (s *StrBuffer) String() string {
return s.builder.String()
}
func (s *StrBuffer) Equals(e interface{}) bool {
switch t := e.(type) { //type switch
case *StrBuffer:
if s.Size() != t.Size() {
return false
}
b1 := s.builder.String()
b2 := t.builder.String()
return b1 == b2
}
return false
}
func (s *StrBuffer) Clone() interface{} {
x := &StrBuffer{}
x.Add(x.String())
return x
}
func (s *StrBuffer) HashCode() int {
if s.hash == 0 {
s.hash = HashString(HASH_SEED, s.String())
}
return s.hash
}
type Joiner struct {
*StrBuffer
separator string
}
func NewJoiner(separator string) *Joiner {
this := new(Joiner)
this.StrBuffer = new(StrBuffer)
this.separator = separator
return this
}
func (j *Joiner) Append(a ...interface{}) *Joiner {
j.StrBuffer.Add(a...)
return j
}
func (j *Joiner) AddAsOne(a ...interface{}) *Joiner {
if j.StrBuffer.Size() > 0 {
j.StrBuffer.Add(j.separator)
}
j.StrBuffer.Add(a...)
return j
}
func (j *Joiner) Add(a ...interface{}) *Joiner {
for _, v := range a {
if j.StrBuffer.Size() > 0 {
j.StrBuffer.Add(j.separator)
}
j.StrBuffer.Add(v)
}
return j
}