Fix subtle number parsing bug

This commit is contained in:
Matt Nadareski
2025-05-13 12:32:15 -04:00
parent 6fc301f49f
commit e82bbdc879

View File

@@ -65,21 +65,23 @@ namespace SabreTools.Core.Tools
// Normalize the string for easier comparison // Normalize the string for easier comparison
numeric = numeric!.ToLowerInvariant(); numeric = numeric!.ToLowerInvariant();
// Get the multiplication modifier and trim characters
long multiplier = DetermineMultiplier(numeric);
numeric = numeric.TrimEnd(['k', 'm', 'g', 't', 'p', 'e', 'z', 'y', 'i', 'b', ' ']);
// Parse the numeric string, if possible // Parse the numeric string, if possible
long value;
if (numeric.StartsWith("0x")) if (numeric.StartsWith("0x"))
value = Convert.ToInt64(numeric, 16); {
else if (long.TryParse(numeric, out long longValue)) return Convert.ToInt64(numeric.Substring(2), 16);
value = longValue; }
else else
return null; {
// Get the multiplication modifier and trim characters
long multiplier = DetermineMultiplier(numeric);
numeric = numeric.TrimEnd(['k', 'm', 'g', 't', 'p', 'e', 'z', 'y', 'i', 'b', ' ']);
// Apply the multiplier and return // Apply the multiplier and return
return value * multiplier; if (!long.TryParse(numeric, out long longValue))
return null;
return longValue * multiplier;
}
} }
/// <summary> /// <summary>