44 * the license and the contributors participating to this project.
55 */
66
7- using System . Diagnostics ;
8- using System . Runtime . CompilerServices ;
97using System . Runtime . InteropServices ;
108using System . Runtime . Versioning ;
119using System . Security . Cryptography . X509Certificates ;
@@ -17,58 +15,6 @@ namespace OpenIddict.Extensions;
1715/// </summary>
1816internal static class OpenIddictPolyfills
1917{
20- extension ( ArgumentOutOfRangeException )
21- {
22- /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is negative.</summary>
23- /// <param name="value">The argument to validate as non-negative.</param>
24- /// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
25- public static void ThrowIfNegative < T > ( T value , [ CallerArgumentExpression ( nameof ( value ) ) ] string ? paramName = null )
26- where T : struct , IComparable < T >
27- {
28- switch ( value )
29- {
30- case byte or ushort or uint or ulong or char :
31- return ;
32- case sbyte n :
33- if ( n < 0 )
34- ThrowArgumentOutOfRangeException ( paramName , value ) ;
35- return ;
36- case short n :
37- if ( n < 0 )
38- ThrowArgumentOutOfRangeException ( paramName , value ) ;
39- return ;
40- case int n :
41- if ( n < 0 )
42- ThrowArgumentOutOfRangeException ( paramName , value ) ;
43- return ;
44- case long n :
45- if ( n < 0L )
46- ThrowArgumentOutOfRangeException ( paramName , value ) ;
47- return ;
48-
49- case float n :
50- if ( n < 0F )
51- ThrowArgumentOutOfRangeException ( paramName , value ) ;
52- return ;
53- case double n :
54- if ( n < 0D )
55- ThrowArgumentOutOfRangeException ( paramName , value ) ;
56- return ;
57- case decimal n :
58- if ( n < 0M )
59- ThrowArgumentOutOfRangeException ( paramName , value ) ;
60- return ;
61- default :
62- throw new InvalidOperationException ( $ "Invalid type '{ typeof ( T ) . AssemblyQualifiedName } ' for { paramName } .") ;
63- }
64-
65- static void ThrowArgumentOutOfRangeException ( string ? paramName , object value )
66- {
67- throw new ArgumentOutOfRangeException ( paramName , value , $ "{ paramName } ('{ value } ') must not be negative.") ;
68- }
69- }
70- }
71-
7218 extension ( Convert )
7319 {
7420#if ! SUPPORTS_HEXADECIMAL_STRING_CONVERSION
@@ -98,78 +44,6 @@ public static byte[] FromHexString(string s)
9844#endif
9945 }
10046
101- extension < TSource > ( IEnumerable < TSource> source)
102- {
103- #if ! SUPPORTS_CHUNK_LINQ_EXTENSION
104- /// <summary>
105- /// Split the elements of a sequence into chunks of size at most <paramref name="size"/>.
106- /// </summary>
107- /// <remarks>
108- /// Every chunk except the last will be of size <paramref name="size"/>.
109- /// The last chunk will contain the remaining elements and may be of a smaller size.
110- /// </remarks>
111- /// <param name="size">Maximum size of each chunk.</param>
112- /// <returns>
113- /// An <see cref="IEnumerable{T}"/> that contains the elements of the input
114- /// sequence split into chunks of size <paramref name="size"/>.
115- /// </returns>
116- public IEnumerable < TSource [ ] > Chunk ( int size )
117- {
118- // Note: this polyfill was directly copied from .NET's source code:
119- // https://github.com/dotnet/runtime/blob/main/src/libraries/System.Linq/src/System/Linq/Chunk.cs.
120-
121- using IEnumerator < TSource > enumerator = source . GetEnumerator ( ) ;
122-
123- if ( enumerator . MoveNext ( ) )
124- {
125- var count = Math . Min ( size , 4 ) ;
126- int index ;
127-
128- do
129- {
130- var array = new TSource [ count ] ;
131-
132- array [ 0 ] = enumerator . Current ;
133- index = 1 ;
134-
135- if ( size != array . Length )
136- {
137- for ( ; index < size && enumerator . MoveNext ( ) ; index ++ )
138- {
139- if ( index >= array . Length )
140- {
141- count = ( int ) Math . Min ( ( uint ) size , 2 * ( uint ) array . Length ) ;
142- Array . Resize ( ref array , count ) ;
143- }
144-
145- array [ index ] = enumerator . Current ;
146- }
147- }
148-
149- else
150- {
151- var local = array ;
152- Debug . Assert ( local . Length == size ) ;
153- for ( ; ( uint ) index < ( uint ) local . Length && enumerator . MoveNext ( ) ; index ++ )
154- {
155- local [ index ] = enumerator . Current ;
156- }
157- }
158-
159- if ( index != array . Length )
160- {
161- Array . Resize ( ref array , index ) ;
162- }
163-
164- yield return array ;
165- }
166-
167- while ( index >= size && enumerator . MoveNext ( ) ) ;
168- }
169- }
170- #endif
171- }
172-
17347 extension ( OperatingSystem )
17448 {
17549#if ! SUPPORTS_OPERATING_SYSTEM_VERSIONS_COMPARISON
0 commit comments