Skip to content

Commit 18c782f

Browse files
g41797linkdotnet
authored andcommitted
Add APIs for reusing
1 parent 1893430 commit 18c782f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

stringbuilder.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,38 @@ func createTrimSet(chars ...rune) map[rune]bool {
336336

337337
return trimSet
338338
}
339+
340+
// Set initial position
341+
func (s *StringBuilder) Rewind() {
342+
s.position = 0
343+
}
344+
345+
// Sets the rune at the specific position
346+
func (s *StringBuilder) SetRuneAt(index int, val rune) error {
347+
if index < 0 {
348+
return fmt.Errorf("index should always be greater than or equal to zero")
349+
}
350+
if index > s.position {
351+
return fmt.Errorf("index cannot be greater than current position")
352+
}
353+
s.data[index] = val
354+
355+
return nil
356+
}
357+
358+
// Change current position
359+
func (s *StringBuilder) Skip(forward int) error {
360+
if forward <= 0 {
361+
return fmt.Errorf("forward should always be greater than zero")
362+
}
363+
364+
newPos := s.position + forward
365+
366+
if newPos >= len(s.data) {
367+
return fmt.Errorf("cannot skip after end of string builder")
368+
}
369+
370+
s.position = newPos
371+
372+
return nil
373+
}

0 commit comments

Comments
 (0)