Skip to content

Commit

Permalink
Add multi-byte character check
Browse files Browse the repository at this point in the history
  • Loading branch information
aradalvand committed Sep 9, 2023
1 parent cd5ca9b commit ac8145d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Sqids/SqidsEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ public SqidsEncoder(SqidsOptions options)
$"The alphabet must contain at least {MinAlphabetLength} characters."
);

if (Encoding.UTF8.GetByteCount(options.Alphabet) != options.Alphabet.Length)
throw new ArgumentOutOfRangeException(
nameof(options.MinLength),
$"The alphabet must not contain multi-byte characters."
);

if (options.MinLength < 0 || options.MinLength > MaxMinLength)
throw new ArgumentOutOfRangeException(
nameof(options.MinLength),
Expand Down
1 change: 1 addition & 0 deletions test/Sqids.Tests/AlphabetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ int[] numbers

[TestCase("aabcdefg")] // NOTE: Repeated characters
[TestCase("ab")] // NOTE: Too short
[TestCase("ë1092")] // NOTE: Contains a multi-byte character
public void Instantiate_WithInvalidAlphabet_Throws(string invalidAlphabet)
{
#if NET7_0_OR_GREATER
Expand Down

0 comments on commit ac8145d

Please sign in to comment.