Skip to content

Commit d832156

Browse files
committed
Use Span<byte> on platforms that support it.
1 parent 8954cbb commit d832156

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,11 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
13721372
// load the certificate at this index; note that 'new X509Certificate' stops at the end of the first certificate it loads
13731373
m_logArguments[1] = index;
13741374
Log.Trace("Session{0} loading certificate at Index {1} in the CA certificate file.", m_logArguments);
1375+
#if NET5_0_OR_GREATER
1376+
var caCertificate = new X509Certificate2(certificateBytes.AsSpan(index, (nextIndex == -1 ? certificateBytes.Length : nextIndex) - index), default(ReadOnlySpan<char>), X509KeyStorageFlags.MachineKeySet);
1377+
#else
13751378
var caCertificate = new X509Certificate2(Utility.ArraySlice(certificateBytes, index, (nextIndex == -1 ? certificateBytes.Length : nextIndex) - index), default(string), X509KeyStorageFlags.MachineKeySet);
1379+
#endif
13761380
certificateChain.ChainPolicy.ExtraStore.Add(caCertificate);
13771381
}
13781382
catch (CryptographicException ex)

src/MySqlConnector/Utilities/Utility.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ public static ArraySegment<T> Slice<T>(this ArraySegment<T> arraySegment, int in
299299
new ArraySegment<T>(arraySegment.Array!, arraySegment.Offset + index, length);
300300
#endif
301301

302+
#if !NET5_0_OR_GREATER
302303
/// <summary>
303304
/// Returns a new <see cref="byte"/> array that is a slice of <paramref name="input"/> starting at <paramref name="offset"/>.
304305
/// </summary>
@@ -314,6 +315,7 @@ public static byte[] ArraySlice(byte[] input, int offset, int length)
314315
Array.Copy(input, offset, slice, 0, slice.Length);
315316
return slice;
316317
}
318+
#endif
317319

318320
/// <summary>
319321
/// Finds the next index of <paramref name="pattern"/> in <paramref name="data"/>, starting at index <paramref name="offset"/>.
@@ -324,19 +326,8 @@ public static byte[] ArraySlice(byte[] input, int offset, int length)
324326
/// <returns>The offset of <paramref name="pattern"/> within <paramref name="data"/>, or <c>-1</c> if <paramref name="pattern"/> was not found.</returns>
325327
public static int FindNextIndex(ReadOnlySpan<byte> data, int offset, ReadOnlySpan<byte> pattern)
326328
{
327-
var limit = data.Length - pattern.Length;
328-
for (var start = offset; start <= limit; start++)
329-
{
330-
var i = 0;
331-
for (; i < pattern.Length; i++)
332-
{
333-
if (data[start + i] != pattern[i])
334-
break;
335-
}
336-
if (i == pattern.Length)
337-
return start;
338-
}
339-
return -1;
329+
var index = MemoryExtensions.IndexOf(data.Slice(offset), pattern);
330+
return index == -1 ? -1 : offset + index;
340331
}
341332

342333
/// <summary>

0 commit comments

Comments
 (0)