Skip to content

Commit

Permalink
Adding "radix" to toInt and toIntOrNUll (simc#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
shinayser authored and simc committed Jan 22, 2020
1 parent 03e1d09 commit 4c136fb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
18 changes: 16 additions & 2 deletions lib/src/string.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,25 @@ extension StringX on String {
bool get isInt => toIntOrNull() != null;

/// Parses the string as an [int] number and returns the result.
int toInt() => int.parse(this);
///
/// The [radix] must be in the range 2..36. The digits used are
/// first the decimal digits 0..9, and then the letters 'a'..'z' with
/// values 10 through 35. Also accepts upper-case letters with the same
/// values as the lower-case ones.
///
/// If no [radix] is given then it defaults to 10.
int toInt({int radix}) => int.parse(this, radix: radix);

/// Parses the string as an [int] number and returns the result or `null` if
/// the string is not a valid representation of a number.
int toIntOrNull() => int.tryParse(this);
///
/// The [radix] must be in the range 2..36. The digits used are
/// first the decimal digits 0..9, and then the letters 'a'..'z' with
/// values 10 through 35. Also accepts upper-case letters with the same
/// values as the lower-case ones.
///
/// If no [radix] is given then it defaults to 10.
int toIntOrNull({int radix}) => int.tryParse(this, radix: radix);

bool get isDouble => toDoubleOrNull() != null;

Expand Down
29 changes: 17 additions & 12 deletions test/string_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,18 @@ void main() {
expect('123456789'.isInt, true);
});

test('.toInt()', () {
expect('0'.toInt(), 0);
expect('1'.toInt(), 1);
expect('123456789'.toInt(), 123456789);
group('.toInt()', () {
test('with radix', () {
expect('100'.toInt(radix: 2), 4);
expect('100'.toInt(radix: 16), 256);
expect('FF'.toInt(radix: 16), 255);
});

test('without radix', () {
expect('0'.toInt(), 0);
expect('1'.toInt(), 1);
expect('123456789'.toInt(), 123456789);
});
});

test('.toIntOrNull()', () {
Expand Down Expand Up @@ -129,18 +137,15 @@ void main() {
expect('message digest'.md5, 'f96b697d7cb7938d525a2f31aaf161d0');
expect('ഐ⌛酪Б👨‍👨‍👧‍👦'.md5, 'c7834eff7c967101cfb65b8f6d15ad46');
expect(
'abcdefghijklmnopqrstuvwxyz'.md5,
'c3fcd3d76192e4007dfb496cca67e13b'
);
'abcdefghijklmnopqrstuvwxyz'.md5, 'c3fcd3d76192e4007dfb496cca67e13b');
expect(
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.md5,
'd174ab98d277d9f5a5611c2c9f419d9f'
);
'd174ab98d277d9f5a5611c2c9f419d9f');
expect(
'12345678901234567890123456789012345678901234567890123456789012'
'345678901234567890'.md5,
'57edf4a22be3c955ac49da2e2107b67a'
);
'345678901234567890'
.md5,
'57edf4a22be3c955ac49da2e2107b67a');
});
});
}

0 comments on commit 4c136fb

Please sign in to comment.