From 7242709dacdd9abbe34b9b4019167f8dcda373ea Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Mon, 7 Oct 2024 22:43:11 +0100 Subject: [PATCH] keep cast and negate conditional --- .../System.Private.CoreLib/src/System/String.cs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/String.cs b/src/libraries/System.Private.CoreLib/src/System/String.cs index 3ac13477211c6..65d1dc9ad9d52 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.cs @@ -432,14 +432,12 @@ public unsafe void CopyTo(int sourceIndex, char[] destination, int destinationIn [MethodImpl(MethodImplOptions.AggressiveInlining)] public void CopyTo(Span destination) { - if (destination.Length >= Length) - { - Buffer.Memmove(ref destination._reference, ref _firstChar, (uint)Length); - } - else + if ((uint)destination.Length < (uint)Length) { ThrowHelper.ThrowArgumentException_DestinationTooShort(); } + + Buffer.Memmove(ref destination._reference, ref _firstChar, (uint)Length); } /// Copies the contents of this string into the destination span. @@ -448,12 +446,13 @@ public void CopyTo(Span destination) [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryCopyTo(Span destination) { - if (destination.Length >= Length) + if ((uint)destination.Length < (uint)Length) { - Buffer.Memmove(ref destination._reference, ref _firstChar, (uint)Length); - return true; + return false; } - return false; + + Buffer.Memmove(ref destination._reference, ref _firstChar, (uint)Length); + return true; } // Returns the entire string as an array of characters.