-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArrayFunctions.cs
More file actions
196 lines (191 loc) · 14.2 KB
/
Copy pathArrayFunctions.cs
File metadata and controls
196 lines (191 loc) · 14.2 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Idmr.Common.dll, Library file with common IDMR resources
* Copyright (C) 2007-2014 Michael Gaisser (mjgaisser@gmail.com)
* Licensed under the MPL v2.0 or later
*
* Full notice in help/Idmr.Common.chm
* Version: 1.3
*/
/* CHANGELOG
* v1.3, 141214
* [UPD] ASCII encoding changed to UTF8
* [UPD] switch to MPL
* v1.2, 121024
* [NEW] WriteToArray(short[], Array, ref int)
* v1.1, XXXXXX
* [NEW] WriteToArray(short[], Array, int)
* [UPD] TrimArray.fullArray converted to Array type
* [UPD] WriteToArray.array converted to Array type
* [UPD] class is static
* v1.0, XXXXXX
* - Release
*/
using System;
namespace Idmr.Common
{
/// <summary>Primarily a wrapper for Buffer.BlockCopy</summary>
public static class ArrayFunctions
{
/// <summary>Retrieves a string from a byte array.</summary>
/// <param name="array">The raw byte array.</param>
/// <param name="offset">The location of the starting character.</param>
/// <param name="length">The number of characters to read.</param>
/// <remarks>String is assumed to be UTF8-encoded, trims null chars.</remarks>
/// <exception cref="ArgumentNullException"><i>array</i> is <b>null</b>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><i>offset</i> or <i>length</i> are less than zero<br/><b>-or-</b><br/><i>offset</i> or <i>length</i> result in a range outside the range of <i>array</i>.</exception>
/// <returns>An UTF8-encoded string.</returns>
public static string ReadStringFromArray(byte[] array, int offset, int length) { return System.Text.Encoding.UTF8.GetString(array, offset, length).Trim('\0'); }
#region TrimArray
/// <summary>Using <i>fullArray</i> starting at <i>offset</i>, fill <i>trimmedArray</i>.</summary>
/// <remarks>Is a simple wrapper for <see cref="Buffer.BlockCopy"/>.</remarks>
/// <param name="fullArray">The original array.</param>
/// <param name="offset">The starting location in <i>fullArray</i>.</param>
/// <param name="trimmedArray">The array to fill with copied values.</param>
/// <exception cref="ArgumentException"><i>fullArray</i> is not an array of primitives<br/><b>-or-</b><br/>The length of <i>fullArray</i> is less than <i>offset</i> plus the length of <i>trimmedArray</i>.</exception>
/// <exception cref="ArgumentNullException"><i>fullArray</i> or <i>trimmedArray</i> are <b>null</b>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><i>offset</i> is less than zero.</exception>
public static void TrimArray(Array fullArray, int offset, byte[] trimmedArray)
{
Buffer.BlockCopy(fullArray, offset, trimmedArray, 0, trimmedArray.Length);
}
/// <summary>Using <i>fullArray</i> starting at <i>offset</i>, fill <i>trimmedArray</i>.</summary>
/// <remarks>Is a simple wrapper for <see cref="Buffer.BlockCopy"/>.</remarks>
/// <param name="fullArray">The original array.</param>
/// <param name="offset">The starting location in fullArray.</param>
/// <param name="trimmedArray">The array to fill with copied values.</param>
/// <exception cref="ArgumentException"><i>fullArray</i> is not an array of primitives<br/><b>-or-</b><br/>The length of <i>fullArray</i> is less than <i>offset</i> plus the length of <i>trimmedArray</i>.</exception>
/// <exception cref="ArgumentNullException"><i>fullArray</i> or <i>trimmedArray</i> are <b>null</b>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><i>offset</i> is less than zero.</exception>
public static void TrimArray(Array fullArray, int offset, short[] trimmedArray)
{
Buffer.BlockCopy(fullArray, offset, trimmedArray, 0, trimmedArray.Length << 1);
}
/// <summary>Using <i>fullArray</i> starting at <i>offset</i>, fill <i>trimmedArray</i>.</summary>
/// <remarks>Is a simple wrapper for <see cref="Buffer.BlockCopy"/>.</remarks>
/// <param name="fullArray">The original array.</param>
/// <param name="offset">The starting location in fullArray.</param>
/// <param name="trimmedArray">The array to fill with copied values.</param>
/// <exception cref="ArgumentException"><i>fullArray</i> is not an array of primitives<br/><b>-or-</b><br/>The length of <i>fullArray</i> is less than <i>offset</i> plus the length of <i>trimmedArray</i>.</exception>
/// <exception cref="ArgumentNullException"><i>fullArray</i> or <i>trimmedArray</i> are <b>null</b>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><i>offset</i> is less than zero.</exception>
public static void TrimArray(Array fullArray, int offset, int[] trimmedArray)
{
Buffer.BlockCopy(fullArray, offset, trimmedArray, 0, trimmedArray.Length << 2);
}
#endregion
#region WriteToArray ref
/// <summary>Copy <i>value</i> to the array at <i>offset</i> and increment.</summary>
/// <remarks>Is a simple wrapper for <see cref="Buffer.BlockCopy"/>.</remarks>
/// <param name="value">The value to be copied to the array.</param>
/// <param name="array">The array to which <i>value</i> is to be copied.</param>
/// <param name="offset">The offset of the starting byte in the array, will be incremented 4.</param>
/// <exception cref="ArgumentException">The length of <i>array</i> is less than <i>offset</i> plus four (4)<br/><b>-or-</b><br/><i>array</i> is not an array of primitives.</exception>
/// <exception cref="ArgumentNullException"><i>array</i> is <b>null</b>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><i>offset</i> is less than zero.</exception>
public static void WriteToArray(int value, Array array, ref int offset)
{
Buffer.BlockCopy(BitConverter.GetBytes(value), 0, array, offset, 4);
offset += 4;
}
/// <summary>Copy <i>value</i> to the array at <i>offset</i> and increment.</summary>
/// <remarks>Is a simple wrapper for <see cref="Buffer.BlockCopy"/>.</remarks>
/// <param name="value">The value to be copied to the array.</param>
/// <param name="array">The array to which <i>value</i> is to be copied.</param>
/// <param name="offset">The offset of the starting byte in the array, will be incremented 2.</param>
/// <exception cref="ArgumentException">The length of <i>array</i> is less than <i>offset</i> plus two (2)<br/><b>-or-</b><br/><i>array</i> is not an array of primitives.</exception>
/// <exception cref="ArgumentNullException"><i>array</i> is <b>null</b>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><i>offset</i> is less than zero.</exception>
public static void WriteToArray(short value, Array array, ref int offset)
{
Buffer.BlockCopy(BitConverter.GetBytes(value), 0, array, offset, 2);
offset += 2;
}
/// <summary>Copy <i>value</i> to the array at <i>offset</i> and increment.</summary>
/// <remarks>Is a simple wrapper for <see cref="Buffer.BlockCopy"/>.</remarks>
/// <param name="value">The value to be copied to the array.</param>
/// <param name="array">The array to which <i>value</i> is to be copied.</param>
/// <param name="offset">The offset of the starting byte in the array, will be incremented the length of the string.</param>
/// <exception cref="ArgumentException">The length of <i>array</i> is less than <i>offset</i> plus the length of <i>value</i><br/><b>-or-</b><br/><i>array</i> is not an array of primitives.</exception>
/// <exception cref="ArgumentNullException"><i>array</i> is <b>null</b>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><i>offset</i> is less than zero.</exception>
public static void WriteToArray(string value, Array array, ref int offset)
{
Buffer.BlockCopy(System.Text.Encoding.UTF8.GetBytes(value), 0, array, offset, value.Length);
offset += value.Length;
}
/// <summary>Copy <i>value</i> to the array at <i>offset</i> and increment.</summary>
/// <remarks>Is a simple wrapper for <see cref="Buffer.BlockCopy"/>.</remarks>
/// <param name="value">The value to be copied to the array, will copy entire array.</param>
/// <param name="array">The array to which <i>value</i> is to be copied.</param>
/// <param name="offset">The offset of the starting byte in the array, will be incremented the length of <i>value</i>.</param>
/// <exception cref="ArgumentException">The length of <i>array</i> is less than <i>offset</i> plus the length of <i>value</i><br/><b>-or-</b><br/><i>array</i> is not an array of primitives.</exception>
/// <exception cref="ArgumentNullException"><i>array</i> or <i>value</i> are <b>null</b>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><i>offset</i> is less than zero.</exception>
public static void WriteToArray(byte[] value, Array array, ref int offset)
{
Buffer.BlockCopy(value, 0, array, offset, value.Length);
offset += value.Length;
}
/// <summary>Copy <i>value</i> to the array at <i>offset</i> and increment.</summary>
/// <remarks>Is a simple wrapper for <see cref="Buffer.BlockCopy"/>.</remarks>
/// <param name="value">The value to be copied to the array, will copy entire array.</param>
/// <param name="array">The array to which <i>value</i> is to be copied.</param>
/// <param name="offset">The offset of the starting byte in the array, will be incremented the byte length of <i>value</i>.</param>
/// <exception cref="ArgumentException">The byte length of <i>array</i> is less than <i>offset</i> plus the byte length of <i>value</i><br/><b>-or-</b><br/><i>array</i> is not an array of primitives.</exception>
/// <exception cref="ArgumentNullException"><i>array</i> or <i>value</i> are <b>null</b>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><i>offset</i> is less than zero.</exception>
public static void WriteToArray(short[] value, Array array, ref int offset)
{
Buffer.BlockCopy(value, 0, array, offset, value.Length << 1);
offset += value.Length << 1;
}
#endregion
#region WriteToArray
/// <summary>Copy <i>value</i> to the array at <i>offset</i>.</summary>
/// <remarks>Is a simple wrapper for <see cref="Buffer.BlockCopy"/>.</remarks>
/// <param name="value">The value to be copied to the array.</param>
/// <param name="array">The array to which <i>value</i> is to be copied.</param>
/// <param name="offset">The offset of the starting byte in the array.</param>
/// <exception cref="ArgumentException">The length of <i>array</i> is less than <i>offset</i> plus four (4)<br/><b>-or-</b><br/><i>array</i> is not an array of primitives.</exception>
/// <exception cref="ArgumentNullException"><i>array</i> is <b>null</b>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><i>offset</i> is less than zero.</exception>
public static void WriteToArray(int value, Array array, int offset) { Buffer.BlockCopy(BitConverter.GetBytes(value), 0, array, offset, 4); }
/// <summary>Copy <i>value</i> to the array at <i>offset</i>.</summary>
/// <remarks>Is a simple wrapper for <see cref="Buffer.BlockCopy"/>.</remarks>
/// <param name="value">The value to be copied to the array.</param>
/// <param name="array">The array to which <i>value</i> is to be copied.</param>
/// <param name="offset">The offset of the starting byte in the array.</param>
/// <exception cref="ArgumentException">The length of <i>array</i> is less than <i>offset</i> plus two (2)<br/><b>-or-</b><br/><i>array</i> is not an array of primitives.</exception>
/// <exception cref="ArgumentNullException"><i>array</i> is <b>null</b>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><i>offset</i> is less than zero.</exception>
public static void WriteToArray(short value, Array array, int offset) { Buffer.BlockCopy(BitConverter.GetBytes(value), 0, array, offset, 2); }
/// <summary>Copy <i>value</i> to the array at <i>offset</i>.</summary>
/// <remarks>Is a simple wrapper for <see cref="Buffer.BlockCopy"/>.</remarks>
/// <param name="value">The value to be copied to the array.</param>
/// <param name="array">The array to which <i>value</i> is to be copied.</param>
/// <param name="offset">The offset of the starting byte in the array.</param>
/// <exception cref="ArgumentException">The length of <i>array</i> is less than <i>offset</i> plus the length of <i>value</i><br/><b>-or-</b><br/><i>array</i> is not an array of primitives.</exception>
/// <exception cref="ArgumentNullException"><i>array</i> is <b>null</b>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><i>offset</i> is less than zero.</exception>
public static void WriteToArray(string value, Array array, int offset) { Buffer.BlockCopy(System.Text.Encoding.UTF8.GetBytes(value), 0, array, offset, value.Length); }
/// <summary>Copy <i>value</i> to the array at <i>offset</i>.</summary>
/// <remarks>Is a simple wrapper for <see cref="Buffer.BlockCopy"/>.</remarks>
/// <param name="value">The value to be copied to the array, will copy entire array.</param>
/// <param name="array">The array to which <i>value</i> is to be copied.</param>
/// <param name="offset">The offset of the starting byte in the array.</param>
/// <exception cref="ArgumentException">The length of <i>array</i> is less than <i>offset</i> plus the length of <i>value</i><br/><b>-or-</b><br/><i>array</i> is not an array of primitives.</exception>
/// <exception cref="ArgumentNullException"><i>array</i> or <i>value</i> are <b>null</b>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><i>offset</i> is less than zero.</exception>
public static void WriteToArray(byte[] value, Array array, int offset) { Buffer.BlockCopy(value, 0, array, offset, value.Length); }
/// <summary>Copy <i>value</i> to the array at <i>offset</i>.</summary>
/// <remarks>Is a simple wrapper for <see cref="Buffer.BlockCopy"/>.</remarks>
/// <param name="value">The value to be copied to the array, will copy entire array.</param>
/// <param name="array">The array to which <i>value</i> is to be copied.</param>
/// <param name="offset">The offset of the starting byte in the array.</param>
/// <exception cref="ArgumentException">The byte length of <i>array</i> is less than <i>offset</i> plus the byte length of <i>value</i><br/><b>-or-</b><br/><i>array</i> is not an array of primitives.</exception>
/// <exception cref="ArgumentNullException"><i>array</i> or <i>value</i> are <b>null</b>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><i>offset</i> is less than zero.</exception>
public static void WriteToArray(short[] value, Array array, int offset) { Buffer.BlockCopy(value, 0, array, offset, value.Length << 1); }
#endregion
}
}