Skip to content

Commit d60c49e

Browse files
committed
feat: Added initial type and first functions
1 parent 12f8f67 commit d60c49e

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/linkdotnet/golang-stringbuilder
2+
3+
go 1.19

stringbuilder.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package Text
2+
3+
type StringBuilder struct {
4+
data []rune
5+
position int
6+
}
7+
8+
// Creates a new instance of the StringBuilder with preallocated array
9+
func NewStringBuilder(initialCapacity int) *StringBuilder {
10+
return &StringBuilder{data: make([]rune, initialCapacity)}
11+
}
12+
13+
// Appends a text to the StringBuilder instance
14+
func (s *StringBuilder) Append(text string) {
15+
newLen := s.position + len(text)
16+
if newLen > cap(s.data) {
17+
s.grow(newLen)
18+
}
19+
20+
copy(s.data[s.position:], []rune(text))
21+
s.position = newLen
22+
}
23+
24+
// Appends a text and a new line character to the StringBuilder instance
25+
func (s *StringBuilder) AppendLine(text string) {
26+
s.Append(text)
27+
s.Append("\n")
28+
}
29+
30+
// Appends a single character to the StringBuilder instance
31+
func (s *StringBuilder) AppendRune(char rune) {
32+
newLen := s.position + 1
33+
if newLen > cap(s.data) {
34+
s.grow(newLen)
35+
}
36+
37+
s.data[s.position] = char
38+
s.position++
39+
}
40+
41+
// Returns the current length of the represented string
42+
func (s *StringBuilder) Len() int {
43+
return s.position
44+
}
45+
46+
// Returns the represented string
47+
func (s *StringBuilder) ToString() string {
48+
return string(s.data[:s.position])
49+
}
50+
51+
func (s *StringBuilder) grow(lenToAdd int) {
52+
// Grow times 2 until lenToAdd fits
53+
newLen := cap(s.data)
54+
55+
if cap(s.data) == 0 {
56+
newLen = 8
57+
}
58+
59+
for newLen < lenToAdd {
60+
newLen = newLen * 2
61+
}
62+
63+
new := make([]rune, newLen)
64+
copy(new, s.data)
65+
s.data = new
66+
}

stringbuilder_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package Text
2+
3+
import "testing"
4+
5+
func TestAppend(t *testing.T) {
6+
const expected string = "Hello World"
7+
sb := StringBuilder{}
8+
9+
sb.Append(expected)
10+
11+
if result := sb.ToString(); result != expected {
12+
t.Errorf("Actual %q, Expected: %q", result, expected)
13+
}
14+
}
15+
16+
func TestLen(t *testing.T) {
17+
sb := StringBuilder{}
18+
19+
sb.Append("1234")
20+
21+
if len := sb.Len(); len != 4 {
22+
t.Errorf("Actual %q, Expected: %q", len, 4)
23+
}
24+
}
25+
26+
func TestAppendLine(t *testing.T) {
27+
const expected string = "Hello World\n"
28+
sb := NewStringBuilder(100)
29+
30+
sb.AppendLine("Hello World")
31+
32+
if result := sb.ToString(); result != expected {
33+
t.Errorf("Actual %q, Expected: %q", result, expected)
34+
}
35+
}
36+
37+
func TestAppendRune(t *testing.T) {
38+
const expected string = "Hello"
39+
sb := NewStringBuilder(100)
40+
41+
sb.Append("Hell")
42+
sb.AppendRune('o')
43+
44+
if result := sb.ToString(); result != expected {
45+
t.Errorf("Actual %q, Expected: %q", result, expected)
46+
}
47+
}

0 commit comments

Comments
 (0)