Skip to content

Commit 3f4929f

Browse files
authored
Added Substring StringBuilder functionality (#8)
* Added Substring StringBuilder functionality * Added start greater than end boundary check
1 parent a8b58b6 commit 3f4929f

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
99
### Added
1010

1111
- `Reverse` is added to the string builder
12+
- `Substring` is added to the string builder
1213

1314
## [0.9.0] - 2023-08-26
1415

stringbuilder.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,22 @@ func (s *StringBuilder) Reverse() *StringBuilder {
289289
return s
290290
}
291291

292+
// Returns a substring from start (inclusive) to end (exclusive).
293+
func (s *StringBuilder) Substring(start, end int) (string, error) {
294+
if start < 0 {
295+
return "", fmt.Errorf("start should always be greater than or equal to zero")
296+
}
297+
if end > s.position {
298+
return "", fmt.Errorf("end cannot be greater than the length of string builder")
299+
}
300+
if start > end {
301+
return "", fmt.Errorf("start cannot be greater than the end for Substring() function")
302+
}
303+
r := make([]rune, end-start)
304+
copy(r, s.data[start:end])
305+
return string(r), nil
306+
}
307+
292308
func (s *StringBuilder) grow(lenToAdd int) {
293309
// Grow times 2 until lenToAdd fits
294310
newLen := len(s.data)

stringbuilder_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,40 @@ func TestReuseReversedStringBuilder(t *testing.T) {
444444
}
445445
}
446446

447+
func TestStringBuilderSubstring(t *testing.T) {
448+
tests := []struct {
449+
name string
450+
start int
451+
end int
452+
substring string
453+
errorMessage string
454+
}{
455+
{"Substring with negative start", -1, 3, "", "start should always be greater than or equal to zero"},
456+
{"Substring with end exceeding string builder length", 0, 5, "", "end cannot be greater than the length of string builder"},
457+
{"Substring with start greater than end", 3, 2, "", "start cannot be greater than the end for Substring() function"},
458+
{"Substring with start equal to zero", 0, 3, "abc", ""},
459+
{"Substring with end equal to length of string builder", 0, 4, "abcd", ""},
460+
{"Substring of length 1", 0, 1, "a", ""},
461+
{"Substring of length 0", 0, 0, "", ""},
462+
{"Substring in middle of string builder", 1, 3, "bc", ""},
463+
}
464+
for _, tt := range tests {
465+
t.Run(tt.name, func(t *testing.T) {
466+
sb := NewStringBuilderFromString("abcd")
467+
s, err := sb.Substring(tt.start, tt.end)
468+
if err != nil {
469+
if err.Error() != tt.errorMessage {
470+
t.Errorf("StringBuilder.Substring() expected error message = %v, got = %v", tt.errorMessage, err.Error())
471+
}
472+
473+
}
474+
if s != tt.substring {
475+
t.Errorf("StringBuilder.Substring() expected substring = %v, got = %v", tt.substring, s)
476+
}
477+
})
478+
}
479+
}
480+
447481
func slicesEqual(a []int, b []int) bool {
448482
if len(a) != len(b) {
449483
return false

0 commit comments

Comments
 (0)