[Style] Make CompareNumeric safer

This commit is contained in:
Matt Nadareski
2016-09-08 20:35:00 -07:00
parent 376c178fc5
commit b075da1afe

View File

@@ -389,12 +389,16 @@ namespace SabreTools.Helper
while (sIndex < s.Length)
{
if (otherIndex >= other.Length)
{
return 1;
}
if (char.IsDigit(s[sIndex]))
{
if (!char.IsDigit(other[otherIndex]))
{
return -1;
}
// Compare the numbers
StringBuilder sBuilder = new StringBuilder(), otherBuilder = new StringBuilder();
@@ -411,33 +415,41 @@ namespace SabreTools.Helper
long sValue = 0L, otherValue = 0L;
try
if (!Int64.TryParse(sBuilder.ToString(), out sValue))
{
sValue = Convert.ToInt64(sBuilder.ToString());
sValue = Int64.MaxValue;
}
catch (OverflowException) { sValue = Int64.MaxValue; }
try
if (!Int64.TryParse(otherBuilder.ToString(), out otherValue))
{
otherValue = Convert.ToInt64(otherBuilder.ToString());
otherValue = Int64.MaxValue;
}
catch (OverflowException) { otherValue = Int64.MaxValue; }
if (sValue < otherValue)
{
return -1;
}
else if (sValue > otherValue)
{
return 1;
}
}
else if (char.IsDigit(other[otherIndex]))
{
return 1;
}
else
{
int difference = string.Compare(s[sIndex].ToString(), other[otherIndex].ToString(), StringComparison.InvariantCultureIgnoreCase);
if (difference > 0)
{
return 1;
}
else if (difference < 0)
{
return -1;
}
sIndex++;
otherIndex++;
@@ -445,8 +457,10 @@ namespace SabreTools.Helper
}
if (otherIndex < other.Length)
{
return -1;
}
}
return 0;
}