From 5d013e970cafe8b6b4093c1d97fd17d75c4e99ff Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 28 Sep 2020 20:58:42 +0200 Subject: [PATCH 01/14] Updated Mvvm package --- Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj b/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj index 24b6ccc43b5..3c87da5efe5 100644 --- a/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj +++ b/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj @@ -23,13 +23,9 @@ - - - - - + From b266f9f03e1078d09aec56e9205d2a7b21a75dcc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 28 Sep 2020 20:58:51 +0200 Subject: [PATCH 02/14] Updated HighPerformance package --- .../Microsoft.Toolkit.HighPerformance.csproj | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj b/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj index 06c9e584da5..4a8c8f1a29c 100644 --- a/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj +++ b/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj @@ -41,7 +41,7 @@ - + @@ -51,14 +51,14 @@ - + - + @@ -76,6 +76,9 @@ + + + - + diff --git a/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj b/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj index 39663f0f30c..ba5af1eb461 100644 --- a/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj +++ b/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj @@ -160,7 +160,7 @@ 1.4.0 - 4.7.1 + 5.0.0-rc.1.20451.14 diff --git a/UnitTests/UnitTests.NetCore/UnitTests.NetCore.csproj b/UnitTests/UnitTests.NetCore/UnitTests.NetCore.csproj index d582791375b..272c3a1b886 100644 --- a/UnitTests/UnitTests.NetCore/UnitTests.NetCore.csproj +++ b/UnitTests/UnitTests.NetCore/UnitTests.NetCore.csproj @@ -16,7 +16,7 @@ - + From 78d560d1ff9b658080a894cc24a37655fe8b8d72 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 6 Oct 2020 19:33:07 +0200 Subject: [PATCH 04/14] Refactored StringPool with new null ref APIs --- .../Buffers/StringPool.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Microsoft.Toolkit.HighPerformance/Buffers/StringPool.cs b/Microsoft.Toolkit.HighPerformance/Buffers/StringPool.cs index b2f75bbf0d3..c05a0f7fdfa 100644 --- a/Microsoft.Toolkit.HighPerformance/Buffers/StringPool.cs +++ b/Microsoft.Toolkit.HighPerformance/Buffers/StringPool.cs @@ -422,11 +422,11 @@ public object SyncRoot /// The input instance to cache. /// The precomputed hashcode for . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public unsafe void Add(string value, int hashcode) + public void Add(string value, int hashcode) { ref string target = ref TryGet(value.AsSpan(), hashcode); - if (Unsafe.AreSame(ref target, ref Unsafe.AsRef(null))) + if (Unsafe.IsNullRef(ref target)) { Insert(value, hashcode); } @@ -443,11 +443,11 @@ public unsafe void Add(string value, int hashcode) /// The precomputed hashcode for . /// A instance with the contents of . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public unsafe string GetOrAdd(string value, int hashcode) + public string GetOrAdd(string value, int hashcode) { ref string result = ref TryGet(value.AsSpan(), hashcode); - if (!Unsafe.AreSame(ref result, ref Unsafe.AsRef(null))) + if (!Unsafe.IsNullRef(ref result)) { return result; } @@ -464,11 +464,11 @@ public unsafe string GetOrAdd(string value, int hashcode) /// The precomputed hashcode for . /// A instance with the contents of , cached if possible. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public unsafe string GetOrAdd(ReadOnlySpan span, int hashcode) + public string GetOrAdd(ReadOnlySpan span, int hashcode) { ref string result = ref TryGet(span, hashcode); - if (!Unsafe.AreSame(ref result, ref Unsafe.AsRef(null))) + if (!Unsafe.IsNullRef(ref result)) { return result; } @@ -488,11 +488,11 @@ public unsafe string GetOrAdd(ReadOnlySpan span, int hashcode) /// The resulting cached instance, if present /// Whether or not the target instance was found. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public unsafe bool TryGet(ReadOnlySpan span, int hashcode, [NotNullWhen(true)] out string? value) + public bool TryGet(ReadOnlySpan span, int hashcode, [NotNullWhen(true)] out string? value) { ref string result = ref TryGet(span, hashcode); - if (!Unsafe.AreSame(ref result, ref Unsafe.AsRef(null))) + if (!Unsafe.IsNullRef(ref result)) { value = result; @@ -527,7 +527,7 @@ public void Reset() private unsafe ref string TryGet(ReadOnlySpan span, int hashcode) { ref MapEntry mapEntriesRef = ref this.mapEntries.DangerousGetReference(); - ref MapEntry entry = ref Unsafe.AsRef(null); + ref MapEntry entry = ref Unsafe.NullRef(); int length = this.buckets.Length, bucketIndex = hashcode & (length - 1); @@ -547,7 +547,7 @@ private unsafe ref string TryGet(ReadOnlySpan span, int hashcode) } } - return ref Unsafe.AsRef(null); + return ref Unsafe.NullRef(); } /// From 8adfec79732dc0fd17435acc64a3dc37cbc8f27a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 8 Oct 2020 12:02:53 +0200 Subject: [PATCH 05/14] Suppressed Unsafe.As nullability warnings Working around lack of annotations below .NET Standard 2.1, see https://github.com/dotnet/runtime/pull/42827 for more info --- Microsoft.Toolkit.HighPerformance/Box{T}.cs | 8 ++++---- .../Extensions/ArrayExtensions.1D.cs | 4 ++-- .../Extensions/ArrayExtensions.2D.cs | 2 +- .../Extensions/ObjectExtensions.cs | 4 ++-- .../Extensions/StringExtensions.cs | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Microsoft.Toolkit.HighPerformance/Box{T}.cs b/Microsoft.Toolkit.HighPerformance/Box{T}.cs index f103f663663..cb6fad72813 100644 --- a/Microsoft.Toolkit.HighPerformance/Box{T}.cs +++ b/Microsoft.Toolkit.HighPerformance/Box{T}.cs @@ -78,7 +78,7 @@ public static Box GetFrom(object obj) ThrowInvalidCastExceptionForGetFrom(); } - return Unsafe.As>(obj); + return Unsafe.As>(obj)!; } /// @@ -94,7 +94,7 @@ public static Box GetFrom(object obj) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Box DangerousGetFrom(object obj) { - return Unsafe.As>(obj); + return Unsafe.As>(obj)!; } /// @@ -108,7 +108,7 @@ public static bool TryGetFrom(object obj, [NotNullWhen(true)] out Box? box) { if (obj.GetType() == typeof(T)) { - box = Unsafe.As>(obj); + box = Unsafe.As>(obj)!; return true; } @@ -145,7 +145,7 @@ public static implicit operator Box(T value) // manually be implemented in the Box type. For instance, boxing a float // and calling ToString() on it directly, on its boxed object or on a Box // reference retrieved from it will produce the same result in all cases. - return Unsafe.As>(value); + return Unsafe.As>(value)!; } /// diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.1D.cs b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.1D.cs index 887cdd0f989..36ea2ba06b2 100644 --- a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.1D.cs +++ b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.1D.cs @@ -31,7 +31,7 @@ public static partial class ArrayExtensions public static ref T DangerousGetReference(this T[] array) { #if NETCORE_RUNTIME - var arrayData = Unsafe.As(array); + var arrayData = Unsafe.As(array)!; ref T r0 = ref Unsafe.As(ref arrayData.Data); return ref r0; @@ -55,7 +55,7 @@ public static ref T DangerousGetReference(this T[] array) public static ref T DangerousGetReferenceAt(this T[] array, int i) { #if NETCORE_RUNTIME - var arrayData = Unsafe.As(array); + var arrayData = Unsafe.As(array)!; ref T r0 = ref Unsafe.As(ref arrayData.Data); ref T ri = ref Unsafe.Add(ref r0, (nint)(uint)i); diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs index 8807c558b33..5993cc3a1be 100644 --- a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs +++ b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs @@ -33,7 +33,7 @@ public static partial class ArrayExtensions public static ref T DangerousGetReference(this T[,] array) { #if NETCORE_RUNTIME - var arrayData = Unsafe.As(array); + var arrayData = Unsafe.As(array)!; ref T r0 = ref Unsafe.As(ref arrayData.Data); return ref r0; diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/ObjectExtensions.cs b/Microsoft.Toolkit.HighPerformance/Extensions/ObjectExtensions.cs index c94675ecdee..0a61350a17e 100644 --- a/Microsoft.Toolkit.HighPerformance/Extensions/ObjectExtensions.cs +++ b/Microsoft.Toolkit.HighPerformance/Extensions/ObjectExtensions.cs @@ -31,7 +31,7 @@ public static class ObjectExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IntPtr DangerousGetObjectDataByteOffset(this object obj, ref T data) { - var rawObj = Unsafe.As(obj); + var rawObj = Unsafe.As(obj)!; ref byte r0 = ref rawObj.Data; ref byte r1 = ref Unsafe.As(ref data); @@ -55,7 +55,7 @@ public static IntPtr DangerousGetObjectDataByteOffset(this object obj, ref T [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ref T DangerousGetObjectDataReferenceAt(this object obj, IntPtr offset) { - var rawObj = Unsafe.As(obj); + var rawObj = Unsafe.As(obj)!; ref byte r0 = ref rawObj.Data; ref byte r1 = ref Unsafe.AddByteOffset(ref r0, offset); ref T r2 = ref Unsafe.As(ref r1); diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/StringExtensions.cs b/Microsoft.Toolkit.HighPerformance/Extensions/StringExtensions.cs index 223480d5b06..02027dfb471 100644 --- a/Microsoft.Toolkit.HighPerformance/Extensions/StringExtensions.cs +++ b/Microsoft.Toolkit.HighPerformance/Extensions/StringExtensions.cs @@ -31,7 +31,7 @@ public static ref char DangerousGetReference(this string text) #if NETCOREAPP3_1 return ref Unsafe.AsRef(text.GetPinnableReference()); #elif NETCOREAPP2_1 - var stringData = Unsafe.As(text); + var stringData = Unsafe.As(text)!; return ref stringData.Data; #else @@ -53,7 +53,7 @@ public static ref char DangerousGetReferenceAt(this string text, int i) #if NETCOREAPP3_1 ref char r0 = ref Unsafe.AsRef(text.GetPinnableReference()); #elif NETCOREAPP2_1 - ref char r0 = ref Unsafe.As(text).Data; + ref char r0 = ref Unsafe.As(text)!.Data; #else ref char r0 = ref MemoryMarshal.GetReference(text.AsSpan()); #endif From fe631534182c4fc2cd5168e2ba88800b87ed4666 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 9 Oct 2020 13:07:45 +0200 Subject: [PATCH 06/14] Reverted package update in MVVM Toolkit Not needed yet as there are no new relevant APIs here --- Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj b/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj index 3c87da5efe5..28b2f4cdfa4 100644 --- a/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj +++ b/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj @@ -25,7 +25,7 @@ - + From 0f1d309033f9c8029a662a4b33cfdfb8bd3f6581 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 9 Oct 2020 13:15:44 +0200 Subject: [PATCH 07/14] Tweaked MVVM Toolkit .csproj --- Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj b/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj index 28b2f4cdfa4..35d647ff831 100644 --- a/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj +++ b/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj @@ -22,10 +22,14 @@ + + + + + - From f24f6a29e78d18fc5382a0d4534aff093095d461 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 9 Oct 2020 13:21:09 +0200 Subject: [PATCH 08/14] Removed trailing whitespace in .csproj --- Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj b/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj index 35d647ff831..24b6ccc43b5 100644 --- a/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj +++ b/Microsoft.Toolkit.Mvvm/Microsoft.Toolkit.Mvvm.csproj @@ -22,7 +22,7 @@ - + From 6f70348b04fd473adb830fcb08e7b01921a183dd Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 26 Oct 2020 17:38:35 +0100 Subject: [PATCH 09/14] Bumped S.R.CS.Unsafe package to .NET 5 RC2 --- .../Microsoft.Toolkit.HighPerformance.csproj | 10 +++++----- .../UnitTests.HighPerformance.NetCore.csproj | 2 +- .../UnitTests.HighPerformance.UWP.csproj | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj b/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj index 4a8c8f1a29c..1a9680ade90 100644 --- a/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj +++ b/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj @@ -41,7 +41,7 @@ - + @@ -51,14 +51,14 @@ - + - + @@ -77,7 +77,7 @@ - + @@ -92,7 +92,7 @@ - + SPAN_RUNTIME_SUPPORT;NETCORE_RUNTIME diff --git a/UnitTests/UnitTests.HighPerformance.NetCore/UnitTests.HighPerformance.NetCore.csproj b/UnitTests/UnitTests.HighPerformance.NetCore/UnitTests.HighPerformance.NetCore.csproj index 0b1bcf64476..30a2dd12cc1 100644 --- a/UnitTests/UnitTests.HighPerformance.NetCore/UnitTests.HighPerformance.NetCore.csproj +++ b/UnitTests/UnitTests.HighPerformance.NetCore/UnitTests.HighPerformance.NetCore.csproj @@ -9,7 +9,7 @@ - + diff --git a/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj b/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj index ba5af1eb461..ee5e26c8d94 100644 --- a/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj +++ b/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj @@ -160,7 +160,7 @@ 1.4.0 - 5.0.0-rc.1.20451.14 + 5.0.0-rc.2.20475.5 From da5264ddf8009c9a761043a8434d5b29a447dfde Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Nov 2020 14:33:41 +0100 Subject: [PATCH 10/14] Minor code tweaks --- UnitTests/UnitTests.NetCore/UnitTests.NetCore.csproj | 2 +- UnitTests/UnitTests.Shared/Diagnostics/Test_ThrowHelper.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/UnitTests/UnitTests.NetCore/UnitTests.NetCore.csproj b/UnitTests/UnitTests.NetCore/UnitTests.NetCore.csproj index 272c3a1b886..d582791375b 100644 --- a/UnitTests/UnitTests.NetCore/UnitTests.NetCore.csproj +++ b/UnitTests/UnitTests.NetCore/UnitTests.NetCore.csproj @@ -16,7 +16,7 @@ - + diff --git a/UnitTests/UnitTests.Shared/Diagnostics/Test_ThrowHelper.cs b/UnitTests/UnitTests.Shared/Diagnostics/Test_ThrowHelper.cs index 992ee7bca0f..2e91cc69930 100644 --- a/UnitTests/UnitTests.Shared/Diagnostics/Test_ThrowHelper.cs +++ b/UnitTests/UnitTests.Shared/Diagnostics/Test_ThrowHelper.cs @@ -3,10 +3,10 @@ // See the LICENSE file in the project root for more information. using System; -using System.Linq; using System.Collections.Generic; using System.ComponentModel; using System.IO; +using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Threading; From 8fe8c46f33619084dbfbb956ffcf9ac9be5d6cc2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 6 Nov 2020 21:57:10 +0100 Subject: [PATCH 11/14] Fixed some nullability warnings --- .../Extensions/ArrayExtensions.2D.cs | 2 +- .../Extensions/ArrayExtensions.3D.cs | 4 ++-- Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs | 4 ++-- .../Memory/ReadOnlyMemory2D{T}.cs | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs index 5993cc3a1be..9af8101f06a 100644 --- a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs +++ b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs @@ -63,7 +63,7 @@ public static ref T DangerousGetReference(this T[,] array) public static ref T DangerousGetReferenceAt(this T[,] array, int i, int j) { #if NETCORE_RUNTIME - var arrayData = Unsafe.As(array); + var arrayData = Unsafe.As(array)!; nint offset = ((nint)(uint)i * (nint)(uint)arrayData.Width) + (nint)(uint)j; ref T r0 = ref Unsafe.As(ref arrayData.Data); ref T ri = ref Unsafe.Add(ref r0, offset); diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs index 846e3c5eadb..3a08882a419 100644 --- a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs +++ b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs @@ -32,7 +32,7 @@ public static partial class ArrayExtensions public static ref T DangerousGetReference(this T[,,] array) { #if NETCORE_RUNTIME - var arrayData = Unsafe.As(array); + var arrayData = Unsafe.As(array)!; ref T r0 = ref Unsafe.As(ref arrayData.Data); return ref r0; @@ -63,7 +63,7 @@ public static ref T DangerousGetReference(this T[,,] array) public static ref T DangerousGetReferenceAt(this T[,,] array, int i, int j, int k) { #if NETCORE_RUNTIME - var arrayData = Unsafe.As(array); + var arrayData = Unsafe.As(array)!; nint offset = ((nint)(uint)i * (nint)(uint)arrayData.Height * (nint)(uint)arrayData.Width) + ((nint)(uint)j * (nint)(uint)arrayData.Width) + (nint)(uint)k; diff --git a/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs b/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs index 6b3cb0cc82e..afaaede8873 100644 --- a/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs +++ b/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs @@ -772,7 +772,7 @@ public bool TryGetMemory(out Memory memory) } else if (typeof(T) == typeof(char) && this.instance.GetType() == typeof(string)) { - string text = Unsafe.As(this.instance); + string text = Unsafe.As(this.instance)!; int index = text.AsSpan().IndexOf(in text.DangerousGetObjectDataReferenceAt(this.offset)); ReadOnlyMemory temp = text.AsMemory(index, (int)Length); @@ -795,7 +795,7 @@ public bool TryGetMemory(out Memory memory) else if (this.instance.GetType() == typeof(T[])) { // If it's a T[] array, also handle the initial offset - T[] array = Unsafe.As(this.instance); + T[] array = Unsafe.As(this.instance)!; int index = array.AsSpan().IndexOf(ref array.DangerousGetObjectDataReferenceAt(this.offset)); memory = array.AsMemory(index, this.height * this.width); diff --git a/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs b/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs index 9b561233c1c..2fb621d68be 100644 --- a/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs +++ b/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs @@ -794,7 +794,7 @@ public bool TryGetMemory(out ReadOnlyMemory memory) // difference between the start of the Span (which directly wraps just the actual character data // within the string), and the input reference, which we can get from the byte offset in use. The result // is the character index which we can use to create the final Memory instance. - string text = Unsafe.As(this.instance); + string text = Unsafe.As(this.instance)!; int index = text.AsSpan().IndexOf(in text.DangerousGetObjectDataReferenceAt(this.offset)); ReadOnlyMemory temp = text.AsMemory(index, (int)Length); @@ -811,7 +811,7 @@ public bool TryGetMemory(out ReadOnlyMemory memory) else if (this.instance.GetType() == typeof(T[])) { // If it's a T[] array, also handle the initial offset - T[] array = Unsafe.As(this.instance); + T[] array = Unsafe.As(this.instance)!; int index = array.AsSpan().IndexOf(ref array.DangerousGetObjectDataReferenceAt(this.offset)); memory = array.AsMemory(index, this.height * this.width); From 53b84a002141a0e500e1f92df9bf154743c95f64 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 6 Nov 2020 21:59:01 +0100 Subject: [PATCH 12/14] Minor code tweaks --- Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs | 7 ++----- .../Memory/ReadOnlyMemory2D{T}.cs | 7 ++----- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs b/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs index afaaede8873..87055a7714a 100644 --- a/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs +++ b/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs @@ -786,11 +786,8 @@ public bool TryGetMemory(out Memory memory) } else if (this.instance is MemoryManager memoryManager) { - unsafe - { - // If the object is a MemoryManager, just slice it as needed - memory = memoryManager.Memory.Slice((int)(void*)this.offset, this.height * this.width); - } + // If the object is a MemoryManager, just slice it as needed + memory = memoryManager.Memory.Slice((int)(nint)this.offset, this.height * this.width); } else if (this.instance.GetType() == typeof(T[])) { diff --git a/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs b/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs index 2fb621d68be..9bd22a40983 100644 --- a/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs +++ b/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs @@ -802,11 +802,8 @@ public bool TryGetMemory(out ReadOnlyMemory memory) } else if (this.instance is MemoryManager memoryManager) { - unsafe - { - // If the object is a MemoryManager, just slice it as needed - memory = memoryManager.Memory.Slice((int)(void*)this.offset, this.height * this.width); - } + // If the object is a MemoryManager, just slice it as needed + memory = memoryManager.Memory.Slice((int)(nint)this.offset, this.height * this.width); } else if (this.instance.GetType() == typeof(T[])) { From 7807070d169efcdc458d4721fe5e49c1685a128c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 10 Nov 2020 02:06:48 +0100 Subject: [PATCH 13/14] =?UTF-8?q?Bumped=20S.R.CS.Unsafe=20package=20to=205?= =?UTF-8?q?.0.0=20=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Microsoft.Toolkit.HighPerformance.csproj | 10 +++++----- .../UnitTests.HighPerformance.NetCore.csproj | 2 +- .../UnitTests.HighPerformance.UWP.csproj | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj b/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj index 1a9680ade90..e3fe544aaef 100644 --- a/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj +++ b/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj @@ -41,7 +41,7 @@ - + @@ -51,14 +51,14 @@ - + - + @@ -77,7 +77,7 @@ - + @@ -92,7 +92,7 @@ - + SPAN_RUNTIME_SUPPORT;NETCORE_RUNTIME diff --git a/UnitTests/UnitTests.HighPerformance.NetCore/UnitTests.HighPerformance.NetCore.csproj b/UnitTests/UnitTests.HighPerformance.NetCore/UnitTests.HighPerformance.NetCore.csproj index 30a2dd12cc1..259513b2899 100644 --- a/UnitTests/UnitTests.HighPerformance.NetCore/UnitTests.HighPerformance.NetCore.csproj +++ b/UnitTests/UnitTests.HighPerformance.NetCore/UnitTests.HighPerformance.NetCore.csproj @@ -9,7 +9,7 @@ - + diff --git a/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj b/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj index ee5e26c8d94..9800aa4b521 100644 --- a/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj +++ b/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj @@ -160,7 +160,7 @@ 1.4.0 - 5.0.0-rc.2.20475.5 + 5.0.0 From 63ad9e741b5908c962611403e217e341e82fe51c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 10 Nov 2020 12:51:22 +0100 Subject: [PATCH 14/14] Fixed FormatException when ColorHelper in non-US --- Microsoft.Toolkit.Uwp/Helpers/ColorHelper.cs | 28 +++++++++----------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/Microsoft.Toolkit.Uwp/Helpers/ColorHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/ColorHelper.cs index 3a98aaee165..f3f82615034 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/ColorHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/ColorHelper.cs @@ -3,7 +3,9 @@ // See the LICENSE file in the project root for more information. using System; +using System.Globalization; using System.Reflection; +using Microsoft.Toolkit.Diagnostics; using Windows.UI; using Color = Windows.UI.Color; @@ -22,10 +24,7 @@ public static class ColorHelper /// The created . public static Color ToColor(this string colorString) { - if (string.IsNullOrEmpty(colorString)) - { - throw new ArgumentException(nameof(colorString)); - } + Guard.IsNotNullOrEmpty(colorString, nameof(colorString)); if (colorString[0] == '#') { @@ -80,8 +79,7 @@ public static Color ToColor(this string colorString) return Color.FromArgb(255, r, g, b); } - default: - throw new FormatException(string.Format("The {0} string passed in the colorString argument is not a recognized Color format.", colorString)); + default: return ThrowHelper.ThrowFormatException("The string passed in the colorString argument is not a recognized Color format."); } } @@ -91,24 +89,24 @@ public static Color ToColor(this string colorString) if (values.Length == 4) { - var scA = double.Parse(values[0].Substring(3)); - var scR = double.Parse(values[1]); - var scG = double.Parse(values[2]); - var scB = double.Parse(values[3]); + var scA = double.Parse(values[0].Substring(3), CultureInfo.InvariantCulture); + var scR = double.Parse(values[1], CultureInfo.InvariantCulture); + var scG = double.Parse(values[2], CultureInfo.InvariantCulture); + var scB = double.Parse(values[3], CultureInfo.InvariantCulture); return Color.FromArgb((byte)(scA * 255), (byte)(scR * 255), (byte)(scG * 255), (byte)(scB * 255)); } if (values.Length == 3) { - var scR = double.Parse(values[0].Substring(3)); - var scG = double.Parse(values[1]); - var scB = double.Parse(values[2]); + var scR = double.Parse(values[0].Substring(3), CultureInfo.InvariantCulture); + var scG = double.Parse(values[1], CultureInfo.InvariantCulture); + var scB = double.Parse(values[2], CultureInfo.InvariantCulture); return Color.FromArgb(255, (byte)(scR * 255), (byte)(scG * 255), (byte)(scB * 255)); } - throw new FormatException(string.Format("The {0} string passed in the colorString argument is not a recognized Color format (sc#[scA,]scR,scG,scB).", colorString)); + return ThrowHelper.ThrowFormatException("The string passed in the colorString argument is not a recognized Color format (sc#[scA,]scR,scG,scB)."); } var prop = typeof(Colors).GetTypeInfo().GetDeclaredProperty(colorString); @@ -118,7 +116,7 @@ public static Color ToColor(this string colorString) return (Color)prop.GetValue(null); } - throw new FormatException(string.Format("The {0} string passed in the colorString argument is not a recognized Color.", colorString)); + return ThrowHelper.ThrowFormatException("The string passed in the colorString argument is not a recognized Color."); } ///