Skip to content

Commit

Permalink
Make MinLength int again
Browse files Browse the repository at this point in the history
It's actually preferable, it seems:
https://stackoverflow.com/a/1148505
  • Loading branch information
aradalvand committed Sep 9, 2023
1 parent c3f3a8f commit b9a0ef5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/Sqids/SqidsEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public sealed class SqidsEncoder
#endif
{
private const int MinAlphabetLength = 3;
private const int MaxMinLength = 255;
private const int MaxStackallocSize = 256; // NOTE: In bytes — this value is essentially arbitrary, the Microsoft docs is using 1024 but recommends being more conservative when choosing the value (https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/stackalloc), Hashids apparently uses 512 (https://github.com/ullmark/hashids.net/blob/9b1c69de4eedddf9d352c96117d8122af202e90f/src/Hashids.net/Hashids.cs#L17), and this article (https://vcsjones.dev/stackalloc/) uses 256. I've tried to be pretty cautious and gone with a low value.

private readonly char[] _alphabet;
Expand Down Expand Up @@ -80,6 +81,12 @@ public SqidsEncoder(SqidsOptions options)
$"The alphabet must contain at least {MinAlphabetLength} characters."
);

if (options.MinLength < 0 || options.MinLength > MaxMinLength)
throw new ArgumentOutOfRangeException(
nameof(options.MinLength),
$"The minimum length must be between 0 and {MaxMinLength}."
);

_minLength = options.MinLength;

// NOTE: Cleanup the blocklist:
Expand Down
4 changes: 2 additions & 2 deletions src/Sqids/SqidsOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public sealed class SqidsOptions
/// <summary>
/// The minimum length for the IDs.
/// The default is 0; meaning the IDs will be as short as possible.
/// 255 is the maximum; hence `byte` as the type.
/// 255 is the maximum.
/// </summary>
public byte MinLength { get; set; } = 0;
public int MinLength { get; set; } = 0;

/// <summary>
/// List of blocked words that must not appear in the IDs.
Expand Down

0 comments on commit b9a0ef5

Please sign in to comment.