Skip to content

Commit

Permalink
Use TryParseUInt32HexNumberStyle directly from Guid.TryParse (#44918)
Browse files Browse the repository at this point in the history
Skips public entry points of uint.TryParse, including argument validation, branches for style, but most impactfully fetching the current number culture when it won't actually be needed.
  • Loading branch information
stephentoub authored Nov 20, 2020
1 parent a61b054 commit f22d7c5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/libraries/System.Private.CoreLib/src/System/Guid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private static bool TryParseGuid(ReadOnlySpan<char> guidString, ref GuidResult r
}

// Two helpers used for parsing components:
// - uint.TryParse(..., NumberStyles.AllowHexSpecifier, ...)
// - Number.TryParseUInt32HexNumberStyle(..., NumberStyles.AllowHexSpecifier, ...)
// Used when we expect the entire provided span to be filled with and only with hex digits and no overflow is possible
// - TryParseHex
// Used when the component may have an optional '+' and "0x" prefix, when it may overflow, etc.
Expand Down Expand Up @@ -421,7 +421,7 @@ private static bool TryParseExactD(ReadOnlySpan<char> guidString, ref GuidResult
// _f, _g must be stored as a big-endian ushort
result._fg = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness((ushort)uintTmp) : (ushort)uintTmp;

if (uint.TryParse(guidString.Slice(28, 8), NumberStyles.AllowHexSpecifier, null, out uintTmp)) // _h, _i, _j, _k
if (Number.TryParseUInt32HexNumberStyle(guidString.Slice(28, 8), NumberStyles.AllowHexSpecifier, out uintTmp) == Number.ParsingStatus.OK) // _h, _i, _j, _k
{
// _h, _i, _j, _k must be stored as a big-endian uint
result._hijk = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(uintTmp) : uintTmp;
Expand All @@ -447,20 +447,20 @@ private static bool TryParseExactN(ReadOnlySpan<char> guidString, ref GuidResult
return false;
}

if (uint.TryParse(guidString.Slice(0, 8), NumberStyles.AllowHexSpecifier, null, out result._a) && // _a
uint.TryParse(guidString.Slice(8, 8), NumberStyles.AllowHexSpecifier, null, out uint uintTmp)) // _b, _c
if (Number.TryParseUInt32HexNumberStyle(guidString.Slice(0, 8), NumberStyles.AllowHexSpecifier, out result._a) == Number.ParsingStatus.OK && // _a
Number.TryParseUInt32HexNumberStyle(guidString.Slice(8, 8), NumberStyles.AllowHexSpecifier, out uint uintTmp) == Number.ParsingStatus.OK) // _b, _c
{
// _b, _c are independently in machine-endian order
if (BitConverter.IsLittleEndian) { uintTmp = BitOperations.RotateRight(uintTmp, 16); }
result._bc = uintTmp;

if (uint.TryParse(guidString.Slice(16, 8), NumberStyles.AllowHexSpecifier, null, out uintTmp)) // _d, _e, _f, _g
if (Number.TryParseUInt32HexNumberStyle(guidString.Slice(16, 8), NumberStyles.AllowHexSpecifier, out uintTmp) == Number.ParsingStatus.OK) // _d, _e, _f, _g
{
// _d, _e, _f, _g must be stored as a big-endian uint
if (BitConverter.IsLittleEndian) { uintTmp = BinaryPrimitives.ReverseEndianness(uintTmp); }
result._defg = uintTmp;

if (uint.TryParse(guidString.Slice(24, 8), NumberStyles.AllowHexSpecifier, null, out uintTmp)) // _h, _i, _j, _k
if (Number.TryParseUInt32HexNumberStyle(guidString.Slice(24, 8), NumberStyles.AllowHexSpecifier, out uintTmp) == Number.ParsingStatus.OK) // _h, _i, _j, _k
{
// _h, _i, _j, _k must be stored as big-endian uint
if (BitConverter.IsLittleEndian) { uintTmp = BinaryPrimitives.ReverseEndianness(uintTmp); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan<char> valu
}

/// <summary>Parses uint limited to styles that make up NumberStyles.HexNumber.</summary>
private static ParsingStatus TryParseUInt32HexNumberStyle(ReadOnlySpan<char> value, NumberStyles styles, out uint result)
internal static ParsingStatus TryParseUInt32HexNumberStyle(ReadOnlySpan<char> value, NumberStyles styles, out uint result)
{
Debug.Assert((styles & ~NumberStyles.HexNumber) == 0, "Only handles subsets of HexNumber format");

Expand Down

0 comments on commit f22d7c5

Please sign in to comment.