Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(MQ cleanup) Remove some unsafe code from System.Xml #43379

Merged
merged 10 commits into from
Nov 3, 2020
Prev Previous commit
Next Next commit
Remove unsafe IntToString implementation
  • Loading branch information
GrabYourPitchforks committed Oct 13, 2020
commit cb2b6fd597dcc199379bc485cb2d95c79cbfff5e
Original file line number Diff line number Diff line change
Expand Up @@ -2846,37 +2846,6 @@ private void InitFromDouble(double dbl)
}
};

/* ----------------------------------------------------------------------------
IntToString()

Converts an integer to a string according to XPath rules.
*/
private static unsafe string IntToString(int val)
{
// The maximum number of characters needed to represent any int value is 11
const int BufSize = 12;
char* pBuf = stackalloc char[BufSize];
char* pch = pBuf += BufSize;
uint u = (uint)(val < 0 ? -val : val);

while (u >= 10)
{
// Fast division by 10
uint quot = (uint)((0x66666667L * u) >> 32) >> 2;
*(--pch) = (char)((u - quot * 10) + '0');
u = quot;
}

*(--pch) = (char)(u + '0');

if (val < 0)
{
*(--pch) = '-';
}

return new string(pch, 0, (int)(pBuf - pch));
}

/* ----------------------------------------------------------------------------
DoubleToString()

Expand All @@ -2890,7 +2859,7 @@ public static string DoubleToString(double dbl)

if (IsInteger(dbl, out iVal))
{
return IntToString(iVal);
return iVal.ToString(CultureInfo.InvariantCulture);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed custom implementation in favor of delegating to the runtime's already-optimized int.ToString implementation.

On .NET 5/6, dereferencing CultureInfo.InvariantCulture is very fast due to recent JIT optimizations.

}

// Handle NaN and infinity
Expand Down