-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayExtensions.cs
55 lines (51 loc) · 1.71 KB
/
ArrayExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Text;
namespace stdlibXtf
{
/// <summary>
/// A collection of methods for the arrays
/// </summary>
public static class ArrayExtensions
{
/// <summary>
/// Extract an array from this array
/// </summary>
/// <param name="values"></param>
/// <param name="startIndex">The start index</param>
/// <param name="length">The number of elements to extract</param>
/// <returns></returns>
public static byte[] SubArray(this byte[] values, long startIndex, long length)
{
if (values == null) { throw new ArgumentNullException(); }
// Prepare the sub array
byte[] sub = new byte[length];
// Make some checks
if (values.LongLength >= (startIndex + length))
{
// copy the data from this array to the sub array
for (int d = 0; d < length; d++)
{
sub[d] = values[d + startIndex];
}
// return the sub array
return sub;
}
else
{
if (values.LongLength > startIndex) // Extract to the end of array
{
// copy the data from this array to the sub array
for (int d = 0; d < (values.LongLength - startIndex); d++)
{
sub[d] = values[d + startIndex];
}
// return the sub array
return sub;
}
else
{ throw new IndexOutOfRangeException(); }
}
}
}
}