Skip to content

Commit 0d216b0

Browse files
authored
FEAT: Add support for additional types (#3)
* FEAT: Add support for additional types * Remove unused code * Added comments for new methods * Addressed comments * Added release notes * Refactored CHANGELOG.md
1 parent e690191 commit 0d216b0

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
66

77
## [Unreleased]
88

9+
### Added
10+
- The string builder can now be used to append integers, boolean & collection of strings. `sb.AppendInt(1).AppendBool(true).AppendList([]string{"a", "b", "c"})`
11+
912
## [0.7.0] - 2023-08-21
1013

1114
\### Added

stringbuilder.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package Text
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"strconv"
6+
)
47

58
type StringBuilder struct {
69
data []rune
@@ -23,14 +26,10 @@ func NewStringBuilderFromString(text string) *StringBuilder {
2326

2427
// Appends a text to the StringBuilder instance
2528
func (s *StringBuilder) Append(text string) *StringBuilder {
29+
s.resize(text)
2630
textRunes := []rune(text)
27-
newLen := s.position + len(textRunes)
28-
if newLen > cap(s.data) {
29-
s.grow(newLen)
30-
}
31-
3231
copy(s.data[s.position:], textRunes)
33-
s.position = newLen
32+
s.position = s.position + len(textRunes)
3433

3534
return s
3635
}
@@ -49,13 +48,42 @@ func (s *StringBuilder) AppendRune(char rune) *StringBuilder {
4948
if newLen > cap(s.data) {
5049
s.grow(newLen)
5150
}
52-
5351
s.data[s.position] = char
5452
s.position++
5553

5654
return s
5755
}
5856

57+
// Appends a single integer to the StringBuilder instance
58+
func (s *StringBuilder) AppendInt(integer int) *StringBuilder {
59+
return s.Append(strconv.Itoa(integer))
60+
}
61+
62+
// Appends a single boolean to the StringBuilder instance
63+
func (s *StringBuilder) AppendBool(flag bool) *StringBuilder {
64+
return s.Append(strconv.FormatBool(flag))
65+
}
66+
67+
// Appends a list of strings to the StringBuilder instance
68+
func (s *StringBuilder) AppendList(words []string) *StringBuilder {
69+
s.resize(words...)
70+
for _, word := range words {
71+
s = s.Append(word)
72+
}
73+
return s
74+
}
75+
76+
func (s *StringBuilder) resize(words ...string) {
77+
allWordLength := 0
78+
for _, word := range words {
79+
allWordLength += len(word)
80+
}
81+
newLen := s.position + allWordLength
82+
if newLen > cap(s.data) {
83+
s.grow(newLen)
84+
}
85+
}
86+
5987
// Returns the current length of the represented string
6088
func (s *StringBuilder) Len() int {
6189
return s.position

stringbuilder_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ func TestAppend(t *testing.T) {
2626
}
2727
}
2828

29+
func TestAppendMultipleTypes(t *testing.T) {
30+
tests := []struct {
31+
stringInput string
32+
intInput int
33+
booleanInput bool
34+
multipleStrings []string
35+
want string
36+
}{
37+
{"hello", 123, false, []string{"a", "b", "c"}, "hello123falseabc"},
38+
{"hello", 123, true, []string{"a", "b", "c"}, "hello123trueabc"},
39+
}
40+
for _, tt := range tests {
41+
s := &StringBuilder{}
42+
s.Append(tt.stringInput).AppendInt(tt.intInput).AppendBool(tt.booleanInput).AppendList(tt.multipleStrings)
43+
44+
if got := s.ToString(); got != tt.want {
45+
t.Errorf("StringBuilder.Append Multiple types = %v, want %v", got, tt.want)
46+
}
47+
}
48+
}
49+
2950
func TestLen(t *testing.T) {
3051
tests := []struct {
3152
name string

0 commit comments

Comments
 (0)