Replace TryCopyTo with CopyTo (#16078)
authorStephen Toub <stoub@microsoft.com>
Tue, 30 Jan 2018 15:17:30 +0000 (10:17 -0500)
committerGitHub <noreply@github.com>
Tue, 30 Jan 2018 15:17:30 +0000 (10:17 -0500)
While doing various Span-related work involving copying, in a few places I previously used a pattern of calling TryCopyTo and then asserting its result, in places where I knew the destination was long enough.  This was because CopyTo was implemented as a wrapper around TryCopyTo and thus involved an extra unnecessary branch.  Now that that's no longer the case, I'm simplifying the call sites.

src/mscorlib/shared/System/Boolean.cs
src/mscorlib/shared/System/Number.Formatting.cs
src/mscorlib/shared/System/Text/ValueStringBuilder.cs

index 896e5f1..c1f4bc9 100644 (file)
@@ -100,10 +100,8 @@ namespace System
         {
             string s = m_value ? TrueLiteral : FalseLiteral;
 
-            if (s.Length <= destination.Length)
+            if (s.AsReadOnlySpan().TryCopyTo(destination))
             {
-                bool copied = s.AsReadOnlySpan().TryCopyTo(destination);
-                Debug.Assert(copied);
                 charsWritten = s.Length;
                 return true;
             }
index 70b35a0..58d501b 100644 (file)
@@ -1128,10 +1128,8 @@ namespace System
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         private static unsafe bool TryCopyTo(char* src, int length, Span<char> destination, out int charsWritten)
         {
-            if (length <= destination.Length)
+            if (new ReadOnlySpan<char>(src, length).TryCopyTo(destination))
             {
-                bool copied = new ReadOnlySpan<char>(src, length).TryCopyTo(destination);
-                Debug.Assert(copied);
                 charsWritten = length;
                 return true;
             }
@@ -1708,8 +1706,7 @@ namespace System
                         if (thousandsSepCtr >= thousandsSepPos.Length)
                         {
                             var newThousandsSepPos = new int[thousandsSepPos.Length * 2];
-                            bool copied = thousandsSepPos.TryCopyTo(newThousandsSepPos);
-                            Debug.Assert(copied, "Expect copy to succeed, as the new array is larger than the original");
+                            thousandsSepPos.CopyTo(newThousandsSepPos);
                             thousandsSepPos = newThousandsSepPos;
                         }
 
index 84e5fa8..99045ca 100644 (file)
@@ -112,8 +112,7 @@ namespace System.Text
                 Grow(s.Length);
             }
 
-            bool copied = s.AsReadOnlySpan().TryCopyTo(_chars.Slice(pos));
-            Debug.Assert(copied, "Grow should have made enough room to successfully copy");
+            s.AsReadOnlySpan().CopyTo(_chars.Slice(pos));
             _pos += s.Length;
         }
 
@@ -175,8 +174,7 @@ namespace System.Text
 
             char[] poolArray = ArrayPool<char>.Shared.Rent(Math.Max(_pos + requiredAdditionalCapacity, _chars.Length * 2));
 
-            bool success = _chars.TryCopyTo(poolArray);
-            Debug.Assert(success);
+            _chars.CopyTo(poolArray);
 
             char[] toReturn = _arrayToReturnToPool;
             _chars = _arrayToReturnToPool = poolArray;