File tree Expand file tree Collapse file tree 5 files changed +55
-4
lines changed Expand file tree Collapse file tree 5 files changed +55
-4
lines changed Original file line number Diff line number Diff line change @@ -214,7 +214,7 @@ public string Encode(ReadOnlySpan<byte> bytes)
214
214
}
215
215
216
216
int outputLen = GetSafeCharCountForEncoding ( bytes ) ;
217
- Span < char > output = outputLen < 1024 ? stackalloc char [ outputLen ] : new char [ outputLen ] ;
217
+ Span < char > output = outputLen < Bits . SafeStackMaxAllocSize ? stackalloc char [ outputLen ] : new char [ outputLen ] ;
218
218
internalEncode ( bytes , output , Alphabet . Value ) ;
219
219
return new string ( output ) ;
220
220
}
Original file line number Diff line number Diff line change @@ -211,7 +211,7 @@ public string Encode(ReadOnlySpan<byte> bytes, bool padding)
211
211
// we are ok with slightly larger buffer since the output string will always
212
212
// have the exact length of the output produced.
213
213
int outputLen = GetSafeCharCountForEncoding ( bytes ) ;
214
- Span < char > output = outputLen < 1024 ? stackalloc char [ outputLen ] : new char [ outputLen ] ;
214
+ Span < char > output = outputLen < Bits . SafeStackMaxAllocSize ? stackalloc char [ outputLen ] : new char [ outputLen ] ;
215
215
if ( ! internalEncode (
216
216
bytes ,
217
217
output ,
Original file line number Diff line number Diff line change @@ -224,7 +224,7 @@ public string Encode(ReadOnlySpan<byte> bytes)
224
224
225
225
int numZeroes = getZeroCount ( bytes ) ;
226
226
int outputLen = getSafeCharCountForEncoding ( bytes . Length , numZeroes ) ;
227
- Span < char > output = outputLen < 1024 ? stackalloc char [ outputLen ] : new char [ outputLen ] ;
227
+ Span < char > output = outputLen < Bits . SafeStackMaxAllocSize ? stackalloc char [ outputLen ] : new char [ outputLen ] ;
228
228
229
229
return internalEncode ( bytes , output , numZeroes , out int numCharsWritten )
230
230
? new string ( output [ ..numCharsWritten ] )
Original file line number Diff line number Diff line change @@ -76,7 +76,7 @@ public string Encode(ReadOnlySpan<byte> bytes)
76
76
}
77
77
78
78
int outputLen = GetSafeCharCountForEncoding ( bytes ) ;
79
- Span < char > output = outputLen < 1024 ? stackalloc char [ outputLen ] : new char [ outputLen ] ;
79
+ Span < char > output = outputLen < Bits . SafeStackMaxAllocSize ? stackalloc char [ outputLen ] : new char [ outputLen ] ;
80
80
81
81
return internalEncode ( bytes , output , out int numCharsWritten )
82
82
? new string ( output [ ..numCharsWritten ] )
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Text ;
4
+
5
+ namespace SimpleBase
6
+ {
7
+ /// <summary>
8
+ /// Helper functions for bit operations.
9
+ /// </summary>
10
+ internal static class Bits
11
+ {
12
+ /// <summary>
13
+ /// Safe one-shot maximum amount to be allocated on stack for temporary buffers and alike.
14
+ /// </summary>
15
+ internal const int SafeStackMaxAllocSize = 1024 ;
16
+
17
+ /// <summary>
18
+ /// Converts a byte array to a hexadecimal string.
19
+ /// </summary>
20
+ /// <param name="bytes"></param>
21
+ /// <returns></returns>
22
+ internal static ulong BigEndianBytesToUInt64 ( ReadOnlySpan < byte > bytes )
23
+ {
24
+ if ( bytes . Length > sizeof ( ulong ) )
25
+ {
26
+ throw new ArgumentOutOfRangeException ( nameof ( bytes ) , "Byte array too long to convert to UInt64" ) ;
27
+ }
28
+
29
+ ulong result = 0 ;
30
+ for ( int i = 0 ; i < bytes . Length ; i ++ )
31
+ {
32
+ result = ( result << 8 ) | bytes [ i ] ;
33
+ }
34
+ return result ;
35
+ }
36
+
37
+ internal static void UInt64ToBigEndianBytes ( ulong value , Span < byte > output )
38
+ {
39
+ if ( output . Length < sizeof ( ulong ) )
40
+ {
41
+ throw new ArgumentException ( "Output is too small" , nameof ( output ) ) ;
42
+ }
43
+ int byteCount = sizeof ( ulong ) ;
44
+ for ( int i = byteCount - 1 ; i >= 0 ; i -- )
45
+ {
46
+ output [ i ] = ( byte ) ( value & 0xFF ) ;
47
+ value >>= 8 ;
48
+ }
49
+ }
50
+ }
51
+ }
You can’t perform that action at this time.
0 commit comments