Skip to content

Commit 7baf194

Browse files
committed
Added Clear and renamed ctor
1 parent 53f06f7 commit 7baf194

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

stringbuilder.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func NewStringBuilder(initialCapacity int) *StringBuilder {
1313
}
1414

1515
// Creates a new instance of the StringBuilder with a preallocated text
16-
func NewFromString(text string) *StringBuilder {
16+
func NewStringBuilderFromString(text string) *StringBuilder {
1717
return &StringBuilder{
1818
data: []rune(text),
1919
position: len(text),
@@ -105,6 +105,12 @@ func (s *StringBuilder) Insert(index int, text string) error {
105105
return nil
106106
}
107107

108+
// Removes all characters from the current instance. This sets the internal size to 0.
109+
// The internal array will stay the same.
110+
func (s *StringBuilder) Clear() {
111+
s.position = 0
112+
}
113+
108114
func (s *StringBuilder) grow(lenToAdd int) {
109115
// Grow times 2 until lenToAdd fits
110116
newLen := cap(s.data)

stringbuilder_test.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ func TestToStringEmptyBuilder(t *testing.T) {
5959
func TestNewFromString(t *testing.T) {
6060
const expected string = "Hello"
6161

62-
sb := NewFromString(expected)
62+
sb := NewStringBuilderFromString(expected)
6363

6464
if result := sb.ToString(); result != expected {
6565
t.Errorf("Actual %q, Expected: %q", result, expected)
6666
}
6767
}
6868

6969
func TestRemovePartOfString(t *testing.T) {
70-
sb := NewFromString("Hello")
70+
sb := NewStringBuilderFromString("Hello")
7171

7272
if err := sb.Remove(3, 2); err != nil {
7373
t.Errorf("Remove threw an error: %v", err)
@@ -79,31 +79,31 @@ func TestRemovePartOfString(t *testing.T) {
7979
}
8080

8181
func TestRemoveWhenStartIndexOutOfBounds(t *testing.T) {
82-
sb := NewFromString("Hello")
82+
sb := NewStringBuilderFromString("Hello")
8383

8484
if err := sb.Remove(100, 1); err == nil {
8585
t.Error("Should throw error but did not")
8686
}
8787
}
8888

8989
func TestRemoveWhenStartIndexNegative(t *testing.T) {
90-
sb := NewFromString("Hello")
90+
sb := NewStringBuilderFromString("Hello")
9191

9292
if err := sb.Remove(-1, 1); err == nil {
9393
t.Error("Should throw error but did not")
9494
}
9595
}
9696

9797
func TestRemoveWhenLengthNegative(t *testing.T) {
98-
sb := NewFromString("Hello")
98+
sb := NewStringBuilderFromString("Hello")
9999

100100
if err := sb.Remove(1, -1); err == nil {
101101
t.Error("Should throw error but did not")
102102
}
103103
}
104104

105105
func TestRemoveWhenEndIndexOutOfBounds(t *testing.T) {
106-
sb := NewFromString("Hello")
106+
sb := NewStringBuilderFromString("Hello")
107107

108108
if err := sb.Remove(4, 4); err == nil {
109109
t.Error("Should throw error but did not")
@@ -112,7 +112,7 @@ func TestRemoveWhenEndIndexOutOfBounds(t *testing.T) {
112112

113113
func TestRemoveWhenLengthZero(t *testing.T) {
114114
const expected string = "Hello"
115-
sb := NewFromString(expected)
115+
sb := NewStringBuilderFromString(expected)
116116

117117
if err := sb.Remove(0, 0); err != nil {
118118
t.Errorf("Remove threw an error: %v", err)
@@ -125,7 +125,7 @@ func TestRemoveWhenLengthZero(t *testing.T) {
125125

126126
func TestInsertAtIndex(t *testing.T) {
127127
const expected string = "Hello my dear and beautiful World"
128-
sb := NewFromString("Hello World")
128+
sb := NewStringBuilderFromString("Hello World")
129129

130130
if err := sb.Insert(5, " my dear and beautiful"); err != nil {
131131
t.Errorf("Insert threw an error: %v", err)
@@ -151,3 +151,13 @@ func TestInsertShouldThrowErrirIfOutOfRange(t *testing.T) {
151151
t.Error("Should throw error but did not")
152152
}
153153
}
154+
155+
func TestClear(t *testing.T) {
156+
sb := NewStringBuilderFromString("Hello")
157+
158+
sb.Clear()
159+
160+
if result := sb.ToString(); result != "" {
161+
t.Errorf("Expected empty string but did receive %v", result)
162+
}
163+
}

0 commit comments

Comments
 (0)