parseSize function
Implementation
int parseSize(String input) {
if (input.isEmpty) {
throw FormatException('Size cannot be empty');
}
final pattern = RegExp(r'^(\d+(?:\.\d+)?)([KMG]B)$', caseSensitive: false);
final match = pattern.firstMatch(input.trim());
if (match == null) {
throw FormatException(
'Invalid size format: $input. Use formats like 500MB or 1GB');
}
final value = double.parse(match.group(1)!); // The numeric part
if (value <= 0) {
throw FormatException('Size must be greater than 0');
}
final unit = match.group(2)!.toUpperCase(); // The unit (MB, GB, etc.)
switch (unit) {
case 'KB':
return (value * 1024).toInt();
case 'MB':
return (value * 1024 * 1024).toInt(); // Convert MB to bytes
case 'GB':
return (value * 1024 * 1024 * 1024).toInt(); // Convert GB to bytes
default:
throw FormatException('Unknown size unit: $unit');
}
}