Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alphabet: handle large ratio of alphabet to seps #30

Merged
merged 1 commit into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/Hashids.net/Hashids.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ private void SetupSeps()

seps = ConsistentShuffle(seps, salt);

if (seps.Length == 0 || (alphabet.Length / seps.Length) > SEP_DIV)
if (seps.Length == 0 || ((float)alphabet.Length / seps.Length) > SEP_DIV)
{
var sepsLength = (int)Math.Ceiling(alphabet.Length / SEP_DIV);
var sepsLength = (int)Math.Ceiling((float)alphabet.Length / SEP_DIV);
if (sepsLength == 1)
sepsLength = 2;

Expand Down
7 changes: 7 additions & 0 deletions test/Hashids.net.test/Hashids_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ void it_can_encode_with_a_custom_alphabet()
h.Encode(1, 2, 3, 4, 5).Should().Be("6nhmFDikA0");
}

[Fact]
void it_can_encode_with_a_custom_alphabet_and_few_seps()
{
var h = new Hashids(salt, 0, "ABCDEFGHIJKMNOPQRSTUVWXYZ23456789");
h.Encode(1, 2, 3, 4, 5).Should().Be("44HYIRU3TO");
}

[Fact]
void it_does_not_produce_repeating_patterns_for_identical_numbers()
{
Expand Down