diff --git a/src/libraries/System.Private.CoreLib/src/System/SearchValues/SearchValues.cs b/src/libraries/System.Private.CoreLib/src/System/SearchValues/SearchValues.cs index f1282892d3be25..3d7bb970e36017 100644 --- a/src/libraries/System.Private.CoreLib/src/System/SearchValues/SearchValues.cs +++ b/src/libraries/System.Private.CoreLib/src/System/SearchValues/SearchValues.cs @@ -242,21 +242,12 @@ public static SearchValues Create(ReadOnlySpan values, StringCom } private static bool TryGetSingleRange(ReadOnlySpan values, out T minInclusive, out T maxInclusive) - where T : struct, INumber, IMinMaxValue + where T : struct, INumber { - T min = T.MaxValue; - T max = T.MinValue; + minInclusive = values.Min(); + maxInclusive = values.Max(); - foreach (T value in values) - { - min = T.Min(min, value); - max = T.Max(max, value); - } - - minInclusive = min; - maxInclusive = max; - - uint range = uint.CreateChecked(max - min) + 1; + uint range = uint.CreateChecked(maxInclusive - minInclusive) + 1; if (range > values.Length) { return false; @@ -268,7 +259,7 @@ private static bool TryGetSingleRange(ReadOnlySpan values, out T minInclus foreach (T value in values) { - int offset = int.CreateChecked(value - min); + int offset = int.CreateChecked(value - minInclusive); seenValues[offset] = true; } diff --git a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Symbolic/BDD.cs b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Symbolic/BDD.cs index 629288b2b51544..42d6af696bd9d5 100644 --- a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Symbolic/BDD.cs +++ b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Symbolic/BDD.cs @@ -307,14 +307,11 @@ public byte[] SerializeToBytes() // in other cases make use of the general serializer to long[] long[] serialized = Serialize(); + // make sure this serialization is not applied to MTBDDs + Debug.Assert(serialized.AsSpan().Min() > 0); + // get the maximal element from the array - long m = 0; - for (int i = 0; i < serialized.Length; i++) - { - // make sure this serialization is not applied to MTBDDs - Debug.Assert(serialized[i] > 0); - m = Math.Max(serialized[i], m); - } + long m = serialized.AsSpan().Max(); // k is the number of bytes needed to represent the maximal element int k = m <= 0xFFFF ? 2 : (m <= 0xFF_FFFF ? 3 : (m <= 0xFFFF_FFFF ? 4 : (m <= 0xFF_FFFF_FFFF ? 5 : (m <= 0xFFFF_FFFF_FFFF ? 6 : (m <= 0xFF_FFFF_FFFF_FFFF ? 7 : 8)))));