@@ -240,25 +240,13 @@ func (s *StringBuilder) Trim(chars ...rune) *StringBuilder {
240
240
// Trims the given characters from the start of the string builder or all whitespaces if no characters are given
241
241
func (s * StringBuilder ) TrimStart (chars ... rune ) * StringBuilder {
242
242
start := 0
243
- trimSet := make ( map [ rune ] bool )
243
+ trimSet := createTrimSet ( chars ... )
244
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 ++
245
+ for _ , ch := range s .data [:s .position ] {
246
+ if _ , exists := trimSet [ch ]; ! exists {
247
+ break
261
248
}
249
+ start ++
262
250
}
263
251
264
252
if start > 0 {
@@ -272,25 +260,13 @@ func (s *StringBuilder) TrimStart(chars ...rune) *StringBuilder {
272
260
// Trims the given characters from the start of the string builder or all whitespaces if no characters are given
273
261
func (s * StringBuilder ) TrimEnd (chars ... rune ) * StringBuilder {
274
262
end := s .position
275
- trimSet := make ( map [ rune ] bool )
263
+ trimSet := createTrimSet ( chars ... )
276
264
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
293
268
}
269
+ end --
294
270
}
295
271
296
272
s .position = end
@@ -313,6 +289,19 @@ func (s *StringBuilder) grow(lenToAdd int) {
313
289
s .data = append (s .data , make ([]rune , newLen - len (s .data ))... )
314
290
}
315
291
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
318
307
}
0 commit comments