Skip to content

Commit 4c212c5

Browse files
authored
Clean up Guid.DecodeByte (#88267)
1 parent b462767 commit 4c212c5

File tree

1 file changed

+7
-17
lines changed
  • src/libraries/System.Private.CoreLib/src/System

1 file changed

+7
-17
lines changed

src/libraries/System.Private.CoreLib/src/System/Guid.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -727,27 +727,17 @@ private static bool TryParseExactX(ReadOnlySpan<char> guidString, ref GuidResult
727727
}
728728

729729
[MethodImpl(MethodImplOptions.AggressiveInlining)]
730-
private static byte DecodeByte(nuint ch1, nuint ch2, ref int invalidIfNegative)
730+
private static byte DecodeByte(char ch1, char ch2, ref int invalidIfNegative)
731731
{
732-
// TODO https://github.com/dotnet/runtime/issues/13464:
733-
// Replace the Unsafe.Add with HexConverter.FromChar once the bounds checks are eliminated.
734-
735732
ReadOnlySpan<byte> lookup = HexConverter.CharToHexLookup;
733+
Debug.Assert(lookup.Length == 256);
736734

737-
int h1 = -1;
738-
if (ch1 < (nuint)lookup.Length)
739-
{
740-
h1 = (sbyte)Unsafe.Add(ref MemoryMarshal.GetReference(lookup), (nint)ch1);
741-
}
742-
h1 <<= 4;
743-
744-
int h2 = -1;
745-
if (ch2 < (nuint)lookup.Length)
746-
{
747-
h2 = (sbyte)Unsafe.Add(ref MemoryMarshal.GetReference(lookup), (nint)ch2);
748-
}
735+
int upper = (sbyte)lookup[(byte)ch1];
736+
int lower = (sbyte)lookup[(byte)ch2];
737+
int result = (upper << 4) | lower;
749738

750-
int result = h1 | h2;
739+
// Result will be negative if ch1 or/and ch2 are greater than 0xFF
740+
result = (ch1 | ch2) >> 8 == 0 ? result : -1;
751741
invalidIfNegative |= result;
752742
return (byte)result;
753743
}

0 commit comments

Comments
 (0)