Refactor multiply operation for better performance in 2 Commands.Utility functions (#14148)

This commit is contained in:
xtqqczze
2020-12-08 13:59:10 -08:00
committed by GitHub
parent 7b59040ffb
commit dd4e5c7ba8
2 changed files with 15 additions and 14 deletions
@@ -623,7 +623,6 @@ namespace Microsoft.PowerShell.Commands
{
object currentValue = objValue;
object statValue = statMinOrMaxValue;
int factor = isMin ? 1 : -1;
double temp;
currentValue = ((objValue != null) && LanguagePrimitives.TryConvertTo<double>(objValue, out temp)) ? temp : currentValue;
@@ -635,13 +634,15 @@ namespace Microsoft.PowerShell.Commands
statValue = PSObject.AsPSObject(statValue).ToString();
}
if ((statValue == null) ||
((LanguagePrimitives.Compare(statValue, currentValue, false, CultureInfo.CurrentCulture) * factor) > 0))
if (statValue == null)
{
return objValue;
}
return statMinOrMaxValue;
int comparisonResult = LanguagePrimitives.Compare(statValue, currentValue, ignoreCase: false, CultureInfo.CurrentCulture);
return (isMin ? comparisonResult : -comparisonResult) > 0
? objValue
: statMinOrMaxValue;
}
/// <summary>
@@ -210,19 +210,19 @@ namespace Microsoft.PowerShell.Commands
second = secondMsh.BaseObject;
}
if (LanguagePrimitives.TryCompare(first, second, !_caseSensitive, _cultureInfo, out int result))
if (!LanguagePrimitives.TryCompare(first, second, !_caseSensitive, _cultureInfo, out int result))
{
return result * (_ascendingOrder ? 1 : -1);
// Note that this will occur if the objects do not support
// IComparable. We fall back to comparing as strings.
// being here means the first object doesn't support ICompare
string firstString = PSObject.AsPSObject(first).ToString();
string secondString = PSObject.AsPSObject(second).ToString();
result = _cultureInfo.CompareInfo.Compare(firstString, secondString, _caseSensitive ? CompareOptions.None : CompareOptions.IgnoreCase);
}
// Note that this will occur if the objects do not support
// IComparable. We fall back to comparing as strings.
// being here means the first object doesn't support ICompare
string firstString = PSObject.AsPSObject(first).ToString();
string secondString = PSObject.AsPSObject(second).ToString();
return _cultureInfo.CompareInfo.Compare(firstString, secondString, _caseSensitive ? CompareOptions.None : CompareOptions.IgnoreCase) * (_ascendingOrder ? 1 : -1);
return _ascendingOrder ? result : -result;
}
private readonly CultureInfo _cultureInfo = null;