Skip to content

Commit 4365459

Browse files
committed
use a constant for stack max alloc threshold
1 parent 7233483 commit 4365459

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

src/Base16.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public string Encode(ReadOnlySpan<byte> bytes)
214214
}
215215

216216
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];
218218
internalEncode(bytes, output, Alphabet.Value);
219219
return new string(output);
220220
}

src/Base32.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public string Encode(ReadOnlySpan<byte> bytes, bool padding)
211211
// we are ok with slightly larger buffer since the output string will always
212212
// have the exact length of the output produced.
213213
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];
215215
if (!internalEncode(
216216
bytes,
217217
output,

src/Base58.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public string Encode(ReadOnlySpan<byte> bytes)
224224

225225
int numZeroes = getZeroCount(bytes);
226226
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];
228228

229229
return internalEncode(bytes, output, numZeroes, out int numCharsWritten)
230230
? new string(output[..numCharsWritten])

src/Base85.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public string Encode(ReadOnlySpan<byte> bytes)
7676
}
7777

7878
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];
8080

8181
return internalEncode(bytes, output, out int numCharsWritten)
8282
? new string(output[..numCharsWritten])

src/Bits.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

0 commit comments

Comments
 (0)