Skip to content

Commit

Permalink
Add strip, stripLeft, stripRight extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
amarghosh committed Aug 21, 2024
1 parent 45ed0a3 commit ed68d8c
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
55 changes: 55 additions & 0 deletions lib/src/StringUtils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,58 @@ class StringUtils {
return false;
}
}

extension Strip on String {
/// Implementation of strip, stripLeft and stripRight
String _strip(
String stripChars, {
bool left = true,
bool right = true,
}) {
if (!left && !right) {
throw ArgumentError('either left or right shall be true in _strip: <$this>');
}
int start = 0;
int end = length;
List<int> sourceChars = runes.toList();
List<int> searchChars = stripChars.runes.toList();
if (left) {
for (int i = 0; i < sourceChars.length; i++) {
if (searchChars.contains(sourceChars[i])) {
start++;
} else {
break;
}
}
}
if (right) {
for (int i = sourceChars.length - 1; i >= 0; i--) {
if (searchChars.contains(sourceChars[i])) {
end--;
} else {
break;
}
}
}

if (start >= end) {
return '';
}
return substring(start, end);
}

/// Return a string after removing any leading and trailing characters specified in [chars]
///
/// '@£^£Some @£^ Thing@@£@^'.strip('@£^') => 'Some @£^ Thing'
String strip(String chars) => _strip(chars, left: true, right: true);

/// Return a string after removing any leading characters specified in [chars]
///
/// '@£^£Some @£^ Thing@@£@^'.stripLeft('@£^') => 'Some @£^ Thing@@£@^'
String stripLeft(String chars) => _strip(chars, left: true, right: false);

/// Return a string after removing any trailing characters specified in [chars]
///
/// '@£^£Some @£^ Thing@@£@^'.strip('@£^') => '@£^£Some @£^ Thing'
String stripRight(String chars) => _strip(chars, left: false, right: true);
}
117 changes: 117 additions & 0 deletions test/string_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,121 @@ void main() {
expect(StringUtils.isIP('0.0.0.256'), false);
expect(StringUtils.isIP('26.0.0.256'), false);
});

group('strip extension', () {
test('strip', () {
// tested function's comment
expect('@£^£Some @£^ Thing@@£@^'.strip('@£^'), 'Some @£^ Thing');

// edge cases that doesn't modify the string
expect('something'.strip('@'), 'something');
expect('something'.strip('@£^'), 'something');
expect('something'.strip(''), 'something');

// remove a single character at either end from the source
expect('@something'.strip('@'), 'something');
expect('@something@'.strip('@'), 'something');
expect('something@'.strip('@'), 'something');

// remove more than one character at either end from the source
expect('@@@something'.strip('@'), 'something');
expect('@@something@@@'.strip('@'), 'something');
expect('something@@@'.strip('@'), 'something');

// search-string with multiple characters in it
expect('@£something'.strip('@£'), 'something');
expect('@£something@£^'.strip('@£^'), 'something');
expect('something@£^'.strip('@£^'), 'something');

// source string with strip-chars in the middle
expect('@£some@£^thing'.strip('@£'), 'some@£^thing');
expect('@£some@£^thing@£@'.strip('@£'), 'some@£^thing');
expect('some@£^thing@£@'.strip('@£'), 'some@£^thing');

// strip returns an empty string
expect('@@@@@£^^£@£'.strip('@£^'), '');
expect(''.strip('@£^'), '');
expect(''.strip(''), '');
expect('2'.strip('2'), '');

// results in a string with one character in it
expect('£@£@£@£@3@££££@^'.strip('£@^'), '3');
});
test('stripLeft', () {
// tested function's comment
expect('@£^£Some @£^ Thing@@£@^'.stripLeft('@£^'), 'Some @£^ Thing@@£@^');

// edge cases that doesn't modify the string
expect('something'.stripLeft('@'), 'something');
expect('something'.stripLeft('@£^'), 'something');
expect('something'.stripLeft(''), 'something');

// remove a single character at the beginning
expect('@something'.stripLeft('@'), 'something');
expect('@something@'.stripLeft('@'), 'something@');
expect('something@'.stripLeft('@'), 'something@');

// remove more than one character from the beginning
expect('@@@something'.stripLeft('@'), 'something');
expect('@@something@@@'.stripLeft('@'), 'something@@@');
expect('something@@@'.stripLeft('@'), 'something@@@');

// search-string with multiple characters in it
expect('@£something'.stripLeft('@£'), 'something');
expect('@£something@£@'.stripLeft('@£'), 'something@£@');
expect('something@£@'.stripLeft('@£'), 'something@£@');

// source string with strip-chars in the middle
expect('@£some@£^thing'.stripLeft('@£'), 'some@£^thing');
expect('@£some@£^thing@£@'.stripLeft('@£'), 'some@£^thing@£@');
expect('some@£^thing@£@'.stripLeft('@£'), 'some@£^thing@£@');

// strip returns an empty string
expect('@@@@@£^£@£'.stripLeft('@£^'), '');
expect(''.stripLeft('@£^'), '');
expect(''.stripLeft(''), '');
expect('2'.stripLeft('2'), '');

// results in a string with one character in it
expect('£@£@£@£@3'.stripLeft('£@^'), '3');
});
test('stripRight', () {
// tested function's comment
expect('@£^£Some @£^ Thing@@£@^'.stripRight('@£^'),'@£^£Some @£^ Thing');

// edge cases that doesn't modify the string
expect('something'.stripRight('@'), 'something');
expect('something'.stripRight('@£^'), 'something');
expect('something'.stripRight(''), 'something');

// remove a single character at the end
expect('@something'.stripRight('@'), '@something');
expect('@something@'.stripRight('@'), '@something');
expect('something@'.stripRight('@'), 'something');

// remove more than one character from the end
expect('@@@something'.stripRight('@'), '@@@something');
expect('@@something@@@'.stripRight('@'), '@@something');
expect('something@@@'.stripRight('@'), 'something');

// search-string with multiple characters in it
expect('@£something'.stripRight('£@'), '@£something');
expect('@£something@£@'.stripRight('@£'), '@£something');
expect('something@£@'.stripRight('@£'), 'something');

// source string with strip-chars in the middle
expect('@£some@£^thing'.stripRight('@£'), '@£some@£^thing');
expect('@£some@£^thing@£@'.stripRight('@£'), '@£some@£^thing');
expect('some@£^thing@£@'.stripRight('@£'), 'some@£^thing');

// strip returns an empty string
expect('@@@@@£^£@£'.stripRight('@£^'), '');
expect(''.stripRight('@£^'), '');
expect(''.stripRight(''), '');
expect('2'.stripRight('2'), '');

// results in a string with one character in it
expect('3@££££@^'.strip('£@^'), '3');
});
});
}

0 comments on commit ed68d8c

Please sign in to comment.