Skip to content

Commit a7478cd

Browse files
committed
use splitter for multiple numbers
1 parent e28e255 commit a7478cd

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

src/Hashids.net/Hashids.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -451,20 +451,26 @@ private long[] GetNumbersFrom(string hash)
451451
if (string.IsNullOrWhiteSpace(hash))
452452
return Array.Empty<long>();
453453

454-
var hashArray = hash.Split(_guards, StringSplitOptions.RemoveEmptyEntries);
455-
if (hashArray.Length == 0)
454+
var guardedHash = hash.AsSpan();
455+
var (count, ranges) = Split(guardedHash, _guards);
456+
457+
if (count == 0)
456458
return Array.Empty<long>();
457-
458-
var unguardedIdx = (hashArray.Length is 3 or 2) ? 1 : 0;
459-
var hashBreakdown = hashArray[unguardedIdx];
459+
460+
var unguardedIndex = count is 3 or 2 ? 1 : 0;
461+
var (start, offset) = ranges[unguardedIndex];
462+
var hashBreakdown = guardedHash.Slice(start, offset);
463+
464+
ArrayPool<(int, int)>.Shared.Return(ranges);
460465

461466
var lottery = hashBreakdown[0];
462467
if (lottery == '\0') // default(char) is '\0'
463468
return Array.Empty<long>();
464469

465-
hashArray = hashBreakdown.Substring(1).Split(_seps, StringSplitOptions.RemoveEmptyEntries);
470+
var hashBuffer = hashBreakdown.Slice(1);
471+
(count, ranges) = Split(hashBuffer, _seps);
466472

467-
var result = new long[hashArray.Length];
473+
var result = new long[count];
468474

469475
Span<char> alphabet = _alphabet.Length < 512 ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
470476
_alphabet.CopyTo(alphabet);
@@ -476,16 +482,19 @@ private long[] GetNumbersFrom(string hash)
476482
var startIndex = 1 + _salt.Length;
477483
var length = _alphabet.Length - startIndex;
478484

479-
for (var j = 0; j < hashArray.Length; j++)
485+
for (var j = 0; j < count; j++)
480486
{
481-
var subHash = hashArray[j].AsSpan();
487+
(start, offset) = ranges[j];
488+
var subHash = hashBuffer.Slice(start, offset);
482489

483490
if (length > 0)
484491
alphabet.Slice(0, length).CopyTo(buffer.Slice(startIndex));
485492

486493
ConsistentShuffle(alphabet, buffer);
487494
result[j] = Unhash(subHash, alphabet);
488495
}
496+
497+
ArrayPool<(int, int)>.Shared.Return(ranges);
489498

490499
// regenerate hash from numbers and compare to given hash to ensure the correct parameters were used
491500
if (GenerateHashFrom(result).Equals(hash, StringComparison.Ordinal))

0 commit comments

Comments
 (0)