Skip to content

Commit 716dae9

Browse files
committed
Add strip, stripLeft, stripRight methods to String
Add an extension to the string class that adds strip, stripLeft, and stripRight methods that are functionally similar to the strip, lstrip and rstrip functions in python's string class.
1 parent 45ed0a3 commit 716dae9

2 files changed

Lines changed: 178 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: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,127 @@ 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+
// ensure that strip works with unicode chars: this text in in the script "Malayalam"
263+
expect('വ മലയാളം'.strip(''), 'വ മലയാളം');
264+
expect('വ മലയാളം'.strip('വ'), ' മലയാളം');
265+
expect('വ മലയാളംx'.strip('xവ'), ' മലയാളം');
266+
expect('kമലയാളംx'.strip('xk'), 'മലയാളം');
267+
});
268+
test('stripLeft', () {
269+
// tested function's comment
270+
expect('@£^£Some @£^ Thing@@£@^'.stripLeft('@£^'), 'Some @£^ Thing@@£@^');
271+
272+
// edge cases that doesn't modify the string
273+
expect('something'.stripLeft('@'), 'something');
274+
expect('something'.stripLeft('@£^'), 'something');
275+
expect('something'.stripLeft(''), 'something');
276+
277+
// remove a single character at the beginning
278+
expect('@something'.stripLeft('@'), 'something');
279+
expect('@something@'.stripLeft('@'), 'something@');
280+
expect('something@'.stripLeft('@'), 'something@');
281+
282+
// remove more than one character from the beginning
283+
expect('@@@something'.stripLeft('@'), 'something');
284+
expect('@@something@@@'.stripLeft('@'), 'something@@@');
285+
expect('something@@@'.stripLeft('@'), 'something@@@');
286+
287+
// search-string with multiple characters in it
288+
expect('@£something'.stripLeft('@£'), 'something');
289+
expect('@£something@£@'.stripLeft('@£'), 'something@£@');
290+
expect('something@£@'.stripLeft('@£'), 'something@£@');
291+
292+
// source string with strip-chars in the middle
293+
expect('@£some@£^thing'.stripLeft('@£'), 'some@£^thing');
294+
expect('@£some@£^thing@£@'.stripLeft('@£'), 'some@£^thing@£@');
295+
expect('some@£^thing@£@'.stripLeft('@£'), 'some@£^thing@£@');
296+
297+
// strip returns an empty string
298+
expect('@@@@@£^£@£'.stripLeft('@£^'), '');
299+
expect(''.stripLeft('@£^'), '');
300+
expect(''.stripLeft(''), '');
301+
expect('2'.stripLeft('2'), '');
302+
303+
// results in a string with one character in it
304+
expect('£@£@£@£@3'.stripLeft('£@^'), '3');
305+
});
306+
test('stripRight', () {
307+
// tested function's comment
308+
expect('@£^£Some @£^ Thing@@£@^'.stripRight('@£^'),'@£^£Some @£^ Thing');
309+
310+
// edge cases that doesn't modify the string
311+
expect('something'.stripRight('@'), 'something');
312+
expect('something'.stripRight('@£^'), 'something');
313+
expect('something'.stripRight(''), 'something');
314+
315+
// remove a single character at the end
316+
expect('@something'.stripRight('@'), '@something');
317+
expect('@something@'.stripRight('@'), '@something');
318+
expect('something@'.stripRight('@'), 'something');
319+
320+
// remove more than one character from the end
321+
expect('@@@something'.stripRight('@'), '@@@something');
322+
expect('@@something@@@'.stripRight('@'), '@@something');
323+
expect('something@@@'.stripRight('@'), 'something');
324+
325+
// search-string with multiple characters in it
326+
expect('@£something'.stripRight('£@'), '@£something');
327+
expect('@£something@£@'.stripRight('@£'), '@£something');
328+
expect('something@£@'.stripRight('@£'), 'something');
329+
330+
// source string with strip-chars in the middle
331+
expect('@£some@£^thing'.stripRight('@£'), '@£some@£^thing');
332+
expect('@£some@£^thing@£@'.stripRight('@£'), '@£some@£^thing');
333+
expect('some@£^thing@£@'.stripRight('@£'), 'some@£^thing');
334+
335+
// strip returns an empty string
336+
expect('@@@@@£^£@£'.stripRight('@£^'), '');
337+
expect(''.stripRight('@£^'), '');
338+
expect(''.stripRight(''), '');
339+
expect('2'.stripRight('2'), '');
340+
341+
// results in a string with one character in it
342+
expect('3@££££@^'.strip('£@^'), '3');
343+
});
344+
});
222345
}

0 commit comments

Comments
 (0)