Skip to content

Commit ed68d8c

Browse files
committed
Add strip, stripLeft, stripRight extensions
1 parent 45ed0a3 commit ed68d8c

2 files changed

Lines changed: 172 additions & 0 deletions

File tree

lib/src/StringUtils.dart

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,58 @@ class StringUtils {
541541
return false;
542542
}
543543
}
544+
545+
extension Strip on String {
546+
/// Implementation of strip, stripLeft and stripRight
547+
String _strip(
548+
String stripChars, {
549+
bool left = true,
550+
bool right = true,
551+
}) {
552+
if (!left && !right) {
553+
throw ArgumentError('either left or right shall be true in _strip: <$this>');
554+
}
555+
int start = 0;
556+
int end = length;
557+
List<int> sourceChars = runes.toList();
558+
List<int> searchChars = stripChars.runes.toList();
559+
if (left) {
560+
for (int i = 0; i < sourceChars.length; i++) {
561+
if (searchChars.contains(sourceChars[i])) {
562+
start++;
563+
} else {
564+
break;
565+
}
566+
}
567+
}
568+
if (right) {
569+
for (int i = sourceChars.length - 1; i >= 0; i--) {
570+
if (searchChars.contains(sourceChars[i])) {
571+
end--;
572+
} else {
573+
break;
574+
}
575+
}
576+
}
577+
578+
if (start >= end) {
579+
return '';
580+
}
581+
return substring(start, end);
582+
}
583+
584+
/// Return a string after removing any leading and trailing characters specified in [chars]
585+
///
586+
/// '@£^£Some @£^ Thing@@£@^'.strip('@£^') => 'Some @£^ Thing'
587+
String strip(String chars) => _strip(chars, left: true, right: true);
588+
589+
/// Return a string after removing any leading characters specified in [chars]
590+
///
591+
/// '@£^£Some @£^ Thing@@£@^'.stripLeft('@£^') => 'Some @£^ Thing@@£@^'
592+
String stripLeft(String chars) => _strip(chars, left: true, right: false);
593+
594+
/// Return a string after removing any trailing characters specified in [chars]
595+
///
596+
/// '@£^£Some @£^ Thing@@£@^'.strip('@£^') => '@£^£Some @£^ Thing'
597+
String stripRight(String chars) => _strip(chars, left: false, right: true);
598+
}

test/string_utils_test.dart

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,121 @@ void main() {
219219
expect(StringUtils.isIP('0.0.0.256'), false);
220220
expect(StringUtils.isIP('26.0.0.256'), false);
221221
});
222+
223+
group('strip extension', () {
224+
test('strip', () {
225+
// tested function's comment
226+
expect('@£^£Some @£^ Thing@@£@^'.strip('@£^'), 'Some @£^ Thing');
227+
228+
// edge cases that doesn't modify the string
229+
expect('something'.strip('@'), 'something');
230+
expect('something'.strip('@£^'), 'something');
231+
expect('something'.strip(''), 'something');
232+
233+
// remove a single character at either end from the source
234+
expect('@something'.strip('@'), 'something');
235+
expect('@something@'.strip('@'), 'something');
236+
expect('something@'.strip('@'), 'something');
237+
238+
// remove more than one character at either end from the source
239+
expect('@@@something'.strip('@'), 'something');
240+
expect('@@something@@@'.strip('@'), 'something');
241+
expect('something@@@'.strip('@'), 'something');
242+
243+
// search-string with multiple characters in it
244+
expect('@£something'.strip('@£'), 'something');
245+
expect('@£something@£^'.strip('@£^'), 'something');
246+
expect('something@£^'.strip('@£^'), 'something');
247+
248+
// source string with strip-chars in the middle
249+
expect('@£some@£^thing'.strip('@£'), 'some@£^thing');
250+
expect('@£some@£^thing@£@'.strip('@£'), 'some@£^thing');
251+
expect('some@£^thing@£@'.strip('@£'), 'some@£^thing');
252+
253+
// strip returns an empty string
254+
expect('@@@@@£^^£@£'.strip('@£^'), '');
255+
expect(''.strip('@£^'), '');
256+
expect(''.strip(''), '');
257+
expect('2'.strip('2'), '');
258+
259+
// results in a string with one character in it
260+
expect('£@£@£@£@3@££££@^'.strip('£@^'), '3');
261+
});
262+
test('stripLeft', () {
263+
// tested function's comment
264+
expect('@£^£Some @£^ Thing@@£@^'.stripLeft('@£^'), 'Some @£^ Thing@@£@^');
265+
266+
// edge cases that doesn't modify the string
267+
expect('something'.stripLeft('@'), 'something');
268+
expect('something'.stripLeft('@£^'), 'something');
269+
expect('something'.stripLeft(''), 'something');
270+
271+
// remove a single character at the beginning
272+
expect('@something'.stripLeft('@'), 'something');
273+
expect('@something@'.stripLeft('@'), 'something@');
274+
expect('something@'.stripLeft('@'), 'something@');
275+
276+
// remove more than one character from the beginning
277+
expect('@@@something'.stripLeft('@'), 'something');
278+
expect('@@something@@@'.stripLeft('@'), 'something@@@');
279+
expect('something@@@'.stripLeft('@'), 'something@@@');
280+
281+
// search-string with multiple characters in it
282+
expect('@£something'.stripLeft('@£'), 'something');
283+
expect('@£something@£@'.stripLeft('@£'), 'something@£@');
284+
expect('something@£@'.stripLeft('@£'), 'something@£@');
285+
286+
// source string with strip-chars in the middle
287+
expect('@£some@£^thing'.stripLeft('@£'), 'some@£^thing');
288+
expect('@£some@£^thing@£@'.stripLeft('@£'), 'some@£^thing@£@');
289+
expect('some@£^thing@£@'.stripLeft('@£'), 'some@£^thing@£@');
290+
291+
// strip returns an empty string
292+
expect('@@@@@£^£@£'.stripLeft('@£^'), '');
293+
expect(''.stripLeft('@£^'), '');
294+
expect(''.stripLeft(''), '');
295+
expect('2'.stripLeft('2'), '');
296+
297+
// results in a string with one character in it
298+
expect('£@£@£@£@3'.stripLeft('£@^'), '3');
299+
});
300+
test('stripRight', () {
301+
// tested function's comment
302+
expect('@£^£Some @£^ Thing@@£@^'.stripRight('@£^'),'@£^£Some @£^ Thing');
303+
304+
// edge cases that doesn't modify the string
305+
expect('something'.stripRight('@'), 'something');
306+
expect('something'.stripRight('@£^'), 'something');
307+
expect('something'.stripRight(''), 'something');
308+
309+
// remove a single character at the end
310+
expect('@something'.stripRight('@'), '@something');
311+
expect('@something@'.stripRight('@'), '@something');
312+
expect('something@'.stripRight('@'), 'something');
313+
314+
// remove more than one character from the end
315+
expect('@@@something'.stripRight('@'), '@@@something');
316+
expect('@@something@@@'.stripRight('@'), '@@something');
317+
expect('something@@@'.stripRight('@'), 'something');
318+
319+
// search-string with multiple characters in it
320+
expect('@£something'.stripRight('£@'), '@£something');
321+
expect('@£something@£@'.stripRight('@£'), '@£something');
322+
expect('something@£@'.stripRight('@£'), 'something');
323+
324+
// source string with strip-chars in the middle
325+
expect('@£some@£^thing'.stripRight('@£'), '@£some@£^thing');
326+
expect('@£some@£^thing@£@'.stripRight('@£'), '@£some@£^thing');
327+
expect('some@£^thing@£@'.stripRight('@£'), 'some@£^thing');
328+
329+
// strip returns an empty string
330+
expect('@@@@@£^£@£'.stripRight('@£^'), '');
331+
expect(''.stripRight('@£^'), '');
332+
expect(''.stripRight(''), '');
333+
expect('2'.stripRight('2'), '');
334+
335+
// results in a string with one character in it
336+
expect('3@££££@^'.strip('£@^'), '3');
337+
});
338+
});
222339
}

0 commit comments

Comments
 (0)