5
5
using System ;
6
6
#if NETCOREAPP
7
7
using System . Buffers ;
8
+ using System . Buffers . Text ;
8
9
#endif
9
10
using System . Diagnostics ;
10
11
using System . Globalization ;
@@ -60,6 +61,9 @@ public static byte[] Base64UrlDecode(string input, int offset, int count)
60
61
61
62
ValidateParameters ( input . Length , nameof ( input ) , offset , count ) ;
62
63
64
+ #if NET9_0_OR_GREATER
65
+ return Base64Url . DecodeFromChars ( input . AsSpan ( offset , count ) ) ;
66
+ #else
63
67
// Special-case empty input
64
68
if ( count == 0 )
65
69
{
@@ -70,6 +74,7 @@ public static byte[] Base64UrlDecode(string input, int offset, int count)
70
74
var buffer = new char [ GetArraySizeRequiredToDecode ( count ) ] ;
71
75
72
76
return Base64UrlDecode ( input , offset , buffer , bufferOffset : 0 , count : count ) ;
77
+ #endif
73
78
}
74
79
75
80
/// <summary>
@@ -99,6 +104,9 @@ public static byte[] Base64UrlDecode(string input, int offset, char[] buffer, in
99
104
ValidateParameters ( input . Length , nameof ( input ) , offset , count ) ;
100
105
ArgumentOutOfRangeThrowHelper . ThrowIfNegative ( bufferOffset ) ;
101
106
107
+ #if NET9_0_OR_GREATER
108
+ return Base64Url . DecodeFromChars ( input . AsSpan ( offset , count ) ) ;
109
+ #else
102
110
if ( count == 0 )
103
111
{
104
112
return Array . Empty < byte > ( ) ;
@@ -150,6 +158,7 @@ public static byte[] Base64UrlDecode(string input, int offset, char[] buffer, in
150
158
// Decode.
151
159
// If the caller provided invalid base64 chars, they'll be caught here.
152
160
return Convert . FromBase64CharArray ( buffer , bufferOffset , arraySizeRequired ) ;
161
+ #endif
153
162
}
154
163
155
164
/// <summary>
@@ -314,6 +323,9 @@ public static int GetArraySizeRequiredToEncode(int count)
314
323
[ SkipLocalsInit ]
315
324
public static string Base64UrlEncode ( ReadOnlySpan < byte > input )
316
325
{
326
+ #if NET9_0_OR_GREATER
327
+ return Base64Url . EncodeToString ( input ) ;
328
+ #else
317
329
const int StackAllocThreshold = 128 ;
318
330
319
331
if ( input . IsEmpty )
@@ -337,6 +349,7 @@ public static string Base64UrlEncode(ReadOnlySpan<byte> input)
337
349
}
338
350
339
351
return base64Url ;
352
+ #endif
340
353
}
341
354
342
355
#if NET9_0_OR_GREATER
@@ -347,9 +360,11 @@ public static string Base64UrlEncode(ReadOnlySpan<byte> input)
347
360
/// <param name="output">The buffer to place the result in.</param>
348
361
/// <returns></returns>
349
362
public static int Base64UrlEncode ( ReadOnlySpan < byte > input , Span < char > output )
363
+ {
364
+ return Base64Url . EncodeToChars ( input , output ) ;
365
+ }
350
366
#else
351
367
private static int Base64UrlEncode ( ReadOnlySpan < byte > input , Span < char > output )
352
- #endif
353
368
{
354
369
Debug . Assert ( output . Length >= GetArraySizeRequiredToEncode ( input . Length ) ) ;
355
370
@@ -383,9 +398,10 @@ private static int Base64UrlEncode(ReadOnlySpan<byte> input, Span<char> output)
383
398
384
399
return charsWritten ;
385
400
}
401
+ #endif
386
402
#endif
387
403
388
- private static int GetNumBase64PaddingCharsToAddForDecode ( int inputLength )
404
+ private static int GetNumBase64PaddingCharsToAddForDecode ( int inputLength )
389
405
{
390
406
switch ( inputLength % 4 )
391
407
{
0 commit comments