Skip to content

Commit

Permalink
Minor Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Taiizor committed Dec 19, 2024
1 parent ce781e5 commit 5cc2723
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
45 changes: 43 additions & 2 deletions src/UUID/Constructor/UUID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,22 @@ public readonly partial struct UUID(ulong timestamp, ulong random) : IEquatable<
/// <summary>
/// The size of the UUID in bytes.
/// </summary>
/// <remarks>
/// This constant represents the fixed size of a UUID, which is 16 bytes (128 bits).
/// This follows the standard UUID specification as defined in RFC 4122.
/// </remarks>
private const int SIZE = 16;

/// <summary>
/// Gets the random component of the UUID.
/// </summary>
/// <remarks>
/// The random component ensures uniqueness even when UUIDs are generated
/// within the same timestamp. It consists of 64 bits of cryptographically
/// secure random data, providing a very low probability of collisions.
/// This is especially important in distributed systems where multiple UUIDs
/// might be generated simultaneously.
/// </remarks>
public ulong Random { get; } = random;

Check warning on line 41 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Build (Release, UUID.sln)

Parameter 'ulong random' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

Check warning on line 41 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Build (Release, UUID.sln)

Parameter 'ulong random' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

Check warning on line 41 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Build (Release, UUID.sln)

Parameter 'ulong random' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

Check warning on line 41 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Build (Release, UUID.sln)

Parameter 'ulong random' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

Check warning on line 41 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Build (Release, UUID.sln)

Parameter 'ulong random' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

Check warning on line 41 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Build (Release, UUID.sln)

Parameter 'ulong random' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

Check warning on line 41 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Build (Release, UUID.sln)

Parameter 'ulong random' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

Check warning on line 41 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Build (Release, UUID.sln)

Parameter 'ulong random' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

Check warning on line 41 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Build (Release, UUID.sln)

Parameter 'ulong random' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

Check warning on line 41 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Build (Release, UUID.sln)

Parameter 'ulong random' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

Check warning on line 41 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp, Release, UUID.sln)

Parameter 'ulong random' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

Check warning on line 41 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp, Release, UUID.sln)

Parameter 'ulong random' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

Check warning on line 41 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp, Release, UUID.sln)

Parameter 'ulong random' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

Check warning on line 41 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp, Release, UUID.sln)

Parameter 'ulong random' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

Check warning on line 41 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp, Release, UUID.sln)

Parameter 'ulong random' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

/// <summary>
Expand Down Expand Up @@ -115,6 +126,16 @@ private static ulong GenerateRandom()
/// <returns>A new UUID instance.</returns>
/// <exception cref="ArgumentNullException">Thrown when input is null.</exception>
/// <exception cref="FormatException">Thrown when input is not in the correct format.</exception>
/// <example>
/// <code>
/// // Parse a UUID from its string representation
/// var uuidString = "0123456789ABCDEF0123456789ABCDEF";
/// var uuid = UUID.Parse(uuidString);
///
/// // The parsed UUID can then be used in comparisons or conversions
/// var guid = uuid.ToGuid();
/// </code>
/// </example>
public static UUID Parse(string input)
{
if (input == null)
Expand Down Expand Up @@ -142,7 +163,7 @@ public static UUID Parse(string input)
public static bool TryParse(string? input, out UUID result)
{
result = default;

if (string.IsNullOrEmpty(input) || input.Length != 32)

Check warning on line 167 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Build (Release, UUID.sln)

Dereference of a possibly null reference.

Check warning on line 167 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Build (Release, UUID.sln)

Dereference of a possibly null reference.

Check warning on line 167 in src/UUID/Constructor/UUID.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp, Release, UUID.sln)

Dereference of a possibly null reference.
{
return false;
Expand Down Expand Up @@ -333,6 +354,19 @@ public static bool TryFromBase64(string base64, out UUID result)
/// Converts the UUID to a byte array.
/// </summary>
/// <returns>A byte array representation of the UUID.</returns>
/// <remarks>
/// This operation allocates a new byte array. For better performance in high-throughput scenarios,
/// consider using TryWriteBytes instead when working with existing byte arrays or spans.
/// The returned array is always 16 bytes long, with the first 8 bytes containing the timestamp
/// and the last 8 bytes containing the random component.
/// </remarks>
/// <example>
/// <code>
/// var uuid = UUID.New();
/// byte[] bytes = uuid.ToByteArray();
/// // bytes can now be used for serialization or network transmission
/// </code>
/// </example>
public byte[] ToByteArray()
{
byte[] bytes = new byte[SIZE];
Expand Down Expand Up @@ -369,7 +403,7 @@ public bool TryWriteBytes(byte[] destination)

byte[] timestampBytes = BitConverter.GetBytes(_timestamp);
byte[] randomBytes = BitConverter.GetBytes(random);

Array.Copy(timestampBytes, 0, destination, 0, 8);
Array.Copy(randomBytes, 0, destination, 8, 8);

Expand Down Expand Up @@ -593,6 +627,13 @@ public int CompareTo(object? obj)
/// <summary>
/// Determines whether one UUID is less than another UUID.
/// </summary>
/// <remarks>
/// The comparison is performed in two steps:
/// 1. First, timestamps are compared
/// 2. If timestamps are equal, random components are compared
/// This ensures a consistent and meaningful ordering of UUIDs,
/// particularly useful when UUIDs are used as database keys.
/// </remarks>
public static bool operator <(UUID left, UUID right)
{
return left.CompareTo(right) < 0;
Expand Down
4 changes: 2 additions & 2 deletions src/UUID/Extensions/ArrayExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ public static bool TryFill(this UUID[] array)
{
ulong timestamp = BitConverter.ToUInt64(randomBytes.Slice(offset, 8));
ulong random = BitConverter.ToUInt64(randomBytes.Slice(offset + 8, 8));

array[i] = new UUID(timestamp, random);
}
#else
// Generate all random bytes at once
byte[] randomBytes = new byte[totalBytes];
using (var rng = RandomNumberGenerator.Create())
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
rng.GetBytes(randomBytes);
}
Expand Down

0 comments on commit 5cc2723

Please sign in to comment.