@@ -240,25 +240,13 @@ func (s *StringBuilder) Trim(chars ...rune) *StringBuilder {
240240// Trims the given characters from the start of the string builder or all whitespaces if no characters are given
241241func (s * StringBuilder ) TrimStart (chars ... rune ) * StringBuilder {
242242 start := 0
243- trimSet := make ( map [ rune ] bool )
243+ trimSet := createTrimSet ( chars ... )
244244
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 ++
245+ for _ , ch := range s .data [:s .position ] {
246+ if _ , exists := trimSet [ch ]; ! exists {
247+ break
261248 }
249+ start ++
262250 }
263251
264252 if start > 0 {
@@ -272,25 +260,13 @@ func (s *StringBuilder) TrimStart(chars ...rune) *StringBuilder {
272260// Trims the given characters from the start of the string builder or all whitespaces if no characters are given
273261func (s * StringBuilder ) TrimEnd (chars ... rune ) * StringBuilder {
274262 end := s .position
275- trimSet := make ( map [ rune ] bool )
263+ trimSet := createTrimSet ( chars ... )
276264
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 --
265+ for i := s .position - 1 ; i >= 0 ; i -- {
266+ if _ , exists := trimSet [s.data [i ]]; ! exists {
267+ break
293268 }
269+ end --
294270 }
295271
296272 s .position = end
@@ -313,6 +289,19 @@ func (s *StringBuilder) grow(lenToAdd int) {
313289 s .data = append (s .data , make ([]rune , newLen - len (s .data ))... )
314290}
315291
316- func isWhitespace (ch rune ) bool {
317- return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'
292+ func createTrimSet (chars ... rune ) map [rune ]bool {
293+ trimSet := make (map [rune ]bool )
294+
295+ if len (chars ) == 0 {
296+ trimSet [' ' ] = true
297+ trimSet ['\t' ] = true
298+ trimSet ['\n' ] = true
299+ trimSet ['\r' ] = true
300+ } else {
301+ for _ , ch := range chars {
302+ trimSet [ch ] = true
303+ }
304+ }
305+
306+ return trimSet
318307}
0 commit comments