diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b14ce3..c0c0d2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T ## [Unreleased] +### Added + - The string builder can now be used to append integers, boolean & collection of strings. `sb.AppendInt(1).AppendBool(true).AppendList([]string{"a", "b", "c"})` + ## [0.7.0] - 2023-08-21 \### Added diff --git a/stringbuilder.go b/stringbuilder.go index 047e890..a5d5b28 100644 --- a/stringbuilder.go +++ b/stringbuilder.go @@ -1,6 +1,9 @@ package Text -import "fmt" +import ( + "fmt" + "strconv" +) type StringBuilder struct { data []rune @@ -23,14 +26,10 @@ func NewStringBuilderFromString(text string) *StringBuilder { // Appends a text to the StringBuilder instance func (s *StringBuilder) Append(text string) *StringBuilder { + s.resize(text) textRunes := []rune(text) - newLen := s.position + len(textRunes) - if newLen > cap(s.data) { - s.grow(newLen) - } - copy(s.data[s.position:], textRunes) - s.position = newLen + s.position = s.position + len(textRunes) return s } @@ -49,13 +48,42 @@ func (s *StringBuilder) AppendRune(char rune) *StringBuilder { if newLen > cap(s.data) { s.grow(newLen) } - s.data[s.position] = char s.position++ return s } +// Appends a single integer to the StringBuilder instance +func (s *StringBuilder) AppendInt(integer int) *StringBuilder { + return s.Append(strconv.Itoa(integer)) +} + +// Appends a single boolean to the StringBuilder instance +func (s *StringBuilder) AppendBool(flag bool) *StringBuilder { + return s.Append(strconv.FormatBool(flag)) +} + +// Appends a list of strings to the StringBuilder instance +func (s *StringBuilder) AppendList(words []string) *StringBuilder { + s.resize(words...) + for _, word := range words { + s = s.Append(word) + } + return s +} + +func (s *StringBuilder) resize(words ...string) { + allWordLength := 0 + for _, word := range words { + allWordLength += len(word) + } + newLen := s.position + allWordLength + if newLen > cap(s.data) { + s.grow(newLen) + } +} + // Returns the current length of the represented string func (s *StringBuilder) Len() int { return s.position diff --git a/stringbuilder_test.go b/stringbuilder_test.go index 48cecfd..5002c58 100644 --- a/stringbuilder_test.go +++ b/stringbuilder_test.go @@ -26,6 +26,27 @@ func TestAppend(t *testing.T) { } } +func TestAppendMultipleTypes(t *testing.T) { + tests := []struct { + stringInput string + intInput int + booleanInput bool + multipleStrings []string + want string + }{ + {"hello", 123, false, []string{"a", "b", "c"}, "hello123falseabc"}, + {"hello", 123, true, []string{"a", "b", "c"}, "hello123trueabc"}, + } + for _, tt := range tests { + s := &StringBuilder{} + s.Append(tt.stringInput).AppendInt(tt.intInput).AppendBool(tt.booleanInput).AppendList(tt.multipleStrings) + + if got := s.ToString(); got != tt.want { + t.Errorf("StringBuilder.Append Multiple types = %v, want %v", got, tt.want) + } + } +} + func TestLen(t *testing.T) { tests := []struct { name string