Skip to content

Commit 0bb5885

Browse files
committed
Use C# 11 list patterns.
Add IndexRange package to support this on older frameworks, then adopt index and range notation when possible.
1 parent 9edfc29 commit 0bb5885

20 files changed

+91
-88
lines changed

.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ dotnet_diagnostic.CA5398.severity = none # Avoid hardcoded SslProtocols values.
8484
dotnet_diagnostic.SA1003.severity = none # Operator must not be followed by whitespace.
8585
dotnet_diagnostic.SA1008.severity = none # Opening parenthesis must not be preceded by a space.
8686
dotnet_diagnostic.SA1009.severity = none # Closing parenthesis must not be followed by a space.
87+
dotnet_diagnostic.SA1010.severity = silent # https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3503
88+
dotnet_diagnostic.SA1011.severity = silent # https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3503
8789
dotnet_diagnostic.SA1027.severity = none # Tabs must not be used.
8890
dotnet_diagnostic.SA1101.severity = none # Prefix local calls with this.
8991
dotnet_diagnostic.SA1116.severity = none # Split parameters should start on line after declaration.

src/MySqlConnector/Core/BatchedCommandPayloadCreator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public bool WriteQueryCommand(ref CommandListPosition commandListPosition, IDict
2727
var commandLength = writer.Position - position - padding.Length;
2828
var span = writer.ArraySegment.AsSpan().Slice(position);
2929
span[0] = 0xFE;
30-
BinaryPrimitives.WriteUInt64LittleEndian(span.Slice(1), (ulong) commandLength);
30+
BinaryPrimitives.WriteUInt64LittleEndian(span[1..], (ulong) commandLength);
3131
} while (wroteCommand);
3232

3333
// remove the padding that was saved for the final command (which wasn't written)

src/MySqlConnector/Core/BinaryRow.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private object ReadDateTime(ReadOnlySpan<byte> value)
186186
second = value[6];
187187
}
188188

189-
var microseconds = value.Length <= 7 ? 0 : MemoryMarshal.Read<int>(value.Slice(7));
189+
var microseconds = value.Length <= 7 ? 0 : MemoryMarshal.Read<int>(value[7..]);
190190

191191
try
192192
{
@@ -209,11 +209,11 @@ private static TimeSpan ReadTimeSpan(ReadOnlySpan<byte> value)
209209
return TimeSpan.Zero;
210210

211211
var isNegative = value[0];
212-
var days = MemoryMarshal.Read<int>(value.Slice(1));
212+
var days = MemoryMarshal.Read<int>(value[1..]);
213213
var hours = (int) value[5];
214214
var minutes = (int) value[6];
215215
var seconds = (int) value[7];
216-
var microseconds = value.Length == 8 ? 0 : MemoryMarshal.Read<int>(value.Slice(8));
216+
var microseconds = value.Length == 8 ? 0 : MemoryMarshal.Read<int>(value[8..]);
217217

218218
if (isNegative != 0)
219219
{

src/MySqlConnector/Core/Row.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOff
135135
var offset = (int) dataOffset;
136136
var lengthToCopy = Math.Max(0, Math.Min(m_dataLengths[ordinal] - offset, length));
137137
if (lengthToCopy > 0)
138-
m_data.Slice(m_dataOffsets[ordinal] + offset, lengthToCopy).Span.CopyTo(buffer.AsSpan().Slice(bufferOffset));
138+
m_data.Slice(m_dataOffsets[ordinal] + offset, lengthToCopy).Span.CopyTo(buffer.AsSpan()[bufferOffset..]);
139139
return lengthToCopy;
140140
}
141141

@@ -487,11 +487,11 @@ protected object ParseDateTime(ReadOnlySpan<byte> value)
487487
goto InvalidDateTime;
488488
if (value.Length < 5 || value[4] != 45)
489489
goto InvalidDateTime;
490-
if (!Utf8Parser.TryParse(value.Slice(5), out int month, out bytesConsumed) || bytesConsumed != 2)
490+
if (!Utf8Parser.TryParse(value[5..], out int month, out bytesConsumed) || bytesConsumed != 2)
491491
goto InvalidDateTime;
492492
if (value.Length < 8 || value[7] != 45)
493493
goto InvalidDateTime;
494-
if (!Utf8Parser.TryParse(value.Slice(8), out int day, out bytesConsumed) || bytesConsumed != 2)
494+
if (!Utf8Parser.TryParse(value[8..], out int day, out bytesConsumed) || bytesConsumed != 2)
495495
goto InvalidDateTime;
496496

497497
if (year == 0 && month == 0 && day == 0)
@@ -515,15 +515,15 @@ protected object ParseDateTime(ReadOnlySpan<byte> value)
515515
{
516516
if (value[10] != 32)
517517
goto InvalidDateTime;
518-
if (!Utf8Parser.TryParse(value.Slice(11), out hour, out bytesConsumed) || bytesConsumed != 2)
518+
if (!Utf8Parser.TryParse(value[11..], out hour, out bytesConsumed) || bytesConsumed != 2)
519519
goto InvalidDateTime;
520520
if (value.Length < 14 || value[13] != 58)
521521
goto InvalidDateTime;
522-
if (!Utf8Parser.TryParse(value.Slice(14), out minute, out bytesConsumed) || bytesConsumed != 2)
522+
if (!Utf8Parser.TryParse(value[14..], out minute, out bytesConsumed) || bytesConsumed != 2)
523523
goto InvalidDateTime;
524524
if (value.Length < 17 || value[16] != 58)
525525
goto InvalidDateTime;
526-
if (!Utf8Parser.TryParse(value.Slice(17), out second, out bytesConsumed) || bytesConsumed != 2)
526+
if (!Utf8Parser.TryParse(value[17..], out second, out bytesConsumed) || bytesConsumed != 2)
527527
goto InvalidDateTime;
528528

529529
if (value.Length == 19)
@@ -535,7 +535,7 @@ protected object ParseDateTime(ReadOnlySpan<byte> value)
535535
if (value[19] != 46)
536536
goto InvalidDateTime;
537537

538-
if (!Utf8Parser.TryParse(value.Slice(20), out microseconds, out bytesConsumed) || bytesConsumed != value.Length - 20)
538+
if (!Utf8Parser.TryParse(value[20..], out microseconds, out bytesConsumed) || bytesConsumed != value.Length - 20)
539539
goto InvalidDateTime;
540540
for (; bytesConsumed < 6; bytesConsumed++)
541541
microseconds *= 10;

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,20 @@ public async Task PrepareAsync(IMySqlCommand command, IOBehavior ioBehavior, Can
168168
buffer[2] = 'L';
169169
buffer[3] = 'L';
170170
buffer[4] = ' ';
171-
buffer = buffer.Slice(5);
171+
buffer = buffer[5..];
172172
state.commandText.AsSpan().CopyTo(buffer);
173-
buffer = buffer.Slice(state.commandText.Length);
173+
buffer = buffer[state.commandText.Length..];
174174
buffer[0] = '(';
175-
buffer = buffer.Slice(1);
175+
buffer = buffer[1..];
176176
if (state.parameterCount > 0)
177177
{
178178
buffer[0] = '?';
179-
buffer = buffer.Slice(1);
179+
buffer = buffer[1..];
180180
for (var i = 1; i < state.parameterCount; i++)
181181
{
182182
buffer[0] = ',';
183183
buffer[1] = '?';
184-
buffer = buffer.Slice(2);
184+
buffer = buffer[2..];
185185
}
186186
}
187187
buffer[0] = ')';
@@ -238,7 +238,7 @@ public async Task PrepareAsync(IMySqlCommand command, IOBehavior ioBehavior, Can
238238
payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
239239
var payloadLength = payload.Span.Length;
240240
Utility.Resize(ref columnsAndParameters, columnsAndParametersSize + payloadLength);
241-
payload.Span.CopyTo(columnsAndParameters.Array.AsSpan().Slice(columnsAndParametersSize));
241+
payload.Span.CopyTo(columnsAndParameters.Array.AsSpan()[columnsAndParametersSize..]);
242242
parameters[i] = ColumnDefinitionPayload.Create(new(columnsAndParameters, columnsAndParametersSize, payloadLength));
243243
columnsAndParametersSize += payloadLength;
244244
}
@@ -258,7 +258,7 @@ public async Task PrepareAsync(IMySqlCommand command, IOBehavior ioBehavior, Can
258258
payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
259259
var payloadLength = payload.Span.Length;
260260
Utility.Resize(ref columnsAndParameters, columnsAndParametersSize + payloadLength);
261-
payload.Span.CopyTo(columnsAndParameters.Array.AsSpan().Slice(columnsAndParametersSize));
261+
payload.Span.CopyTo(columnsAndParameters.Array.AsSpan()[columnsAndParametersSize..]);
262262
columns[i] = ColumnDefinitionPayload.Create(new(columnsAndParameters, columnsAndParametersSize, payloadLength));
263263
columnsAndParametersSize += payloadLength;
264264
}
@@ -499,7 +499,7 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
499499

500500
// second packet: SET NAMES query
501501
m_pipelinedResetConnectionBytes[5] = (byte) m_setNamesPayload.Span.Length;
502-
m_setNamesPayload.Span.CopyTo(m_pipelinedResetConnectionBytes.AsSpan().Slice(9));
502+
m_setNamesPayload.Span.CopyTo(m_pipelinedResetConnectionBytes.AsSpan()[9..]);
503503
}
504504
}
505505

src/MySqlConnector/Core/ServerVersion.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ public ServerVersion(ReadOnlySpan<byte> versionString)
1414
var build = 0;
1515
if (Utf8Parser.TryParse(versionString, out int major, out var bytesConsumed))
1616
{
17-
versionString = versionString.Slice(bytesConsumed);
18-
if (versionString.Length > 0 && versionString[0] == 0x2E)
17+
versionString = versionString[bytesConsumed..];
18+
if (versionString is [ 0x2E, ..])
1919
{
20-
versionString = versionString.Slice(1);
20+
versionString = versionString[1..];
2121
if (Utf8Parser.TryParse(versionString, out minor, out bytesConsumed))
2222
{
23-
versionString = versionString.Slice(bytesConsumed);
24-
if (versionString.Length > 0 && versionString[0] == 0x2E)
23+
versionString = versionString[bytesConsumed..];
24+
if (versionString is [ 0x2E, .. ])
2525
{
26-
versionString = versionString.Slice(1);
26+
versionString = versionString[1..];
2727
if (Utf8Parser.TryParse(versionString, out build, out bytesConsumed))
2828
{
29-
versionString = versionString.Slice(bytesConsumed);
29+
versionString = versionString[bytesConsumed..];
3030
}
3131
}
3232
}
@@ -36,21 +36,21 @@ public ServerVersion(ReadOnlySpan<byte> versionString)
3636
Version = new Version(major, minor, build);
3737

3838
// check for MariaDB version appended to a fake MySQL version
39-
if (versionString.Length != 0 && versionString[0] == 0x2D)
39+
if (versionString is [ 0x2D, .. ])
4040
{
41-
versionString = versionString.Slice(1);
41+
versionString = versionString[1..];
4242
ReadOnlySpan<byte> mariaDb = "-MariaDB";
4343
var mariaDbIndex = versionString.IndexOf(mariaDb);
4444
if (mariaDbIndex != -1)
4545
{
4646
var totalBytesRead = 0;
4747
if (Utf8Parser.TryParse(versionString, out major, out bytesConsumed) && versionString[bytesConsumed] == 0x2E)
4848
{
49-
versionString = versionString.Slice(bytesConsumed + 1);
49+
versionString = versionString[(bytesConsumed + 1)..];
5050
totalBytesRead += bytesConsumed + 1;
5151
if (Utf8Parser.TryParse(versionString, out minor, out bytesConsumed) && versionString[bytesConsumed] == 0x2E)
5252
{
53-
versionString = versionString.Slice(bytesConsumed + 1);
53+
versionString = versionString[(bytesConsumed + 1)..];
5454
totalBytesRead += bytesConsumed + 1;
5555
if (Utf8Parser.TryParse(versionString, out build, out bytesConsumed) && versionString[bytesConsumed] == 0x2D)
5656
{

src/MySqlConnector/MySqlBulkCopy.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ await m_valuesEnumerator.MoveNextAsync().ConfigureAwait(false) :
405405

406406
var inputIndex = 0;
407407
var bytesWritten = 0;
408-
while (outputIndex >= maxLength || !WriteValue(m_connection, values[valueIndex], ref inputIndex, ref utf8Encoder, buffer.AsSpan(0, maxLength).Slice(outputIndex), out bytesWritten))
408+
while (outputIndex >= maxLength || !WriteValue(m_connection, values[valueIndex], ref inputIndex, ref utf8Encoder, buffer.AsSpan(0, maxLength)[outputIndex..], out bytesWritten))
409409
{
410410
var payload = new PayloadData(new ArraySegment<byte>(buffer, 0, outputIndex + bytesWritten));
411411
await m_connection.Session.SendReplyAsync(payload, ioBehavior, cancellationToken).ConfigureAwait(false);
@@ -643,7 +643,7 @@ static bool WriteSubstring(string value, ref int inputIndex, ref Encoder? utf8En
643643

644644
output[0] = (byte) '\\';
645645
output[1] = (byte) value[inputIndex];
646-
output = output.Slice(2);
646+
output = output[2..];
647647
bytesWritten += 2;
648648
inputIndex++;
649649
}
@@ -659,7 +659,7 @@ static bool WriteSubstring(string value, ref int inputIndex, ref Encoder? utf8En
659659
utf8Encoder.Convert(value.AsSpan(inputIndex, nextIndex - inputIndex), output, nextIndex == value.Length, out var charsUsed, out var bytesUsed, out var completed);
660660

661661
bytesWritten += bytesUsed;
662-
output = output.Slice(bytesUsed);
662+
output = output[bytesUsed..];
663663
inputIndex += charsUsed;
664664

665665
if (!completed)
@@ -679,7 +679,7 @@ static bool WriteBytes(ReadOnlySpan<byte> value, ref int inputIndex, Span<byte>
679679
var by = value[inputIndex];
680680
output[0] = hex[(by >> 4) & 0xF];
681681
output[1] = hex[by & 0xF];
682-
output = output.Slice(2);
682+
output = output[2..];
683683
bytesWritten += 2;
684684
}
685685

src/MySqlConnector/MySqlCommandBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ protected override void SetRowUpdatingHandler(DbDataAdapter adapter)
8686

8787
public override string UnquoteIdentifier(string quotedIdentifier)
8888
{
89-
if (quotedIdentifier.Length >= 2 && quotedIdentifier[0] == QuotePrefix[0] && quotedIdentifier[quotedIdentifier.Length - 1] == QuoteSuffix[0])
90-
quotedIdentifier = quotedIdentifier.Substring(1, quotedIdentifier.Length - 2);
89+
if (quotedIdentifier is [ '`', .., '`' ])
90+
quotedIdentifier = quotedIdentifier[1..^1];
9191
return quotedIdentifier.Replace("``", "`");
9292
}
9393

src/MySqlConnector/MySqlConnector.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
</ItemGroup>
2222

2323
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' OR '$(TargetFramework)' == 'net461' OR '$(TargetFramework)' == 'net471' OR '$(TargetFramework)' == 'netstandard2.0' ">
24+
<PackageReference Include="IndexRange" Version="1.0.2" PrivateAssets="All" />
2425
<PackageReference Include="System.Buffers" Version="4.5.1" />
2526
<PackageReference Include="System.Memory" Version="4.5.4" />
2627
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.3.0" />

src/MySqlConnector/MySqlGeometry.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static MySqlGeometry FromWkb(int srid, ReadOnlySpan<byte> wkb)
1717
{
1818
var bytes = new byte[wkb.Length + 4];
1919
BinaryPrimitives.WriteInt32LittleEndian(bytes, srid);
20-
wkb.CopyTo(bytes.AsSpan().Slice(4));
20+
wkb.CopyTo(bytes.AsSpan()[4..]);
2121
return new MySqlGeometry(bytes);
2222
}
2323

@@ -37,7 +37,7 @@ public static MySqlGeometry FromWkb(int srid, ReadOnlySpan<byte> wkb)
3737
/// <summary>
3838
/// The Well-known Binary serialization of this geometry.
3939
/// </summary>
40-
public ReadOnlySpan<byte> WKB => ValueSpan.Slice(4);
40+
public ReadOnlySpan<byte> WKB => ValueSpan[4..];
4141

4242
/// <summary>
4343
/// The internal MySQL form of this geometry.

0 commit comments

Comments
 (0)