Skip to content

Commit 94a4971

Browse files
committed
Added Trim methods
1 parent bd1cce4 commit 94a4971

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

CHANGELOG.md

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

99
## [0.8.0] - 2023-08-24
1010

11+
### Added
12+
- `Trim`, `TrimStart`, `TrimEnd` are added to the string builder
13+
1114
### Added
1215

1316
- 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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,72 @@ func (s *StringBuilder) Write(p []byte) (int, error) {
232232
return delta, nil
233233
}
234234

235+
// Trims the given characters from the start and end of the string builder or all whitespaces if no characters are given
236+
func (s *StringBuilder) Trim(chars ...rune) *StringBuilder {
237+
return s.TrimStart(chars...).TrimEnd(chars...)
238+
}
239+
240+
// Trims the given characters from the start of the string builder or all whitespaces if no characters are given
241+
func (s *StringBuilder) TrimStart(chars ...rune) *StringBuilder {
242+
start := 0
243+
trimSet := make(map[rune]bool)
244+
245+
if len(chars) == 0 {
246+
for _, ch := range s.data[:s.position] {
247+
if !isWhitespace(ch) {
248+
break
249+
}
250+
start++
251+
}
252+
} else {
253+
for _, ch := range chars {
254+
trimSet[ch] = true
255+
}
256+
for _, ch := range s.data[:s.position] {
257+
if _, exists := trimSet[ch]; !exists {
258+
break
259+
}
260+
start++
261+
}
262+
}
263+
264+
if start > 0 {
265+
copy(s.data, s.data[start:s.position])
266+
s.position -= start
267+
}
268+
269+
return s
270+
}
271+
272+
// Trims the given characters from the start of the string builder or all whitespaces if no characters are given
273+
func (s *StringBuilder) TrimEnd(chars ...rune) *StringBuilder {
274+
end := s.position
275+
trimSet := make(map[rune]bool)
276+
277+
if len(chars) == 0 {
278+
for i := s.position - 1; i >= 0; i-- {
279+
if !isWhitespace(s.data[i]) {
280+
break
281+
}
282+
end--
283+
}
284+
} else {
285+
for _, ch := range chars {
286+
trimSet[ch] = true
287+
}
288+
for i := s.position - 1; i >= 0; i-- {
289+
if _, exists := trimSet[s.data[i]]; !exists {
290+
break
291+
}
292+
end--
293+
}
294+
}
295+
296+
s.position = end
297+
298+
return s
299+
}
300+
235301
func (s *StringBuilder) grow(lenToAdd int) {
236302
// Grow times 2 until lenToAdd fits
237303
newLen := len(s.data)
@@ -246,3 +312,7 @@ func (s *StringBuilder) grow(lenToAdd int) {
246312

247313
s.data = append(s.data, make([]rune, newLen-len(s.data))...)
248314
}
315+
316+
func isWhitespace(ch rune) bool {
317+
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'
318+
}

stringbuilder_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,36 @@ func TestWriteReturnsAddedAmount(t *testing.T) {
377377
}
378378
}
379379

380+
func TestTrimStartWithWhitespaces(t *testing.T) {
381+
s := NewStringBuilderFromString(" Hello World")
382+
383+
s.TrimStart()
384+
385+
if got := s.ToString(); got != "Hello World" {
386+
t.Errorf("StringBuilder.TrimStart() = %v, want %v", got, "Hello World")
387+
}
388+
}
389+
390+
func TestTrimStartWithGivenCharacters(t *testing.T) {
391+
s := NewStringBuilderFromString("aHello World")
392+
393+
s.TrimStart('a')
394+
395+
if got := s.ToString(); got != "Hello World" {
396+
t.Errorf("StringBuilder.TrimStart() = %v, want %v", got, "Hello World")
397+
}
398+
}
399+
400+
func TestTrimWithWhitespacesAtTheStartAndEnd(t *testing.T) {
401+
s := NewStringBuilderFromString(" Hello World ")
402+
403+
s.Trim()
404+
405+
if got := s.ToString(); got != "Hello World" {
406+
t.Errorf("StringBuilder.Trim() = %v, want %v", got, "Hello World")
407+
}
408+
}
409+
380410
func slicesEqual(a []int, b []int) bool {
381411
if len(a) != len(b) {
382412
return false

0 commit comments

Comments
 (0)