Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Hashids.net/Hashids.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public partial class Hashids : IHashids
public const string DEFAULT_ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
public const string DEFAULT_SEPS = "cfhistuCFHISTU";
public const int MIN_ALPHABET_LENGTH = 16;
public const int MAX_STACKALLOC_SIZE = 512;

private const double SEP_DIV = 3.5;
private const double GUARD_DIV = 12.0;
Expand Down Expand Up @@ -289,13 +290,13 @@ private string GenerateHashFrom(ReadOnlySpan<long> numbers)

var stringBuilder = StringBuilderPool.Get();

Span<char> alphabet = _alphabet.Length < 512 ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
Span<char> alphabet = _alphabet.Length < MAX_STACKALLOC_SIZE ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
_alphabet.CopyTo(alphabet);

var lottery = alphabet[(int)(numbersHashInt % _alphabet.Length)];
stringBuilder.Append(lottery);

Span<char> shuffleBuffer = _alphabet.Length < 512 ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
Span<char> shuffleBuffer = _alphabet.Length < MAX_STACKALLOC_SIZE ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
shuffleBuffer[0] = lottery;
_salt.AsSpan().Slice(0, Math.Min(_salt.Length, _alphabet.Length - 1)).CopyTo(shuffleBuffer.Slice(1));

Expand Down Expand Up @@ -419,10 +420,10 @@ private long[] GetNumbersFrom(string hash)

var result = new long[hashArray.Length];

Span<char> alphabet = _alphabet.Length < 512 ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
Span<char> alphabet = _alphabet.Length < MAX_STACKALLOC_SIZE ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
_alphabet.CopyTo(alphabet);

Span<char> buffer = _alphabet.Length < 512 ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
Span<char> buffer = _alphabet.Length < MAX_STACKALLOC_SIZE ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
buffer[0] = lottery;
_salt.AsSpan().Slice(0, Math.Min(_salt.Length, _alphabet.Length - 1)).CopyTo(buffer.Slice(1));

Expand Down