Skip to content

Commit 0c28af8

Browse files
committed
Added Remove and FromString
1 parent a304b6b commit 0c28af8

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

stringbuilder.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package Text
22

3+
import "fmt"
4+
35
type StringBuilder struct {
46
data []rune
57
position int
@@ -10,6 +12,14 @@ func NewStringBuilder(initialCapacity int) *StringBuilder {
1012
return &StringBuilder{data: make([]rune, initialCapacity)}
1113
}
1214

15+
// Creates a new instance of the StringBuilder with a preallocated text
16+
func NewFromString(text string) *StringBuilder {
17+
return &StringBuilder{
18+
data: []rune(text),
19+
position: len(text),
20+
}
21+
}
22+
1323
// Appends a text to the StringBuilder instance
1424
func (s *StringBuilder) Append(text string) {
1525
newLen := s.position + len(text)
@@ -48,6 +58,33 @@ func (s *StringBuilder) ToString() string {
4858
return string(s.data[:s.position])
4959
}
5060

61+
func (s *StringBuilder) Remove(start int, length int) error {
62+
if start >= s.position {
63+
return fmt.Errorf("start is after the end of the string")
64+
}
65+
if start < 0 {
66+
return fmt.Errorf("start can't be a negative value")
67+
}
68+
if length < 0 {
69+
return fmt.Errorf("length can't be a negative value")
70+
}
71+
72+
endIndex := start + length - 1
73+
74+
if endIndex > s.position {
75+
return fmt.Errorf("can't delete after the end of the string")
76+
}
77+
78+
if length == 0 {
79+
return nil
80+
}
81+
82+
s.data = append(s.data[:start], s.data[endIndex])
83+
s.position -= length
84+
85+
return nil
86+
}
87+
5188
func (s *StringBuilder) grow(lenToAdd int) {
5289
// Grow times 2 until lenToAdd fits
5390
newLen := cap(s.data)

stringbuilder_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,65 @@ func TestAppendRune(t *testing.T) {
4545
t.Errorf("Actual %q, Expected: %q", result, expected)
4646
}
4747
}
48+
49+
func TestToStringEmptyBuilder(t *testing.T) {
50+
sb := StringBuilder{}
51+
52+
if result := sb.ToString(); result != "" {
53+
t.Errorf("String should be empty but was: %q", result)
54+
}
55+
}
56+
57+
func TestNewFromString(t *testing.T) {
58+
const expected string = "Hello"
59+
60+
sb := NewFromString(expected)
61+
62+
if result := sb.ToString(); result != expected {
63+
t.Errorf("Actual %q, Expected: %q", result, expected)
64+
}
65+
}
66+
67+
func TestRemovePartOfString(t *testing.T) {
68+
sb := NewFromString("Hello")
69+
70+
if err := sb.Remove(3, 2); err != nil {
71+
t.Errorf("Remove threw an error: %v", err)
72+
}
73+
74+
if result := sb.ToString(); result != "Hel" {
75+
t.Errorf("Actual %q, Expected: %q", result, "Hel")
76+
}
77+
}
78+
79+
func TestRemoveWhenStartIndexOutOfBounds(t *testing.T) {
80+
sb := NewFromString("Hello")
81+
82+
if err := sb.Remove(100, 1); err == nil {
83+
t.Error("Should throw error but did not")
84+
}
85+
}
86+
87+
func TestRemoveWhenStartIndexNegative(t *testing.T) {
88+
sb := NewFromString("Hello")
89+
90+
if err := sb.Remove(-1, 1); err == nil {
91+
t.Error("Should throw error but did not")
92+
}
93+
}
94+
95+
func TestRemoveWhenLengthNegative(t *testing.T) {
96+
sb := NewFromString("Hello")
97+
98+
if err := sb.Remove(1, -1); err == nil {
99+
t.Error("Should throw error but did not")
100+
}
101+
}
102+
103+
func TestRemoveWhenEndIndexOutOfBounds(t *testing.T) {
104+
sb := NewFromString("Hello")
105+
106+
if err := sb.Remove(4, 4); err == nil {
107+
t.Error("Should throw error but did not")
108+
}
109+
}

0 commit comments

Comments
 (0)