Use nint instead of IntPtr in generic SpanHelpers (#41199)
authorJan Kotas <jkotas@microsoft.com>
Sun, 23 Aug 2020 16:01:51 +0000 (09:01 -0700)
committerGitHub <noreply@github.com>
Sun, 23 Aug 2020 16:01:51 +0000 (16:01 +0000)
src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs

index 5ea8ad1..2f2763f 100644 (file)
@@ -49,7 +49,7 @@ namespace System
         {
             Debug.Assert(length >= 0);
 
-            IntPtr index = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
+            nint index = 0; // Use nint for arithmetic to avoid unnecessary 64->32->64 truncations
 
             if (default(T) != null || (object)value != null)
             {
@@ -99,8 +99,8 @@ namespace System
             }
             else
             {
-                byte* len = (byte*)length;
-                for (index = (IntPtr)0; index.ToPointer() < len; index += 1)
+                nint len = length;
+                for (index = 0; index < len; index++)
                 {
                     if ((object)Unsafe.Add(ref searchSpace, index) is null)
                     {
@@ -119,7 +119,7 @@ namespace System
         {
             Debug.Assert(length >= 0);
 
-            IntPtr index = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
+            nint index = 0; // Use nint for arithmetic to avoid unnecessary 64->32->64 truncations
             if (default(T) != null || (object)value != null)
             {
                 while (length >= 8)
@@ -173,8 +173,8 @@ namespace System
             }
             else
             {
-                byte* len = (byte*)length;
-                for (index = (IntPtr)0; index.ToPointer() < len; index += 1)
+                nint len = (nint)length;
+                for (index = 0; index < len; index++)
                 {
                     if ((object)Unsafe.Add(ref searchSpace, index) is null)
                     {
@@ -185,21 +185,21 @@ namespace System
             return -1;
 
         Found: // Workaround for https://github.com/dotnet/runtime/issues/8795
-            return (int)(byte*)index;
+            return (int)index;
         Found1:
-            return (int)(byte*)(index + 1);
+            return (int)(index + 1);
         Found2:
-            return (int)(byte*)(index + 2);
+            return (int)(index + 2);
         Found3:
-            return (int)(byte*)(index + 3);
+            return (int)(index + 3);
         Found4:
-            return (int)(byte*)(index + 4);
+            return (int)(index + 4);
         Found5:
-            return (int)(byte*)(index + 5);
+            return (int)(index + 5);
         Found6:
-            return (int)(byte*)(index + 6);
+            return (int)(index + 6);
         Found7:
-            return (int)(byte*)(index + 7);
+            return (int)(index + 7);
         }
 
         public static int IndexOfAny<T>(ref T searchSpace, T value0, T value1, int length) where T : IEquatable<T>
@@ -780,7 +780,7 @@ namespace System
             if (Unsafe.AreSame(ref first, ref second))
                 goto Equal;
 
-            IntPtr index = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
+            nint index = 0; // Use nint for arithmetic to avoid unnecessary 64->32->64 truncations
             T lookUp0;
             T lookUp1;
             while (length >= 8)