Skip to content

Commit 8d36b47

Browse files
committed
Add ArraySegment.Slice extension method.
API modelled off proposed Span<T> API: https://github.com/dotnet/corefxlab/blob/master/docs/Span.md#api-surface
1 parent 2cc688b commit 8d36b47

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

src/MySqlConnector/Protocol/Serialization/BufferedByteReader.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public ValueTask<ArraySegment<byte>> ReadBytesAsync(IByteHandler byteHandler, in
99
{
1010
if (m_remainingData.Count >= count)
1111
{
12-
var readBytes = new ArraySegment<byte>(m_remainingData.Array, m_remainingData.Offset, count);
13-
m_remainingData = new ArraySegment<byte>(m_remainingData.Array, m_remainingData.Offset + count, m_remainingData.Count - count);
12+
var readBytes = m_remainingData.Slice(0, count);
13+
m_remainingData = m_remainingData.Slice(count);
1414
return new ValueTask<ArraySegment<byte>>(readBytes);
1515
}
1616

@@ -35,8 +35,8 @@ private ValueTask<ArraySegment<byte>> ReadBytesAsync(IByteHandler byteHandler, A
3535

3636
if (previousReadBytes.Array == null && readBytes.Count >= count)
3737
{
38-
m_remainingData = new ArraySegment<byte>(readBytes.Array, readBytes.Offset + count, readBytes.Count - count);
39-
return new ValueTask<ArraySegment<byte>>(new ArraySegment<byte>(readBytes.Array, readBytes.Offset, count));
38+
m_remainingData = readBytes.Slice(count);
39+
return new ValueTask<ArraySegment<byte>>(readBytes.Slice(0, count));
4040
}
4141

4242
var previousReadBytesArray = previousReadBytes.Array;
@@ -50,8 +50,8 @@ private ValueTask<ArraySegment<byte>> ReadBytesAsync(IByteHandler byteHandler, A
5050

5151
if (previousReadBytes.Count >= count)
5252
{
53-
m_remainingData = new ArraySegment<byte>(previousReadBytes.Array, previousReadBytes.Offset + count, previousReadBytes.Count - count);
54-
return new ValueTask<ArraySegment<byte>>(new ArraySegment<byte>(previousReadBytes.Array, previousReadBytes.Offset, count));
53+
m_remainingData = previousReadBytes.Slice(count);
54+
return new ValueTask<ArraySegment<byte>>(previousReadBytes.Slice(0, count));
5555
}
5656

5757
return ReadBytesAsync(byteHandler, previousReadBytes, count, ioBehavior);

src/MySqlConnector/Protocol/Serialization/CompressedPayloadHandler.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private ValueTask<ArraySegment<byte>> ReadBytesAsync(int count, ProtocolErrorBeh
6060
{
6161
int bytesToRead = Math.Min(m_remainingData.Count, count);
6262
var result = new ArraySegment<byte>(m_remainingData.Array, m_remainingData.Offset, bytesToRead);
63-
m_remainingData = new ArraySegment<byte>(m_remainingData.Array, m_remainingData.Offset + bytesToRead, m_remainingData.Count - bytesToRead);
63+
m_remainingData = m_remainingData.Slice(bytesToRead);
6464
return new ValueTask<ArraySegment<byte>>(result);
6565
}
6666

@@ -155,8 +155,8 @@ private ValueTask<ArraySegment<byte>> ReadBytesAsync(int count, ProtocolErrorBeh
155155
}
156156
}
157157

158-
var result = new ArraySegment<byte>(m_remainingData.Array, m_remainingData.Offset, count);
159-
m_remainingData = new ArraySegment<byte>(m_remainingData.Array, m_remainingData.Offset + count, m_remainingData.Count - count);
158+
var result = m_remainingData.Slice(0, count);
159+
m_remainingData = m_remainingData.Slice(count);
160160
return new ValueTask<ArraySegment<byte>>(result);
161161
});
162162
});
@@ -202,7 +202,7 @@ private ValueTask<int> CompressAndWrite(ArraySegment<byte> remainingUncompressed
202202
{
203203
// setting the length to 0 indicates sending uncompressed data
204204
uncompressedLength = 0;
205-
compressedData = new ArraySegment<byte>(remainingUncompressedData.Array, remainingUncompressedData.Offset, remainingUncompressedBytes);
205+
compressedData = remainingUncompressedData.Slice(0, remainingUncompressedBytes);
206206
}
207207

208208
var buffer = new byte[compressedData.Count + 7];
@@ -211,7 +211,7 @@ private ValueTask<int> CompressAndWrite(ArraySegment<byte> remainingUncompressed
211211
SerializationUtility.WriteUInt32(uncompressedLength, buffer, 4, 3);
212212
Buffer.BlockCopy(compressedData.Array, compressedData.Offset, buffer, 7, compressedData.Count);
213213

214-
remainingUncompressedData = new ArraySegment<byte>(remainingUncompressedData.Array, remainingUncompressedData.Offset + remainingUncompressedBytes, remainingUncompressedData.Count - remainingUncompressedBytes);
214+
remainingUncompressedData = remainingUncompressedData.Slice(remainingUncompressedBytes);
215215
return m_byteHandler.WriteBytesAsync(new ArraySegment<byte>(buffer, 0, buffer.Length), ioBehavior)
216216
.ContinueWith(_ => remainingUncompressedData.Count == 0 ? default(ValueTask<int>) :
217217
CompressAndWrite(remainingUncompressedData, ioBehavior));

src/MySqlConnector/Utility.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ public static string FormatInvariant(this string format, params object[] args)
2525
public static string GetString(this Encoding encoding, ArraySegment<byte> arraySegment)
2626
=> encoding.GetString(arraySegment.Array, arraySegment.Offset, arraySegment.Count);
2727

28+
/// <summary>
29+
/// Returns a new <see cref="ArraySegment{T}"/> that starts at index <paramref name="index"/> into <paramref name="arraySegment"/>.
30+
/// </summary>
31+
/// <param name="arraySegment">The <see cref="ArraySegment{T}"/> from which to create a slice.</param>
32+
/// <param name="index">The non-negative, zero-based starting index of the new slice (relative to <see cref="ArraySegment{T}.Offset"/> of <paramref name="arraySegment"/>.</param>
33+
/// <returns>A new <see cref="ArraySegment{T}"/> starting at the <paramref name="index"/>th element of <paramref name="arraySegment"/> and continuing to the end of <paramref name="arraySegment"/>.</returns>
34+
public static ArraySegment<T> Slice<T>(this ArraySegment<T> arraySegment, int index) =>
35+
new ArraySegment<T>(arraySegment.Array, arraySegment.Offset + index, arraySegment.Count - index);
36+
37+
/// <summary>
38+
/// Returns a new <see cref="ArraySegment{T}"/> that starts at index <paramref name="index"/> into <paramref name="arraySegment"/> and has a length of <paramref name="length"/>.
39+
/// </summary>
40+
/// <param name="arraySegment">The <see cref="ArraySegment{T}"/> from which to create a slice.</param>
41+
/// <param name="index">The non-negative, zero-based starting index of the new slice (relative to <see cref="ArraySegment{T}.Offset"/> of <paramref name="arraySegment"/>.</param>
42+
/// <param name="length">The non-negative length of the new slice.</param>
43+
/// <returns>A new <see cref="ArraySegment{T}"/> of length <paramref name="length"/>, starting at the <paramref name="index"/>th element of <paramref name="arraySegment"/>.</returns>
44+
public static ArraySegment<T> Slice<T>(this ArraySegment<T> arraySegment, int index, int length) =>
45+
new ArraySegment<T>(arraySegment.Array, arraySegment.Offset + index, length);
46+
2847
#if NET45
2948
public static bool TryGetBuffer(this MemoryStream memoryStream, out ArraySegment<byte> buffer)
3049
{

0 commit comments

Comments
 (0)