Skip to content

Commit 05c0f12

Browse files
committed
fix: Correct edge checks for grow
1 parent 56b36d7 commit 05c0f12

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

CHANGELOG.md

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

77
## [Unreleased]
88

9-
## [0.8.0] - 2023-08-24
10-
119
### Added
1210
- `Trim`, `TrimStart`, `TrimEnd` are added to the string builder
1311

12+
### Fixed
13+
- Sometimes the grow mechanism did not work properly leading to a panic
14+
15+
## [0.8.0] - 2023-08-24
16+
1417
### Added
1518

1619
- The string builder can now be used to append integers, boolean & collection of strings. `sb.AppendInt(1).AppendBool(true).AppendList([]string{"a", "b", "c"})`

stringbuilder.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (s *StringBuilder) AppendLine(text string) *StringBuilder {
4545
// Appends a single character to the StringBuilder instance
4646
func (s *StringBuilder) AppendRune(char rune) *StringBuilder {
4747
newLen := s.position + 1
48-
if newLen > cap(s.data) {
48+
if newLen >= cap(s.data) {
4949
s.grow(newLen)
5050
}
5151
s.data[s.position] = char
@@ -79,7 +79,7 @@ func (s *StringBuilder) resize(words ...string) {
7979
allWordLength += len(word)
8080
}
8181
newLen := s.position + allWordLength
82-
if newLen > cap(s.data) {
82+
if newLen >= cap(s.data) {
8383
s.grow(newLen)
8484
}
8585
}
@@ -274,6 +274,12 @@ func (s *StringBuilder) TrimEnd(chars ...rune) *StringBuilder {
274274
return s
275275
}
276276

277+
// Returns the internal array of the string builder. Be careful as this returns the internal slice.
278+
// Changes to that will reflect in this string builder instance.
279+
func (s *StringBuilder) AsRuneArray() []rune {
280+
return s.data
281+
}
282+
277283
func (s *StringBuilder) grow(lenToAdd int) {
278284
// Grow times 2 until lenToAdd fits
279285
newLen := len(s.data)

0 commit comments

Comments
 (0)