TryParse static method
Returns a ParseOutput object by parsing a string value.
ParseOutput output = ByteSize.TryParse('1024MB');
output.ByteSizeObj == ByteSize.FromMegaBytes(1024);
Implementation
static ParseOutput TryParse(String value) {
// Return a Boolean and a ByteSize object
// Arg checking
if (_IsNullOrWhiteSpace(value)) {
throw ArgumentNullException('Input String is null or whitespace');
}
// Get the index of the first non-digit character
String c, tempS = value.replaceAll(' ', ''); // Protect against whitespaces
var found = false;
// Pick first non-digit number
var num = 1;
for (var i = 0; i < tempS.length; i++) {
c = tempS[i];
if (!(_NumArray.contains(c) || c == '.')) {
found = true;
break;
}
num += 1;
} // end for
if (found == false) {
return ParseOutput(true, ByteSize());
}
var lastNumber = num - 1;
// Cut the input string in half
var numberPart = tempS.substring(0, lastNumber).trim();
var sizePart = tempS.substring(lastNumber).trim();
// Get the numeric part
double number;
try {
number = double.parse(numberPart);
} on Exception {
return ParseOutput(true, ByteSize());
}
// Get the magnitude part
int tempInt;
try {
tempInt = _SymbolArray.indexOf(sizePart);
} on Exception {
return ParseOutput(true, ByteSize());
}
if (tempInt == 0) {
var d1 = 1 * 1.0;
if (_FloatingMod(number, d1) != 0) {
// Can't have partial bits
return ParseOutput(true, ByteSize());
}
return ParseOutput(false, ByteSize.FromBits(number.toInt()));
}
if (tempInt == 1) {
return ParseOutput(false, ByteSize.FromBytes(number));
}
if ([2, 3, 4].contains(tempInt)) {
return ParseOutput(false, ByteSize.FromKiloBytes(number));
}
if ([5, 6, 7].contains(tempInt)) {
return ParseOutput(false, ByteSize.FromMegaBytes(number));
}
if ([8, 9, 10].contains(tempInt)) {
return ParseOutput(false, ByteSize.FromGigaBytes(number));
}
if ([11, 12, 13].contains(tempInt)) {
return ParseOutput(false, ByteSize.FromTeraBytes(number));
}
if ([14, 15, 16].contains(tempInt)) {
return ParseOutput(false, ByteSize.FromPetaBytes(number));
}
return ParseOutput(true, ByteSize());
}