Skip to content

Commit

Permalink
Convert the 'if (Const < (uint)span.Length)' patterns to equivalent n…
Browse files Browse the repository at this point in the history
…atural versions (#49450)
  • Loading branch information
SingleAccretion authored Mar 11, 2021
1 parent 877a8df commit 2f2614f
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static bool TryParse(ReadOnlySpan<byte> source, out bool value, out int b
return true;
}

if (4 < (uint)source.Length)
if (source.Length > 4)
{
if (dw == 0x534c4146 /* 'SLAF' */ && (source[4] & ~0x20) == 'E')
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1208,9 +1208,7 @@ private static bool TryFormatO(DateTime dateTime, TimeSpan offset, Span<char> de
// Tue, 03 Jan 2017 08:08:05 GMT
private static bool TryFormatR(DateTime dateTime, TimeSpan offset, Span<char> destination, out int charsWritten)
{
// Writing the check in this fashion elides all bounds checks on 'destination'
// for the remainder of the method.
if (28 >= (uint)destination.Length)
if (destination.Length <= 28)
{
charsWritten = 0;
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private bool InternalFallback(ReadOnlySpan<char> chars, out int charsConsumed)
{
firstChar = chars[0];

if (1 < (uint)chars.Length)
if (chars.Length > 1)
{
secondChar = chars[1];
}
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Text/Rune.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public static OperationStatus DecodeFromUtf16(ReadOnlySpan<char> source, out Run
// Let's optimistically assume for now it's a high surrogate and hope
// that combining it with the next char yields useful results.

if (1 < (uint)source.Length)
if (source.Length > 1)
{
char secondChar = source[1];
if (TryCreate(firstChar, secondChar, out result))
Expand Down Expand Up @@ -817,7 +817,7 @@ internal static int ReadFirstRuneFromUtf16Buffer(ReadOnlySpan<char> input)

// Treat 'returnValue' as the high surrogate.

if (1 >= (uint)input.Length)
if (input.Length <= 1)
{
return -1; // not an argument exception - just a "bad data" failure
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static unsafe OperationStatus FromUtf16(ReadOnlySpan<char> source, Span<b

destination = destination.Slice((int)(pOutputBufferRemaining - (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(destination))));

if (2 >= (uint)destination.Length)
if (destination.Length <= 2)
{
operationStatus = OperationStatus.DestinationTooSmall;
break;
Expand Down

0 comments on commit 2f2614f

Please sign in to comment.