From ecc1ca12aeb7935b7268e752a88c2fa16b875abc Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 10 Oct 2022 12:53:38 -0400 Subject: [PATCH] Combine AsSpan().Slice(...) calls into AsSpan(...) (#76799) --- .../Common/src/System/Net/MultiArrayBuffer.cs | 6 +++--- .../System/IO/StreamConformanceTests.cs | 2 +- .../src/System/Data/Odbc/OdbcHandle.cs | 6 +++--- .../System/Diagnostics/Metrics/Instrument.netcore.cs | 2 +- .../src/System/Diagnostics/Metrics/Instrument.netfx.cs | 10 +++++----- .../src/System/Diagnostics/Metrics/TagList.cs | 4 ++-- .../src/System/Formats/Cbor/CborHelpers.netstandard.cs | 2 +- .../FunctionalTests/SslStreamNetworkStreamTest.cs | 2 +- .../tests/FunctionalTests/SocketAsyncEventArgsTest.cs | 2 +- .../src/System/Globalization/Ordinal.cs | 2 +- .../System/Security/Cryptography/CapiHelper.Windows.cs | 2 +- .../Utf8JsonReaderTests.TryGet.cs | 4 ++-- 12 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/libraries/Common/src/System/Net/MultiArrayBuffer.cs b/src/libraries/Common/src/System/Net/MultiArrayBuffer.cs index 325158f6530c1..ec9d6d68b244e 100644 --- a/src/libraries/Common/src/System/Net/MultiArrayBuffer.cs +++ b/src/libraries/Common/src/System/Net/MultiArrayBuffer.cs @@ -200,16 +200,16 @@ public void GrowAvailableSpace(int byteCount) } byte[]?[] newBlockArray = new byte[]?[blockArraySize]; - _blocks.AsSpan().Slice((int)unusedInitialBlocks, (int)usedBlocks).CopyTo(newBlockArray); + _blocks.AsSpan((int)unusedInitialBlocks, (int)usedBlocks).CopyTo(newBlockArray); _blocks = newBlockArray; } else { // We can shift the array down to make enough space - _blocks.AsSpan().Slice((int)unusedInitialBlocks, (int)usedBlocks).CopyTo(_blocks); + _blocks.AsSpan((int)unusedInitialBlocks, (int)usedBlocks).CopyTo(_blocks); // Null out the part of the array left over from the shift, so that we aren't holding references to those blocks. - _blocks.AsSpan().Slice((int)usedBlocks, (int)unusedInitialBlocks).Clear(); + _blocks.AsSpan((int)usedBlocks, (int)unusedInitialBlocks).Clear(); } uint shift = unusedInitialBlocks * BlockSize; diff --git a/src/libraries/Common/tests/StreamConformanceTests/System/IO/StreamConformanceTests.cs b/src/libraries/Common/tests/StreamConformanceTests/System/IO/StreamConformanceTests.cs index bd702c278c6a6..52be9c00d7790 100644 --- a/src/libraries/Common/tests/StreamConformanceTests/System/IO/StreamConformanceTests.cs +++ b/src/libraries/Common/tests/StreamConformanceTests/System/IO/StreamConformanceTests.cs @@ -3007,7 +3007,7 @@ public virtual async Task ZeroByteRead_PerformsZeroByteReadOnUnderlyingStreamWhe if (FlushGuaranteesAllDataWritten) { - AssertExtensions.SequenceEqual(data, buffer.AsSpan().Slice(0, bytesRead)); + AssertExtensions.SequenceEqual(data, buffer.AsSpan(0, bytesRead)); } } } diff --git a/src/libraries/System.Data.Odbc/src/System/Data/Odbc/OdbcHandle.cs b/src/libraries/System.Data.Odbc/src/System/Data/Odbc/OdbcHandle.cs index 38b8125a6119a..0e6be81299dba 100644 --- a/src/libraries/System.Data.Odbc/src/System/Data/Odbc/OdbcHandle.cs +++ b/src/libraries/System.Data.Odbc/src/System/Data/Odbc/OdbcHandle.cs @@ -188,7 +188,7 @@ internal ODBC32.SQLRETURN GetDiagnosticField(out string sqlState) ODBC.TraceODBC(3, "SQLGetDiagFieldW", retcode); if ((retcode == ODBC32.SQLRETURN.SUCCESS) || (retcode == ODBC32.SQLRETURN.SUCCESS_WITH_INFO)) { - sqlState = new string(buffer.AsSpan().Slice(0, buffer.AsSpan().IndexOf('\0'))); + sqlState = new string(buffer.AsSpan(0, buffer.AsSpan().IndexOf('\0'))); } else { @@ -208,13 +208,13 @@ internal ODBC32.SQLRETURN GetDiagnosticRecord(short record, out string sqlState, if ((retcode == ODBC32.SQLRETURN.SUCCESS) || (retcode == ODBC32.SQLRETURN.SUCCESS_WITH_INFO)) { - sqlState = new string(buffer.AsSpan().Slice(0, buffer.AsSpan().IndexOf('\0'))); + sqlState = new string(buffer.AsSpan(0, buffer.AsSpan().IndexOf('\0'))); } else { sqlState = string.Empty; } - messageBuilder.Append(new string(message.AsSpan().Slice(0, message.AsSpan().IndexOf('\0')))); + messageBuilder.Append(new string(message.AsSpan(0, message.AsSpan().IndexOf('\0')))); return retcode; } } diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Instrument.netcore.cs b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Instrument.netcore.cs index e60b079d0197a..facf6e4cce7e4 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Instrument.netcore.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Instrument.netcore.cs @@ -104,7 +104,7 @@ protected void RecordMeasurement(T measurement, in TagList tagList) KeyValuePair[]? tags = tagList.Tags; if (tags is not null) { - RecordMeasurement(measurement, tags.AsSpan().Slice(0, tagList.Count)); + RecordMeasurement(measurement, tags.AsSpan(0, tagList.Count)); return; } diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Instrument.netfx.cs b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Instrument.netfx.cs index d973afdacafda..990735f023ee5 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Instrument.netfx.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Instrument.netfx.cs @@ -32,7 +32,7 @@ protected void RecordMeasurement(T measurement, KeyValuePair ta var tags = ts_tags ?? new KeyValuePair[MaxTagsCount]; ts_tags = null; tags[0] = tag; - RecordMeasurement(measurement, tags.AsSpan().Slice(0, 1)); + RecordMeasurement(measurement, tags.AsSpan(0, 1)); ts_tags = tags; } @@ -48,7 +48,7 @@ protected void RecordMeasurement(T measurement, KeyValuePair ta ts_tags = null; tags[0] = tag1; tags[1] = tag2; - RecordMeasurement(measurement, tags.AsSpan().Slice(0, 2)); + RecordMeasurement(measurement, tags.AsSpan(0, 2)); ts_tags = tags; } @@ -66,7 +66,7 @@ protected void RecordMeasurement(T measurement, KeyValuePair ta tags[0] = tag1; tags[1] = tag2; tags[2] = tag3; - RecordMeasurement(measurement, tags.AsSpan().Slice(0, 3)); + RecordMeasurement(measurement, tags.AsSpan(0, 3)); ts_tags = tags; } @@ -80,7 +80,7 @@ protected void RecordMeasurement(T measurement, in TagList tagList) KeyValuePair[]? tags = tagList.Tags; if (tags is not null) { - RecordMeasurement(measurement, tags.AsSpan().Slice(0, tagList.Count)); + RecordMeasurement(measurement, tags.AsSpan(0, tagList.Count)); return; } @@ -104,7 +104,7 @@ protected void RecordMeasurement(T measurement, in TagList tagList) ts_tags = null; - RecordMeasurement(measurement, tags.AsSpan().Slice(0, tagList.Count)); + RecordMeasurement(measurement, tags.AsSpan(0, tagList.Count)); ts_tags = tags; } diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/TagList.cs b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/TagList.cs index fb8d1027b4a29..bc1d02525895a 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/TagList.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/TagList.cs @@ -234,7 +234,7 @@ public readonly void CopyTo(Span> tags) if (_overflowTags is not null) { - _overflowTags.AsSpan().Slice(0, _tagsCount).CopyTo(tags); + _overflowTags.AsSpan(0, _tagsCount).CopyTo(tags); return; } @@ -273,7 +273,7 @@ public readonly void CopyTo(KeyValuePair[] array, int arrayInde throw new ArgumentOutOfRangeException(nameof(arrayIndex)); } - CopyTo(array.AsSpan().Slice(arrayIndex)); + CopyTo(array.AsSpan(arrayIndex)); } /// diff --git a/src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/CborHelpers.netstandard.cs b/src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/CborHelpers.netstandard.cs index b4aafd735d5f8..cfe76086ae58d 100644 --- a/src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/CborHelpers.netstandard.cs +++ b/src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/CborHelpers.netstandard.cs @@ -99,7 +99,7 @@ public static BigInteger CreateBigIntegerFromUnsignedBigEndianBytes(byte[] bigEn // you can add a zero-byte value to the most significant side of the array. // Right in this case as it is Big-endian. var bytesPlusOne = new byte[bigEndianBytes.Length + 1]; - bigEndianBytes.CopyTo(bytesPlusOne.AsSpan().Slice(1)); + bigEndianBytes.CopyTo(bytesPlusOne.AsSpan(1)); temp = bytesPlusOne; } else diff --git a/src/libraries/System.Net.Security/tests/FunctionalTests/SslStreamNetworkStreamTest.cs b/src/libraries/System.Net.Security/tests/FunctionalTests/SslStreamNetworkStreamTest.cs index 9e43cfe74ed88..70777d0a447be 100644 --- a/src/libraries/System.Net.Security/tests/FunctionalTests/SslStreamNetworkStreamTest.cs +++ b/src/libraries/System.Net.Security/tests/FunctionalTests/SslStreamNetworkStreamTest.cs @@ -1030,7 +1030,7 @@ public async Task SslStream_RandomSizeWrites_OK(int bufferSize, int readBufferSi } Assert.Equal(bufferSize, totalLength); - AssertExtensions.SequenceEqual(dataToCopy.AsSpan(), dataReceived.AsSpan().Slice(0, totalLength)); + AssertExtensions.SequenceEqual(dataToCopy.AsSpan(), dataReceived.AsSpan(0, totalLength)); }); await TestConfiguration.WhenAllOrAnyFailedWithTimeout(writer, reader); diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketAsyncEventArgsTest.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketAsyncEventArgsTest.cs index e691520ce5b5f..7865f203c0ce3 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketAsyncEventArgsTest.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketAsyncEventArgsTest.cs @@ -583,7 +583,7 @@ public void AcceptAsync_WithReceiveBuffer_Success() Assert.Equal(acceptBufferDataSize, acceptArgs.BytesTransferred); - AssertExtensions.SequenceEqual(sendBuffer, acceptArgs.Buffer.AsSpan().Slice(0, acceptArgs.BytesTransferred)); + AssertExtensions.SequenceEqual(sendBuffer, acceptArgs.Buffer.AsSpan(0, acceptArgs.BytesTransferred)); } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/Ordinal.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/Ordinal.cs index 76d2b06b20b90..dbc72e55bde43 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/Ordinal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/Ordinal.cs @@ -326,7 +326,7 @@ internal static unsafe int LastIndexOf(string source, string value, int startInd if (GlobalizationMode.Invariant) { - return ignoreCase ? InvariantModeCasing.LastIndexOfIgnoreCase(source.AsSpan().Slice(startIndex, count), value) : LastIndexOf(source, value, startIndex, count); + return ignoreCase ? InvariantModeCasing.LastIndexOfIgnoreCase(source.AsSpan(startIndex, count), value) : LastIndexOf(source, value, startIndex, count); } if (GlobalizationMode.UseNls) diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CapiHelper.Windows.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CapiHelper.Windows.cs index 9ec990e578eb3..103aa6ad512bf 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CapiHelper.Windows.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CapiHelper.Windows.cs @@ -98,7 +98,7 @@ internal static string GetDefaultProvider(int dwType) } // check to see if there are upgrades available for the requested CSP - string providerNameString = new string(providerName.AsSpan().Slice(0, providerName.AsSpan().IndexOf('\0'))); + string providerNameString = new string(providerName.AsSpan(0, providerName.AsSpan().IndexOf('\0'))); string? wszUpgrade = null; if (dwType == (int)ProviderType.PROV_RSA_FULL) { diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.TryGet.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.TryGet.cs index 9ae890d35397b..7fab16145eb36 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.TryGet.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.TryGet.cs @@ -1224,7 +1224,7 @@ void Test(ref Utf8JsonReader reader) byte[] buffer = new byte[expectedUtf8Size]; for (int i = 0; i < expectedUtf8Size; i++) { - JsonTestHelper.AssertThrows(ref reader, (ref Utf8JsonReader reader) => reader.CopyString(buffer.AsSpan().Slice(0, i))); + JsonTestHelper.AssertThrows(ref reader, (ref Utf8JsonReader reader) => reader.CopyString(buffer.AsSpan(0, i))); Assert.All(buffer, static b => Assert.Equal(0, b)); } @@ -1249,7 +1249,7 @@ void Test(ref Utf8JsonReader reader) char[] buffer = new char[expectedSize]; for (int i = 0; i < expectedSize; i++) { - JsonTestHelper.AssertThrows(ref reader, (ref Utf8JsonReader reader) => reader.CopyString(buffer.AsSpan().Slice(0, i))); + JsonTestHelper.AssertThrows(ref reader, (ref Utf8JsonReader reader) => reader.CopyString(buffer.AsSpan(0, i))); Assert.All(buffer, static c => Assert.Equal(0, c)); }