Skip to content
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ Converts the ULID to a canonical string representation. Format arguments are ign
`==`, `!=`, `<`, `<=`, `>`, `>=`.
- Implements standard comparison and equality methods:\
`CompareTo`, `Equals`, `GetHashCode`.
- Provides implicit operators to and from `Guid`.
- Provides implicit operators to and from `Guid` and `string`.

### GenerationOptions

Expand Down Expand Up @@ -448,11 +448,11 @@ Job=DefaultJob
| ToGuid | Ulid | 0.7462 ns | 0.0117 ns | - | - |
| ToGuid | NUlid | 0.2691 ns | 0.0070 ns | - | - |

| ToString | ByteAetherUlid | 12.2227 ns | 0.2543 ns | 0.0096 | 80 B |
| ToString | NetUlid | 23.7706 ns | 0.2422 ns | 0.0095 | 80 B |
| ToString | Ulid | 11.1126 ns | 0.2121 ns | 0.0096 | 80 B |
| ToString | NUlid | 28.9672 ns | 0.1506 ns | 0.0095 | 80 B |
| ToString | Guid | 7.2446 ns | 0.0341 ns | 0.0115 | 96 B |
| ToString | ByteAetherUlid | 12.254 ns | 0.2822 ns | 0.0096 | 80 B |
| ToString | NetUlid | 26.314 ns | 0.2748 ns | 0.0095 | 80 B |
| ToString | Ulid | 12.373 ns | 0.1887 ns | 0.0096 | 80 B |
| ToString | NUlid | 27.661 ns | 0.2090 ns | 0.0095 | 80 B |
| ToString | Guid | 7.208 ns | 0.0447 ns | 0.0115 | 96 B |

| CompareTo | ByteAetherUlid | 0.0007 ns | 0.0022 ns | - | - |
| CompareTo | NetUlid | 3.6812 ns | 0.0298 ns | - | - |
Expand Down
2 changes: 1 addition & 1 deletion src/ByteAether.Ulid.Tests/Ulid.Comparable.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void CompareTo_NullUlid_ShouldReturnPositive()
var ulid = Ulid.New();

// Act
var comparisonResult = ulid.CompareTo(null);
var comparisonResult = ulid.CompareTo((object?)null);

// Assert
Assert.True(comparisonResult > 0);
Expand Down
4 changes: 2 additions & 2 deletions src/ByteAether.Ulid.Tests/Ulid.Equatable.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void Equals_Null_ShouldReturnFalse()
var ulid = Ulid.New();

// Act & Assert
Assert.False(ulid.Equals(null));
Assert.False(ulid.Equals((object?)null));
}

[Fact]
Expand All @@ -89,4 +89,4 @@ public void Equals_SameAsObject_ShouldReturnTrue()
// Act & Assert
Assert.True(ulid1.Equals((object)ulid2));
}
}
}
6 changes: 3 additions & 3 deletions src/ByteAether.Ulid.Tests/Ulid.String.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ByteAether.Ulid.Tests;
public class UlidStringTests
{
private static readonly string _goodUlidString = "01F8MECHZX3TBDSZG8P8X7XRMM";
private const string _goodUlidString = "01F8MECHZX3TBDSZG8P8X7XRMM";

[Fact]
public void ToString_ShouldReturnExpectedString()
Expand Down Expand Up @@ -221,7 +221,7 @@ public void TryParse_ReadOnlySpanByte_InvalidInput_ShouldReturnFalse(string inpu
[Fact]
public void ToString_WrongLetters_ShouldReplaceWithCorrect()
{
// Crockford's Base32 subtitution test
// Crockford's Base32 substitution test
var inputChars = new[] { 'O', 'o', 'I', 'i', 'L', 'l', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' };
var outputChars = new[] { '0', '0', '1', '1', '1', '1', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z' };

Expand All @@ -236,4 +236,4 @@ public void ToString_WrongLetters_ShouldReplaceWithCorrect()
Assert.Equal(outputString, resultString);
}
}
}
}
2 changes: 1 addition & 1 deletion src/ByteAether.Ulid/PACKAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Create from existing `Guid`.
`==`, `!=`, `<`, `<=`, `>`, `>=`.
- Implements standard comparison and equality methods:\
`CompareTo`, `Equals`, `GetHashCode`.
- Provides implicit operators to and from `Guid`.
- Provides implicit operators to and from `Guid` and `string`.

### GenerationOptions

Expand Down
48 changes: 40 additions & 8 deletions src/ByteAether.Ulid/Ulid.String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text;
#if !(NETSTANDARD2_1_OR_GREATER || NETCOREAPP)
using System.Runtime.InteropServices;
#endif

namespace ByteAether.Ulid;

Expand Down Expand Up @@ -82,17 +79,28 @@ public readonly partial struct Ulid
#else
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public readonly string ToString(string? format = null, IFormatProvider? formatProvider = null)
public readonly string ToString(string? format, IFormatProvider? formatProvider) => ToString();

/// <summary>
/// Returns a string representation of the current instance of <see cref="Ulid"/> in its canonical Crockford's Base32 format.'
/// </summary>
/// <returns>Crockford's Base32 representation of the ULID</returns>
#if NET5_0_OR_GREATER
[SkipLocalsInit]
#endif
#if NETCOREAPP3_0_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public override readonly string ToString()
{
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP
return string.Create(UlidStringLength, this, (span, ulid) => ulid.TryFill(span, _base32Chars));
#else
Span<char> span = stackalloc char[UlidStringLength];
TryFill(span, _base32Chars);
unsafe
{
return new string((char*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span)), 0, UlidStringLength);
}
return span.ToString();
#endif
}

Expand Down Expand Up @@ -385,4 +393,28 @@ private bool TryFill<T>(Span<T> span, T[] map)

return true;
}

/// <summary>
/// Allows implicit conversion of <see cref="Ulid"/> to <see cref="string"/>.
/// </summary>
/// <param name="ulid"></param>
/// <returns></returns>
#if NETCOREAPP3_0_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static implicit operator string(Ulid ulid) => ulid.ToString();

/// <summary>
/// Allows implicit conversion of <see cref="string"/> to <see cref="Ulid"/>.
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
#if NETCOREAPP3_0_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static implicit operator Ulid(string str) => Parse(str);
}