Skip to content

Commit

Permalink
Remove uint casts in string.(Try)CopyTo
Browse files Browse the repository at this point in the history
  • Loading branch information
xtqqczze committed Oct 7, 2024
1 parent 48dbc4f commit f8f7f2d
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/libraries/System.Private.CoreLib/src/System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,12 @@ public unsafe void CopyTo(int sourceIndex, char[] destination, int destinationIn
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(Span<char> destination)
{
if ((uint)Length <= (uint)destination.Length)
{
Buffer.Memmove(ref destination._reference, ref _firstChar, (uint)Length);
}
else
if (destination.Length < Length)
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}

Buffer.Memmove(ref destination._reference, ref _firstChar, (uint)Length);
}

/// <summary>Copies the contents of this string into the destination span.</summary>
Expand All @@ -448,13 +446,13 @@ public void CopyTo(Span<char> destination)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryCopyTo(Span<char> destination)
{
bool retVal = false;
if ((uint)Length <= (uint)destination.Length)
if (destination.Length < Length)
{
Buffer.Memmove(ref destination._reference, ref _firstChar, (uint)Length);
retVal = true;
return false;
}
return retVal;

Buffer.Memmove(ref destination._reference, ref _firstChar, (uint)Length);
return true;
}

// Returns the entire string as an array of characters.
Expand Down

1 comment on commit f8f7f2d

@xtqqczze
Copy link
Owner Author

Choose a reason for hiding this comment

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

Please sign in to comment.