From b591e34caef5663ae89a59ba96c9894ff9faac35 Mon Sep 17 00:00:00 2001 From: varunu28 Date: Mon, 21 Aug 2023 17:45:14 -0700 Subject: [PATCH 1/6] FEAT: Add support for additional types --- stringbuilder.go | 24 +++++++++++++++++++++++- stringbuilder_test.go | 21 +++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/stringbuilder.go b/stringbuilder.go index 047e890..bf98c24 100644 --- a/stringbuilder.go +++ b/stringbuilder.go @@ -1,6 +1,9 @@ package Text -import "fmt" +import ( + "fmt" + "strconv" +) type StringBuilder struct { data []rune @@ -21,6 +24,10 @@ func NewStringBuilderFromString(text string) *StringBuilder { } } +type SupportedTypes interface { + string | int +} + // Appends a text to the StringBuilder instance func (s *StringBuilder) Append(text string) *StringBuilder { textRunes := []rune(text) @@ -56,6 +63,21 @@ func (s *StringBuilder) AppendRune(char rune) *StringBuilder { return s } +func (s *StringBuilder) AppendInt(integer int) *StringBuilder { + return s.Append(strconv.Itoa(integer)) +} + +func (s *StringBuilder) AppendBoolean(flag bool) *StringBuilder { + return s.Append(strconv.FormatBool(flag)) +} + +func (s *StringBuilder) AppendList(words []string) *StringBuilder { + for _, word := range words { + s = s.Append(word) + } + return s +} + // 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..4d477a8 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).AppendBoolean(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 From cda3a17e823225296f1f55fa98f0590296707b18 Mon Sep 17 00:00:00 2001 From: varunu28 Date: Mon, 21 Aug 2023 17:47:16 -0700 Subject: [PATCH 2/6] Remove unused code --- stringbuilder.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/stringbuilder.go b/stringbuilder.go index bf98c24..60a799f 100644 --- a/stringbuilder.go +++ b/stringbuilder.go @@ -24,10 +24,6 @@ func NewStringBuilderFromString(text string) *StringBuilder { } } -type SupportedTypes interface { - string | int -} - // Appends a text to the StringBuilder instance func (s *StringBuilder) Append(text string) *StringBuilder { textRunes := []rune(text) From ae5041dcd6f6535909d250552cc1f046d411d1ea Mon Sep 17 00:00:00 2001 From: varunu28 Date: Mon, 21 Aug 2023 17:48:29 -0700 Subject: [PATCH 3/6] Added comments for new methods --- stringbuilder.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stringbuilder.go b/stringbuilder.go index 60a799f..b10db06 100644 --- a/stringbuilder.go +++ b/stringbuilder.go @@ -59,14 +59,17 @@ func (s *StringBuilder) AppendRune(char rune) *StringBuilder { 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) AppendBoolean(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 { for _, word := range words { s = s.Append(word) From 81912f9a9716b4a48f811c6943810721ca2ac429 Mon Sep 17 00:00:00 2001 From: varunu28 Date: Tue, 22 Aug 2023 07:04:15 -0700 Subject: [PATCH 4/6] Addressed comments --- stringbuilder.go | 23 +++++++++++++++-------- stringbuilder_test.go | 2 +- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/stringbuilder.go b/stringbuilder.go index b10db06..a5d5b28 100644 --- a/stringbuilder.go +++ b/stringbuilder.go @@ -26,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 } @@ -52,7 +48,6 @@ func (s *StringBuilder) AppendRune(char rune) *StringBuilder { if newLen > cap(s.data) { s.grow(newLen) } - s.data[s.position] = char s.position++ @@ -65,18 +60,30 @@ func (s *StringBuilder) AppendInt(integer int) *StringBuilder { } // Appends a single boolean to the StringBuilder instance -func (s *StringBuilder) AppendBoolean(flag bool) *StringBuilder { +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 4d477a8..5002c58 100644 --- a/stringbuilder_test.go +++ b/stringbuilder_test.go @@ -39,7 +39,7 @@ func TestAppendMultipleTypes(t *testing.T) { } for _, tt := range tests { s := &StringBuilder{} - s.Append(tt.stringInput).AppendInt(tt.intInput).AppendBoolean(tt.booleanInput).AppendList(tt.multipleStrings) + 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) From e6edb90e79b0103b355cd338a98132028fbd0bea Mon Sep 17 00:00:00 2001 From: varunu28 Date: Tue, 22 Aug 2023 15:46:28 -0700 Subject: [PATCH 5/6] Added release notes --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b14ce3..6941888 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T ## [Unreleased] +## [0.7.1] - 2023-08-22 + +### 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 From 6b891aeac0048f7c3097765b5f3cc02fda089ae2 Mon Sep 17 00:00:00 2001 From: varunu28 Date: Wed, 23 Aug 2023 06:31:46 -0700 Subject: [PATCH 6/6] Refactored CHANGELOG.md --- CHANGELOG.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6941888..c0c0d2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,6 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T ## [Unreleased] -## [0.7.1] - 2023-08-22 - ### 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"})`