Skip to content

Commit 46e8c45

Browse files
committed
Handle umlauts in Append
1 parent d9f915e commit 46e8c45

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
66

77
## [Unreleased]
88

9+
### Fixed
10+
- Fixed a bug where `Append` and `AppendLine` could not handle umlauts
11+
912
## [0.5.1] - 2022-12-27
1013

1114
### Fixed

stringbuilder.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ func NewStringBuilderFromString(text string) *StringBuilder {
2222

2323
// Appends a text to the StringBuilder instance
2424
func (s *StringBuilder) Append(text string) {
25-
newLen := s.position + len(text)
25+
textRunes := []rune(text)
26+
newLen := s.position + len(textRunes)
2627
if newLen > cap(s.data) {
2728
s.grow(newLen)
2829
}
2930

30-
copy(s.data[s.position:], []rune(text))
31+
copy(s.data[s.position:], textRunes)
3132
s.position = newLen
3233
}
3334

stringbuilder_test.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,44 @@ import (
66
)
77

88
func TestAppend(t *testing.T) {
9-
const expected string = "Hello World"
10-
sb := StringBuilder{}
9+
tests := []struct {
10+
want string
11+
}{
12+
{"Hello World"},
13+
{"Hallöchen"},
14+
}
15+
for _, tt := range tests {
16+
t.Run(tt.want, func(t *testing.T) {
17+
s := &StringBuilder{}
1118

12-
sb.Append(expected)
19+
s.Append(tt.want)
1320

14-
if result := sb.ToString(); result != expected {
15-
t.Errorf("Actual %q, Expected: %q", result, expected)
21+
if got := s.ToString(); got != tt.want {
22+
t.Errorf("StringBuilder.Append() = %v, want %v", got, tt.want)
23+
}
24+
})
1625
}
1726
}
1827

1928
func TestLen(t *testing.T) {
20-
sb := StringBuilder{}
29+
tests := []struct {
30+
name string
31+
input string
32+
want int
33+
}{
34+
{"English word", "Hello", 5},
35+
{"Word with Umlaut", "Hallöchen", 9},
36+
}
37+
for _, tt := range tests {
38+
t.Run(tt.name, func(t *testing.T) {
39+
s := &StringBuilder{}
2140

22-
sb.Append("1234")
41+
s.Append(tt.input)
2342

24-
if len := sb.Len(); len != 4 {
25-
t.Errorf("Actual %q, Expected: %q", len, 4)
43+
if got := s.Len(); got != tt.want {
44+
t.Errorf("StringBuilder.Append() = %v, want %v", got, tt.want)
45+
}
46+
})
2647
}
2748
}
2849

@@ -58,7 +79,7 @@ func TestToStringEmptyBuilder(t *testing.T) {
5879
}
5980

6081
func TestNewFromString(t *testing.T) {
61-
const expected string = "Hello"
82+
const expected string = "Hellöchen"
6283

6384
sb := NewStringBuilderFromString(expected)
6485

@@ -298,6 +319,7 @@ func TestReplace(t *testing.T) {
298319
{"Replace Hello with Hallochen", "Hello World", "Hello", "Hallochen", "Hallochen World"},
299320
{"Replace Hello with Hallöchen", "Hello World", "Hello", "Hallöchen", "Hallöchen World"},
300321
{"Replace ö with ä", "äö", "ö", "ä", "ää"},
322+
{"Replace with same word", "Hello", "llo", "llo", "Hello"},
301323
}
302324
for _, tt := range tests {
303325
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)