splitHumanReadableSize function
Split a human-readable file size to (value, unit)
print(splitHumanReadableSize('30')); // (30.0, 'B')
print(splitHumanReadableSize('30B')); // (30.0, 'B')
print(splitHumanReadableSize('1m')); // (1.0, 'M')
print(splitHumanReadableSize('2.5M')); // (2.5, 'M')
print(splitHumanReadableSize('5.5MB')); // (5.5, 'M')
Implementation
(double, String)? splitHumanReadableSize(String humanSize) {
// KiB->KB; ?iB->?B
if (humanSize.contains(r'i')) {
humanSize = humanSize.replaceFirst(r'i', r'');
}
final Match? match = regexSizeInput.firstMatch(humanSize.trim());
if (match == null) return null; // Invalid format
final double value = double.parse(match.group(1)!);
String? unit = match.group(3)?.toUpperCase();
if (unit == null || unit.isEmpty) unit = 'B';
return (value, unit);
}