From ad8f9686a0d0727bf9d38010d6320269d4940b8f Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Thu, 24 Aug 2017 16:22:02 -0700 Subject: [PATCH 1/2] Remove Span(T[], int) ctor - from corefx issue #23471 --- src/System.Memory/ref/System.Memory.cs | 2 - src/System.Memory/src/System/ReadOnlySpan.cs | 27 ---------- src/System.Memory/src/System/Span.cs | 29 ---------- .../tests/ReadOnlySpan/CtorArray.cs | 20 ------- .../tests/ReadOnlySpan/CtorArrayInt.cs | 54 ------------------- src/System.Memory/tests/Span/CtorArray.cs | 17 ------ src/System.Memory/tests/Span/CtorArrayInt.cs | 54 ------------------- .../tests/System.Memory.Tests.csproj | 2 - 8 files changed, 205 deletions(-) delete mode 100644 src/System.Memory/tests/ReadOnlySpan/CtorArrayInt.cs delete mode 100644 src/System.Memory/tests/Span/CtorArrayInt.cs diff --git a/src/System.Memory/ref/System.Memory.cs b/src/System.Memory/ref/System.Memory.cs index b13ac7a0b582..389fcdb54ec4 100644 --- a/src/System.Memory/ref/System.Memory.cs +++ b/src/System.Memory/ref/System.Memory.cs @@ -13,7 +13,6 @@ public partial struct ReadOnlySpan { public static ReadOnlySpan Empty { get { throw null; } } public ReadOnlySpan(T[] array) { throw null;} - public ReadOnlySpan(T[] array, int start) { throw null;} public ReadOnlySpan(T[] array, int start, int length) { throw null;} public unsafe ReadOnlySpan(void* pointer, int length) { throw null;} public bool IsEmpty { get { throw null; } } @@ -43,7 +42,6 @@ public partial struct Span { public static Span Empty { get { throw null; } } public Span(T[] array) { throw null;} - public Span(T[] array, int start) { throw null;} public Span(T[] array, int start, int length) { throw null;} public unsafe Span(void* pointer, int length) { throw null;} public bool IsEmpty { get { throw null; } } diff --git a/src/System.Memory/src/System/ReadOnlySpan.cs b/src/System.Memory/src/System/ReadOnlySpan.cs index 7f8e0e745a4b..cfd4f56d71cc 100644 --- a/src/System.Memory/src/System/ReadOnlySpan.cs +++ b/src/System.Memory/src/System/ReadOnlySpan.cs @@ -35,33 +35,6 @@ public ReadOnlySpan(T[] array) _byteOffset = SpanHelpers.PerTypeValues.ArrayAdjustment; } - /// - /// Creates a new read-only span over the portion of the target array beginning - /// at 'start' index and covering the remainder of the array. - /// - /// The target array. - /// The index at which to begin the read-only span. - /// Thrown when is a null - /// reference (Nothing in Visual Basic). - /// Thrown when is covariant and array's type is not exactly T[]. - /// - /// Thrown when the specified is not in the range (<0 or >=Length). - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ReadOnlySpan(T[] array, int start) - { - if (array == null) - ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); - - int arrayLength = array.Length; - if ((uint)start > (uint)arrayLength) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); - - _length = arrayLength - start; - _pinnable = Unsafe.As>(array); - _byteOffset = SpanHelpers.PerTypeValues.ArrayAdjustment.Add(start); - } - /// /// Creates a new read-only span over the portion of the target array beginning /// at 'start' index and ending at 'end' index (exclusive). diff --git a/src/System.Memory/src/System/Span.cs b/src/System.Memory/src/System/Span.cs index 8df86446b3a1..02609491939f 100644 --- a/src/System.Memory/src/System/Span.cs +++ b/src/System.Memory/src/System/Span.cs @@ -39,35 +39,6 @@ public Span(T[] array) _byteOffset = SpanHelpers.PerTypeValues.ArrayAdjustment; } - /// - /// Creates a new span over the portion of the target array beginning - /// at 'start' index and covering the remainder of the array. - /// - /// The target array. - /// The index at which to begin the span. - /// Thrown when is a null - /// reference (Nothing in Visual Basic). - /// Thrown when is covariant and array's type is not exactly T[]. - /// - /// Thrown when the specified is not in the range (<0 or >=Length). - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Span(T[] array, int start) - { - if (array == null) - ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); - if (default(T) == null && array.GetType() != typeof(T[])) - ThrowHelper.ThrowArrayTypeMismatchException_ArrayTypeMustBeExactMatch(typeof(T)); - - int arrayLength = array.Length; - if ((uint)start > (uint)arrayLength) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); - - _length = arrayLength - start; - _pinnable = Unsafe.As>(array); - _byteOffset = SpanHelpers.PerTypeValues.ArrayAdjustment.Add(start); - } - /// /// Creates a new span over the portion of the target array beginning /// at 'start' index and ending at 'end' index (exclusive). diff --git a/src/System.Memory/tests/ReadOnlySpan/CtorArray.cs b/src/System.Memory/tests/ReadOnlySpan/CtorArray.cs index eedc15b9fa7b..825154c633b6 100644 --- a/src/System.Memory/tests/ReadOnlySpan/CtorArray.cs +++ b/src/System.Memory/tests/ReadOnlySpan/CtorArray.cs @@ -23,9 +23,6 @@ public static void CtorArray1() span = new ReadOnlySpan(a); span.Validate(91, 92, -93, 94); - span = new ReadOnlySpan(a, 0); - span.Validate(91, 92, -93, 94); - span = new ReadOnlySpan(a, 0, a.Length); span.Validate(91, 92, -93, 94); } @@ -39,9 +36,6 @@ public static void CtorArray2() span = new ReadOnlySpan(a); span.Validate(91, -92, 93, 94, -95); - span = new ReadOnlySpan(a, 0); - span.Validate(91, -92, 93, 94, -95); - span = new ReadOnlySpan(a, 0, a.Length); span.Validate(91, -92, 93, 94, -95); } @@ -57,9 +51,6 @@ public static void CtorArray3() span = new ReadOnlySpan(a); span.Validate(o1, o2); - span = new ReadOnlySpan(a, 0); - span.Validate(o1, o2); - span = new ReadOnlySpan(a, 0, a.Length); span.Validate(o1, o2); } @@ -73,9 +64,6 @@ public static void CtorArrayZeroLength() span = new ReadOnlySpan(empty); span.Validate(); - span = new ReadOnlySpan(empty, 0); - span.Validate(); - span = new ReadOnlySpan(empty, 0, empty.Length); span.Validate(); } @@ -84,7 +72,6 @@ public static void CtorArrayZeroLength() public static void CtorArrayNullArray() { Assert.Throws(() => new ReadOnlySpan((int[])null).DontBox()); - Assert.Throws(() => new ReadOnlySpan((int[])null, 0).DontBox()); Assert.Throws(() => new ReadOnlySpan((int[])null, 0, 0).DontBox()); } @@ -100,9 +87,6 @@ public static void CtorArrayWrongValueType() span = new ReadOnlySpan(aAsIntArray); span.Validate(42, -1); - span = new ReadOnlySpan(aAsIntArray, 0); - span.Validate(42, -1); - span = new ReadOnlySpan(aAsIntArray, 0, aAsIntArray.Length); span.Validate(42, -1); } @@ -118,8 +102,6 @@ public static void CtorVariantArrayType() string[] strArray = { "Hello" }; span = new ReadOnlySpan(strArray); span.Validate("Hello"); - span = new ReadOnlySpan(strArray, 0); - span.Validate("Hello"); span = new ReadOnlySpan(strArray, 0, strArray.Length); span.Validate("Hello"); @@ -128,8 +110,6 @@ public static void CtorVariantArrayType() TestClass[] clsArray = { c1, c2 }; span = new ReadOnlySpan(clsArray); span.Validate(c1, c2); - span = new ReadOnlySpan(clsArray, 0); - span.Validate(c1, c2); span = new ReadOnlySpan(clsArray, 0, clsArray.Length); span.Validate(c1, c2); } diff --git a/src/System.Memory/tests/ReadOnlySpan/CtorArrayInt.cs b/src/System.Memory/tests/ReadOnlySpan/CtorArrayInt.cs deleted file mode 100644 index c63094c58dc5..000000000000 --- a/src/System.Memory/tests/ReadOnlySpan/CtorArrayInt.cs +++ /dev/null @@ -1,54 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.SpanTests -{ - // - // Tests for Span.ctor(T[], int). If the test is not specific to this overload, consider putting it in CtorArray.cs instread. - // - public static partial class ReadOnlySpanTests - { - [Fact] - public static void CtorArrayInt1() - { - int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98 }; - ReadOnlySpan span = new ReadOnlySpan(a, 3); - span.Validate(93, 94, 95, 96, 97, 98); - } - - [Fact] - public static void CtorArrayInt2() - { - long[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98 }; - ReadOnlySpan span = new ReadOnlySpan(a, 3); - span.Validate(93, 94, 95, 96, 97, 98); - } - - [Fact] - public static void CtorArrayIntNegativeStart() - { - int[] a = new int[3]; - Assert.Throws(() => new ReadOnlySpan(a, -1).DontBox()); - } - - [Fact] - public static void CtorArrayIntStartTooLarge() - { - int[] a = new int[3]; - Assert.Throws(() => new ReadOnlySpan(a, 4).DontBox()); - } - - [Fact] - public static void CtorArrayIntStartEqualsLength() - { - // Valid for start to equal the array length. This returns an empty span that starts "just past the array." - int[] a = { 91, 92, 93 }; - ReadOnlySpan span = new ReadOnlySpan(a, 3); - span.Validate(); - } - } -} - diff --git a/src/System.Memory/tests/Span/CtorArray.cs b/src/System.Memory/tests/Span/CtorArray.cs index 625dc62b81ac..1fb981674cbb 100644 --- a/src/System.Memory/tests/Span/CtorArray.cs +++ b/src/System.Memory/tests/Span/CtorArray.cs @@ -23,9 +23,6 @@ public static void CtorArray1() span = new Span(a); span.Validate(91, 92, -93, 94); - span = new Span(a, 0); - span.Validate(91, 92, -93, 94); - span = new Span(a, 0, a.Length); span.Validate(91, 92, -93, 94); } @@ -39,9 +36,6 @@ public static void CtorArray2() span = new Span(a); span.Validate(91, -92, 93, 94, -95); - span = new Span(a, 0); - span.Validate(91, -92, 93, 94, -95); - span = new Span(a, 0, a.Length); span.Validate(91, -92, 93, 94, -95); } @@ -57,9 +51,6 @@ public static void CtorArray3() span = new Span(a); span.Validate(o1, o2); - span = new Span(a, 0); - span.Validate(o1, o2); - span = new Span(a, 0, a.Length); span.Validate(o1, o2); } @@ -73,9 +64,6 @@ public static void CtorArrayZeroLength() span = new Span(empty); span.Validate(); - span = new Span(empty, 0); - span.Validate(); - span = new Span(empty, 0, empty.Length); span.Validate(); } @@ -84,7 +72,6 @@ public static void CtorArrayZeroLength() public static void CtorArrayNullArray() { Assert.Throws(() => new Span((int[])null).DontBox()); - Assert.Throws(() => new Span((int[])null, 0).DontBox()); Assert.Throws(() => new Span((int[])null, 0, 0).DontBox()); } @@ -94,7 +81,6 @@ public static void CtorArrayWrongArrayType() // Cannot pass variant array, if array type is not a valuetype. string[] a = { "Hello" }; Assert.Throws(() => new Span(a).DontBox()); - Assert.Throws(() => new Span(a, 0).DontBox()); Assert.Throws(() => new Span(a, 0, a.Length).DontBox()); } @@ -110,9 +96,6 @@ public static void CtorArrayWrongValueType() span = new Span(aAsIntArray); span.Validate(42, -1); - span = new Span(aAsIntArray, 0); - span.Validate(42, -1); - span = new Span(aAsIntArray, 0, aAsIntArray.Length); span.Validate(42, -1); } diff --git a/src/System.Memory/tests/Span/CtorArrayInt.cs b/src/System.Memory/tests/Span/CtorArrayInt.cs deleted file mode 100644 index e838d6268b75..000000000000 --- a/src/System.Memory/tests/Span/CtorArrayInt.cs +++ /dev/null @@ -1,54 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.SpanTests -{ - // - // Tests for Span.ctor(T[], int). If the test is not specific to this overload, consider putting it in CtorArray.cs instread. - // - public static partial class SpanTests - { - [Fact] - public static void CtorArrayInt1() - { - int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98 }; - Span span = new Span(a, 3); - span.Validate(93, 94, 95, 96, 97, 98); - } - - [Fact] - public static void CtorArrayInt2() - { - long[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98 }; - Span span = new Span(a, 3); - span.Validate(93, 94, 95, 96, 97, 98); - } - - [Fact] - public static void CtorArrayIntNegativeStart() - { - int[] a = new int[3]; - Assert.Throws(() => new Span(a, -1).DontBox()); - } - - [Fact] - public static void CtorArrayIntStartTooLarge() - { - int[] a = new int[3]; - Assert.Throws(() => new Span(a, 4).DontBox()); - } - - [Fact] - public static void CtorArrayIntStartEqualsLength() - { - // Valid for start to equal the array length. This returns an empty span that starts "just past the array." - int[] a = { 91, 92, 93 }; - Span span = new Span(a, 3); - span.Validate(); - } - } -} - diff --git a/src/System.Memory/tests/System.Memory.Tests.csproj b/src/System.Memory/tests/System.Memory.Tests.csproj index 156b5a9edbc1..eaca378048a3 100644 --- a/src/System.Memory/tests/System.Memory.Tests.csproj +++ b/src/System.Memory/tests/System.Memory.Tests.csproj @@ -20,7 +20,6 @@ - @@ -52,7 +51,6 @@ - From cc9b96077b317b63a965bd7da33166f1b0e2558d Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Thu, 24 Aug 2017 16:55:50 -0700 Subject: [PATCH 2/2] Remove uses of the Span/ROS constructor that takes an array and offset --- src/Common/src/System/Security/Cryptography/DerEncoder.cs | 2 +- .../src/System/Xml/Core/XmlRawTextWriterGenerator.cxx | 2 +- .../src/System/Xml/Core/XmlUtf8RawTextWriter.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Common/src/System/Security/Cryptography/DerEncoder.cs b/src/Common/src/System/Security/Cryptography/DerEncoder.cs index bf70a42bf8f2..bf9dca7edd06 100644 --- a/src/Common/src/System/Security/Cryptography/DerEncoder.cs +++ b/src/Common/src/System/Security/Cryptography/DerEncoder.cs @@ -160,7 +160,7 @@ internal static byte[][] SegmentedEncodeUnsignedInteger(ReadOnlySpan bigEn int length = end - start; int writeStart = bigEndianBytes[start] > 0x7F ? 1 : 0; var dataBytes = new byte[length + writeStart]; - bigEndianBytes.Slice(start, length).CopyTo(new Span(dataBytes, writeStart)); + bigEndianBytes.Slice(start, length).CopyTo(new Span(dataBytes).Slice(writeStart)); return new[] { diff --git a/src/System.Private.Xml/src/System/Xml/Core/XmlRawTextWriterGenerator.cxx b/src/System.Private.Xml/src/System/Xml/Core/XmlRawTextWriterGenerator.cxx index b06d4032513a..e4b180b9804e 100644 --- a/src/System.Private.Xml/src/System/Xml/Core/XmlRawTextWriterGenerator.cxx +++ b/src/System.Private.Xml/src/System/Xml/Core/XmlRawTextWriterGenerator.cxx @@ -212,7 +212,7 @@ namespace System.Xml { if ( !stream.CanSeek || stream.Position == 0 ) { ReadOnlySpan bom = encoding.Preamble; if ( bom.Length != 0 ) { - bom.CopyTo(new Span(bufBytes, 1)); + bom.CopyTo(new Span(bufBytes).Slice(1)); bufPos += bom.Length; textPos += bom.Length; } diff --git a/src/System.Private.Xml/src/System/Xml/Core/XmlUtf8RawTextWriter.cs b/src/System.Private.Xml/src/System/Xml/Core/XmlUtf8RawTextWriter.cs index f10d2efc5695..cdf5290c9196 100644 --- a/src/System.Private.Xml/src/System/Xml/Core/XmlUtf8RawTextWriter.cs +++ b/src/System.Private.Xml/src/System/Xml/Core/XmlUtf8RawTextWriter.cs @@ -128,7 +128,7 @@ public XmlUtf8RawTextWriter(Stream stream, XmlWriterSettings settings) : this(se ReadOnlySpan bom = encoding.Preamble; if (bom.Length != 0) { - bom.CopyTo(new Span(bufBytes, 1)); + bom.CopyTo(new Span(bufBytes).Slice(1)); bufPos += bom.Length; textPos += bom.Length; }