Skip to content

Commit b5a4374

Browse files
committed
Revert chaining in functions that can return an error
1 parent 06cb328 commit b5a4374

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

stringbuilder.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,41 +66,41 @@ func (s *StringBuilder) ToString() string {
6666
return string(s.data[:s.position])
6767
}
6868

69-
func (s *StringBuilder) Remove(start int, length int) (*StringBuilder, error) {
69+
func (s *StringBuilder) Remove(start int, length int) error {
7070
if start >= s.position {
71-
return s, fmt.Errorf("start is after the end of the string")
71+
return fmt.Errorf("start is after the end of the string")
7272
}
7373
if start < 0 {
74-
return s, fmt.Errorf("start can't be a negative value")
74+
return fmt.Errorf("start can't be a negative value")
7575
}
7676
if length < 0 {
77-
return s, fmt.Errorf("length can't be a negative value")
77+
return fmt.Errorf("length can't be a negative value")
7878
}
7979

8080
endIndex := start + length - 1
8181

8282
if endIndex > s.position {
83-
return s, fmt.Errorf("can't delete after the end of the string")
83+
return fmt.Errorf("can't delete after the end of the string")
8484
}
8585

8686
if length == 0 {
87-
return s, nil
87+
return nil
8888
}
8989

9090
x := start + length
9191
copy(s.data[start:], s.data[x:])
9292
s.position -= length
9393

94-
return s, nil
94+
return nil
9595
}
9696

97-
func (s *StringBuilder) Insert(index int, text string) (*StringBuilder, error) {
97+
func (s *StringBuilder) Insert(index int, text string) error {
9898
if index < 0 {
99-
return s, fmt.Errorf("index can't be negative")
99+
return fmt.Errorf("index can't be negative")
100100
}
101101

102102
if index > s.position {
103-
return s, fmt.Errorf("can't write outside the buffer")
103+
return fmt.Errorf("can't write outside the buffer")
104104
}
105105

106106
runeText := []rune(text)
@@ -112,7 +112,7 @@ func (s *StringBuilder) Insert(index int, text string) (*StringBuilder, error) {
112112
s.data = append(s.data[:index], append(runeText, s.data[index:]...)...)
113113
s.position = newLen
114114

115-
return s, nil
115+
return nil
116116
}
117117

118118
// Removes all characters from the current instance. This sets the internal size to 0.

stringbuilder_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestNewFromString(t *testing.T) {
9292
func TestRemovePartOfString(t *testing.T) {
9393
sb := NewStringBuilderFromString("Hello")
9494

95-
if _, err := sb.Remove(3, 2); err != nil {
95+
if err := sb.Remove(3, 2); err != nil {
9696
t.Errorf("Remove threw an error: %v", err)
9797
}
9898

@@ -104,31 +104,31 @@ func TestRemovePartOfString(t *testing.T) {
104104
func TestRemoveWhenStartIndexOutOfBounds(t *testing.T) {
105105
sb := NewStringBuilderFromString("Hello")
106106

107-
if _, err := sb.Remove(100, 1); err == nil {
107+
if err := sb.Remove(100, 1); err == nil {
108108
t.Error("Should throw error but did not")
109109
}
110110
}
111111

112112
func TestRemoveWhenStartIndexNegative(t *testing.T) {
113113
sb := NewStringBuilderFromString("Hello")
114114

115-
if _, err := sb.Remove(-1, 1); err == nil {
115+
if err := sb.Remove(-1, 1); err == nil {
116116
t.Error("Should throw error but did not")
117117
}
118118
}
119119

120120
func TestRemoveWhenLengthNegative(t *testing.T) {
121121
sb := NewStringBuilderFromString("Hello")
122122

123-
if _, err := sb.Remove(1, -1); err == nil {
123+
if err := sb.Remove(1, -1); err == nil {
124124
t.Error("Should throw error but did not")
125125
}
126126
}
127127

128128
func TestRemoveWhenEndIndexOutOfBounds(t *testing.T) {
129129
sb := NewStringBuilderFromString("Hello")
130130

131-
if _, err := sb.Remove(4, 4); err == nil {
131+
if err := sb.Remove(4, 4); err == nil {
132132
t.Error("Should throw error but did not")
133133
}
134134
}
@@ -137,7 +137,7 @@ func TestRemoveWhenLengthZero(t *testing.T) {
137137
const expected string = "Hello"
138138
sb := NewStringBuilderFromString(expected)
139139

140-
if _, err := sb.Remove(0, 0); err != nil {
140+
if err := sb.Remove(0, 0); err != nil {
141141
t.Errorf("Remove threw an error: %v", err)
142142
}
143143

@@ -150,7 +150,7 @@ func TestRemoveInTheMiddle(t *testing.T) {
150150
const expected string = "Hlo World"
151151
sb := NewStringBuilderFromString("Hello World")
152152

153-
if _, err := sb.Remove(1, 2); err != nil {
153+
if err := sb.Remove(1, 2); err != nil {
154154
t.Errorf("Remove threw an error: %v", err)
155155
}
156156

@@ -163,7 +163,7 @@ func TestInsertAtIndex(t *testing.T) {
163163
const expected string = "Hello my dear and beautiful World"
164164
sb := NewStringBuilderFromString("Hello World")
165165

166-
if _, err := sb.Insert(5, " my dear and beautiful"); err != nil {
166+
if err := sb.Insert(5, " my dear and beautiful"); err != nil {
167167
t.Errorf("Insert threw an error: %v", err)
168168
}
169169

@@ -175,15 +175,15 @@ func TestInsertAtIndex(t *testing.T) {
175175
func TestInsertShouldThrowIfNegativeIndex(t *testing.T) {
176176
sb := StringBuilder{}
177177

178-
if _, err := sb.Insert(-1, "Test"); err == nil {
178+
if err := sb.Insert(-1, "Test"); err == nil {
179179
t.Error("Should throw error but did not")
180180
}
181181
}
182182

183183
func TestInsertShouldThrowErrirIfOutOfRange(t *testing.T) {
184184
sb := StringBuilder{}
185185

186-
if _, err := sb.Insert(1, "Test"); err == nil {
186+
if err := sb.Insert(1, "Test"); err == nil {
187187
t.Error("Should throw error but did not")
188188
}
189189
}

0 commit comments

Comments
 (0)