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

Feature/base32fix #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
.mono/

# Build results
[Dd]ebug/
Expand Down
15 changes: 13 additions & 2 deletions src/Ulid.Unity/Assets/Scripts/Ulid/Ulid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace System // wa-o, System Namespace!?
// https://en.wikipedia.org/wiki/Base32
static readonly char[] Base32Text = "0123456789ABCDEFGHJKMNPQRSTVWXYZ".ToCharArray();
static readonly byte[] Base32Bytes = Encoding.UTF8.GetBytes(Base32Text);
static readonly byte[] CharToBase32 = new byte[] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17, 255, 18, 19, 255, 20, 21, 255, 22, 23, 24, 25, 26, 255, 27, 28, 29, 30, 31, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17, 255, 18, 19, 255, 20, 21, 255, 22, 23, 24, 25, 26, 255, 27, 28, 29, 30, 31 };
static readonly byte[] CharToBase32 = new byte[] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17, 1, 18, 19, 1, 20, 21, 0, 22, 23, 24, 25, 26, 255, 27, 28, 29, 30, 31, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17, 1, 18, 19, 1, 20, 21, 0, 22, 23, 24, 25, 26, 255, 27, 28, 29, 30, 31 };
static readonly DateTimeOffset UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

public static readonly Ulid MinValue = new Ulid(UnixEpoch.ToUnixTimeMilliseconds(), new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
Expand Down Expand Up @@ -278,6 +278,10 @@ public static Ulid Parse(string base32)
public static Ulid Parse(ReadOnlySpan<char> base32)
{
if (base32.Length != 26) throw new ArgumentException("invalid base32 length, length:" + base32.Length);
for (int i = 0; i < base32.Length; i++)
{
if (CharToBase32[base32[i]] == 255) throw new ArgumentException("invalid base32 character, character:" + base32[i]);
}
return new Ulid(base32);
}

Expand All @@ -299,7 +303,14 @@ public static bool TryParse(ReadOnlySpan<char> base32, out Ulid ulid)
ulid = default(Ulid);
return false;
}

for (int i = 0; i < base32.Length; i++)
{
if (CharToBase32[base32[i]] == 255)
{
ulid = default(Ulid);
return false;
}
}
try
{
ulid = new Ulid(base32);
Expand Down
15 changes: 13 additions & 2 deletions src/Ulid/Ulid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace System // wa-o, System Namespace!?
// https://en.wikipedia.org/wiki/Base32
static readonly char[] Base32Text = "0123456789ABCDEFGHJKMNPQRSTVWXYZ".ToCharArray();
static readonly byte[] Base32Bytes = Encoding.UTF8.GetBytes(Base32Text);
static readonly byte[] CharToBase32 = new byte[] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17, 255, 18, 19, 255, 20, 21, 255, 22, 23, 24, 25, 26, 255, 27, 28, 29, 30, 31, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17, 255, 18, 19, 255, 20, 21, 255, 22, 23, 24, 25, 26, 255, 27, 28, 29, 30, 31 };
static readonly byte[] CharToBase32 = new byte[] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17, 1, 18, 19, 1, 20, 21, 0, 22, 23, 24, 25, 26, 255, 27, 28, 29, 30, 31, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17, 1, 18, 19, 1, 20, 21, 0, 22, 23, 24, 25, 26, 255, 27, 28, 29, 30, 31 };
static readonly DateTimeOffset UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

public static readonly Ulid MinValue = new Ulid(UnixEpoch.ToUnixTimeMilliseconds(), new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
Expand Down Expand Up @@ -278,6 +278,10 @@ public static Ulid Parse(string base32)
public static Ulid Parse(ReadOnlySpan<char> base32)
{
if (base32.Length != 26) throw new ArgumentException("invalid base32 length, length:" + base32.Length);
for (int i = 0; i < base32.Length; i++)
{
if (CharToBase32[base32[i]] == 255) throw new ArgumentException("invalid base32 character, character:" + base32[i]);
}
return new Ulid(base32);
}

Expand All @@ -299,7 +303,14 @@ public static bool TryParse(ReadOnlySpan<char> base32, out Ulid ulid)
ulid = default(Ulid);
return false;
}

for (int i = 0; i < base32.Length; i++)
{
if (CharToBase32[base32[i]] == 255)
{
ulid = default(Ulid);
return false;
}
}
try
{
ulid = new Ulid(base32);
Expand Down
8 changes: 8 additions & 0 deletions tests/Ulid.Tests/UlidTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,21 @@ public void UlidParseRejectsInvalidStrings()
{
Assert.Throws<ArgumentException>(() => Ulid.Parse("1234"));
Assert.Throws<ArgumentException>(() => Ulid.Parse(Guid.NewGuid().ToString()));
Assert.Throws<ArgumentException>(() => Ulid.Parse("01HV0CXYMHVZD8AETQVAYVDT0U"));
}

[Fact]
public void UlidTryParseFailsForInvalidStrings()
{
Assert.False(Ulid.TryParse("1234", out _));
Assert.False(Ulid.TryParse(Guid.NewGuid().ToString(), out _));
Assert.False(Ulid.TryParse("01HV0CXYMHVZD8AETQVAYVDT0U", out _));
}

[Fact]
public void CrockfordBase32DecodingMapsILO()
{
Ulid.Parse("01HV0CXYMHVZD8AETQVAILOilo").Should().Be(Ulid.Parse("01HV0CXYMHVZD8AETQVA110110"));
}

#if NET6_0_OR_GREATER
Expand Down