summaryrefslogtreecommitdiff
path: root/mcs/class/corlib/System/Array.cs
diff options
context:
space:
mode:
Diffstat (limited to 'mcs/class/corlib/System/Array.cs')
-rw-r--r--mcs/class/corlib/System/Array.cs74
1 files changed, 57 insertions, 17 deletions
diff --git a/mcs/class/corlib/System/Array.cs b/mcs/class/corlib/System/Array.cs
index 91654019f1..756378c56d 100644
--- a/mcs/class/corlib/System/Array.cs
+++ b/mcs/class/corlib/System/Array.cs
@@ -2864,32 +2864,54 @@ namespace System
{
if (array == null)
throw new ArgumentNullException ("array");
+
+ if (match == null)
+ throw new ArgumentNullException ("match");
- return FindLastIndex<T> (array, 0, array.Length, match);
+ return GetLastIndex (array, 0, array.Length, match);
}
public static int FindLastIndex<T> (T [] array, int startIndex, Predicate<T> match)
{
if (array == null)
throw new ArgumentNullException ();
+
+ if (startIndex < 0 || (uint) startIndex > (uint) array.Length)
+ throw new ArgumentOutOfRangeException ("startIndex");
+
+ if (match == null)
+ throw new ArgumentNullException ("match");
- return FindLastIndex<T> (array, startIndex, array.Length - startIndex, match);
+ return GetLastIndex (array, 0, startIndex + 1, match);
}
public static int FindLastIndex<T> (T [] array, int startIndex, int count, Predicate<T> match)
{
if (array == null)
throw new ArgumentNullException ("array");
+
if (match == null)
throw new ArgumentNullException ("match");
+
+ if (startIndex < 0 || (uint) startIndex > (uint) array.Length)
+ throw new ArgumentOutOfRangeException ("startIndex");
- if (startIndex > array.Length || startIndex + count > array.Length)
- throw new ArgumentOutOfRangeException ();
-
- for (int i = startIndex + count - 1; i >= startIndex; i--)
- if (match (array [i]))
+ if (count < 0)
+ throw new ArgumentOutOfRangeException ("count");
+
+ if (startIndex - count + 1 < 0)
+ throw new ArgumentOutOfRangeException ("count must refer to a location within the array");
+
+ return GetLastIndex (array, startIndex - count + 1, count, match);
+ }
+
+ internal static int GetLastIndex<T> (T[] array, int startIndex, int count, Predicate<T> match)
+ {
+ // unlike FindLastIndex, takes regular params for search range
+ for (int i = startIndex + count; i != startIndex;)
+ if (match (array [--i]))
return i;
-
+
return -1;
}
@@ -2897,16 +2919,25 @@ namespace System
{
if (array == null)
throw new ArgumentNullException ("array");
+
+ if (match == null)
+ throw new ArgumentNullException ("match");
- return FindIndex<T> (array, 0, array.Length, match);
+ return GetIndex (array, 0, array.Length, match);
}
public static int FindIndex<T> (T [] array, int startIndex, Predicate<T> match)
{
if (array == null)
throw new ArgumentNullException ("array");
-
- return FindIndex<T> (array, startIndex, array.Length - startIndex, match);
+
+ if (startIndex < 0 || (uint) startIndex > (uint) array.Length)
+ throw new ArgumentOutOfRangeException ("startIndex");
+
+ if (match == null)
+ throw new ArgumentNullException ("match");
+
+ return GetIndex (array, startIndex, array.Length - startIndex, match);
}
public static int FindIndex<T> (T [] array, int startIndex, int count, Predicate<T> match)
@@ -2914,13 +2945,22 @@ namespace System
if (array == null)
throw new ArgumentNullException ("array");
- if (match == null)
- throw new ArgumentNullException ("match");
+ if (startIndex < 0)
+ throw new ArgumentOutOfRangeException ("startIndex");
- if (startIndex > array.Length || startIndex + count > array.Length)
- throw new ArgumentOutOfRangeException ();
-
- for (int i = startIndex; i < startIndex + count; i ++)
+ if (count < 0)
+ throw new ArgumentOutOfRangeException ("count");
+
+ if ((uint) startIndex + (uint) count > (uint) array.Length)
+ throw new ArgumentOutOfRangeException ("index and count exceed length of list");
+
+ return GetIndex (array, startIndex, count, match);
+ }
+
+ internal static int GetIndex<T> (T[] array, int startIndex, int count, Predicate<T> match)
+ {
+ int end = startIndex + count;
+ for (int i = startIndex; i < end; i ++)
if (match (array [i]))
return i;