Skip to content

Commit acddb34

Browse files
committed
fix: applying bunch of fixes
1 parent 106e3d4 commit acddb34

File tree

12 files changed

+63
-46
lines changed

12 files changed

+63
-46
lines changed

.github/workflows/publish.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ jobs:
6868
uses: actions/setup-dotnet@v4
6969
with:
7070
dotnet-version: |
71-
7.0.x
7271
8.0.x
7372
9.0.x
7473

Benchmark/Benchmark.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net7.0;net8.0;net9.0</TargetFrameworks>
5+
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<IsPublishable>False</IsPublishable>

Benchmark/Dictionary/Class.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
namespace Benchmark.Dictionary;
22

3+
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
34
public class DictonaryTestClass {
45
public String Data1 { get; set; }
56
public String Data2 { get; set; }
67
public String Data3 { get; set; }
78
}
9+
10+
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.

Library/Classes/UUIDJsonConverter/UUIDJsonConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ public partial class UUIDJsonConverter : JsonConverter<UUID> {
1212
public override UUID Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
1313
ReadOnlySpan<Byte> data = reader.ValueSpan;
1414

15-
if (data != null && data.Length == Format.UUID_STRING_LENGTH) {
16-
Vector128<Byte> d = Format.Parse(data);
17-
return new UUID(d);
15+
if (data == Span<Byte>.Empty || data.Length != Format.UUID_STRING_LENGTH) {
16+
throw new JsonException("Unknown UUID format");
1817
}
1918

20-
throw new JsonException("Unknown UUID format");
19+
Vector128<Byte> d = Format.Parse(data);
20+
return new UUID(d);
2121
}
2222

2323
/// <inheritdoc/>

Library/DaanV2.UUID.Net.csproj

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net7.0;net8.0;net9.0</TargetFrameworks>
4+
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<AssemblyName>DaanV2.UUID.Net</AssemblyName>
@@ -23,9 +23,8 @@ Complies with the RFC 4122 / RFC 9562 standard. And has version 1-8 UUIDs implem
2323
<PackageReadmeFile>README.md</PackageReadmeFile>
2424
<RepositoryUrl>https://github.com/DaanV2/DaanV2.UUID.Net</RepositoryUrl>
2525
<RepositoryType>git</RepositoryType>
26-
<PackageTags>UUID;RFC-4122;V1;V3;V4;V5;V6;V7;V8</PackageTags>
27-
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
28-
<PackageLicenseFile>LICENSE.rfc9562.txt</PackageLicenseFile>
26+
<PackageTags>UUID;RFC-4122;RFC-9562;V1;V2;V3;V4;V5;V6;V7;V8</PackageTags>
27+
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
2928
<IncludeSymbols>True</IncludeSymbols>
3029
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
3130
</PropertyGroup>
@@ -59,13 +58,13 @@ Complies with the RFC 4122 / RFC 9562 standard. And has version 1-8 UUIDs implem
5958
<Pack>True</Pack>
6059
<PackagePath>\</PackagePath>
6160
</None>
62-
<None Include="..\README.md">
6361
<None Include="..\LICENSE.rfc9562.txt">
64-
<Pack>True</Pack>
65-
<PackagePath>\</PackagePath>
62+
<Pack>True</Pack>
63+
<PackagePath>\</PackagePath>
6664
</None>
67-
<Pack>True</Pack>
68-
<PackagePath>\</PackagePath>
65+
<None Include="..\README.md">
66+
<Pack>True</Pack>
67+
<PackagePath>\</PackagePath>
6968
</None>
7069
<None Include="..\Resources\icon.png">
7170
<Pack>True</Pack>

Library/Static Classes/Format/Format - Extract.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static (UInt64 bits48, UInt16 bits12, UInt64 bits62) Extract(UUID uuid) {
1919

2020
// Remove top 2 bits, so we have 62 bits of data
2121
const UInt64 mask = 0xC000000000000000u;
22-
dataC = (dataC & ~mask);
22+
dataC &= ~mask;
2323

2424
return (dataA, dataB, dataC);
2525
}

Library/Static Classes/V1/V1 - Generate.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ internal static UUID Generate(UInt64 timestamp, UInt16 nanoSeconds, ReadOnlySpan
7575
UInt32 timeLow = (UInt32)(timestamp & 0xFFFFFFFF);
7676
UInt16 timeMid = (UInt16)((timestamp >> 32) & 0xFFFF);
7777
UInt16 timeHi = (UInt16)((timestamp >> 48) & 0x0FFF); // 12 bits for time_hi
78-
UInt16 timeHiAndVersion = (UInt16)(timeHi | ((byte)V1.Version << 12)); // Set version 1 using constant
78+
UInt16 timeHiAndVersion = (UInt16)(timeHi | ((UInt16)V1.Version << 12)); // Set version 1 using constant
7979

8080
BinaryPrimitives.WriteUInt32BigEndian(data, timeLow);
8181
BinaryPrimitives.WriteUInt16BigEndian(data[4..], timeMid);
8282
BinaryPrimitives.WriteUInt16BigEndian(data[6..], timeHiAndVersion);
8383

8484
// Split nanoSeconds (clock sequence) into 14 bits
8585
UInt16 clockSeq = (UInt16)(nanoSeconds & 0x3FFF);
86-
data[8] = (byte)(((clockSeq >> 8) & 0x3F) | (byte)V1.Variant); // Set variant using constant
87-
data[9] = (byte)(clockSeq & 0xFF);
86+
data[8] = (Byte)(((clockSeq >> 8) & 0x3F) | (Byte)V1.Variant); // Set variant using constant
87+
data[9] = (Byte)(clockSeq & 0xFF);
8888

8989
var uuid = Vector128.Create<Byte>(data);
9090
return new UUID(uuid);

Library/Static Classes/V2/V2 - Batch.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
namespace DaanV2.UUID;
44

55
public static partial class V2 {
6-
/// <inheritdoc cref="Batch(Int32, DateTime, ReadOnlySpan{Byte})"/>
6+
/// <inheritdoc cref="Batch(Int32, DateTime, ReadOnlySpan{Byte}, Byte)"/>
77

88
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
9-
public static UUID[] Batch(Int32 amount, byte domain = 0) {
9+
public static UUID[] Batch(Int32 amount, Byte domain = 0) {
1010
return Batch(amount, DateTime.UtcNow, V1.GetMacAddressBytes(), domain);
1111
}
1212

13-
/// <inheritdoc cref="Batch(Int32, DateTime, ReadOnlySpan{Byte})"/>
13+
/// <inheritdoc cref="Batch(Int32, DateTime, ReadOnlySpan{Byte}, Byte)"/>
1414

1515
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
16-
public static UUID[] Batch(Int32 amount, DateTime startTimestamp, byte domain = 0) {
16+
public static UUID[] Batch(Int32 amount, DateTime startTimestamp, Byte domain = 0) {
1717
return Batch(amount, startTimestamp, V1.GetMacAddressBytes(), domain);
1818
}
1919

20-
/// <inheritdoc cref="Batch(Int32, DateTime, ReadOnlySpan{Byte})"/>
20+
/// <inheritdoc cref="Batch(Int32, DateTime, ReadOnlySpan{Byte}, Byte)"/>
2121

2222
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
23-
public static UUID[] Batch(Int32 amount, ReadOnlySpan<Byte> macAddress, byte domain = 0) {
23+
public static UUID[] Batch(Int32 amount, ReadOnlySpan<Byte> macAddress, Byte domain = 0) {
2424
return Batch(amount, DateTime.UtcNow, macAddress, domain);
2525
}
2626

@@ -29,14 +29,15 @@ public static UUID[] Batch(Int32 amount, ReadOnlySpan<Byte> macAddress, byte dom
2929
/// <param name="amount">The amount of <see cref="UUID"/> to create</param>
3030
/// <param name="startTimestamp">The time to start at, will increment for each following <see cref="UUID"/></param>
3131
/// <param name="macAddress">The macAddress to use, excepts to be 6 bytes</param>
32+
/// <param name="domain">An byte to indicate the domain</param>
3233
/// <returns>A collection of <see cref="UUID"/></returns>
3334
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
34-
public static UUID[] Batch(Int32 amount, DateTime startTimestamp, ReadOnlySpan<Byte> macAddress, byte domain = 0) {
35+
public static UUID[] Batch(Int32 amount, DateTime startTimestamp, ReadOnlySpan<Byte> macAddress, Byte domain = 0) {
3536
var uuids = new UUID[amount];
3637
DateTime timestamp = startTimestamp;
3738
for (Int32 i = 0; i < amount; i++) {
3839
// Use i as the local identifier (UID/GID) for demonstration; domain is passed in
39-
uuids[i] = Generate(timestamp, (uint)i, macAddress, domain);
40+
uuids[i] = Generate(timestamp, (UInt32)i, macAddress, domain);
4041
// Optionally increment timestamp if you want unique timestamps per UUID
4142
if (i % UInt16.MaxValue == 0) {
4243
timestamp = timestamp.AddTicks(1);

Library/Static Classes/V2/V2 - Generate.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System.Buffers.Binary;
22
using System.Runtime.CompilerServices;
33
using System.Runtime.Intrinsics;
4-
using System.Runtime.InteropServices;
5-
using System;
64

75
namespace DaanV2.UUID;
86

@@ -20,19 +18,19 @@ public static UUID Generate() {
2018
/// Generates a Version 2 UUID with custom timestamp, UID, and MAC address.
2119
/// </summary>
2220
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
23-
public static UUID Generate(DateTime timestamp, uint localIdentifier, ReadOnlySpan<byte> macAddress, byte domain = 0) {
24-
ulong uuidTimestamp = V1.TimeStamp(timestamp);
21+
public static UUID Generate(DateTime timestamp, UInt32 localIdentifier, ReadOnlySpan<Byte> macAddress, Byte domain = 0) {
22+
UInt64 uuidTimestamp = V1.TimeStamp(timestamp);
2523

26-
Span<byte> data = stackalloc byte[16];
24+
Span<Byte> data = stackalloc Byte[16];
2725

2826
// Write node (MAC address)
2927
macAddress.CopyTo(data[10..]);
3028

3129
// Split timestamp
32-
uint timeLow = (uint)(uuidTimestamp & 0xFFFFFFFF);
33-
ushort timeMid = (ushort)((uuidTimestamp >> 32) & 0xFFFF);
34-
ushort timeHi = (ushort)((uuidTimestamp >> 48) & 0x0FFF); // 12 bits for time_hi
35-
ushort timeHiAndVersion = (ushort)(timeHi | ((ushort)Version.V2)); // Set version 2
30+
UInt32 timeLow = (UInt32)(uuidTimestamp & 0xFFFFFFFF);
31+
UInt16 timeMid = (UInt16)((uuidTimestamp >> 32) & 0xFFFF);
32+
UInt16 timeHi = (UInt16)((uuidTimestamp >> 48) & 0x0FFF); // 12 bits for time_hi
33+
UInt16 timeHiAndVersion = (UInt16)(timeHi | ((UInt16)Version.V2)); // Set version 2
3634

3735
BinaryPrimitives.WriteUInt32BigEndian(data, timeLow);
3836
BinaryPrimitives.WriteUInt16BigEndian(data[4..], timeMid);
@@ -41,7 +39,7 @@ public static UUID Generate(DateTime timestamp, uint localIdentifier, ReadOnlySp
4139
// Per RFC 4122 DCE Security UUID:
4240
// data[8]: high 2 bits = variant, low 6 bits = high 6 bits of local identifier
4341
// data[9]: domain (per spec), or low 8 bits of local identifier if domain not used
44-
data[8] = (byte)(((localIdentifier >> 8) & 0x3F) | (byte)Variant.V1);
42+
data[8] = (Byte)(((localIdentifier >> 8) & 0x3F) | (Byte)Variant.V1);
4543
data[9] = domain; // Store domain in data[9]
4644

4745
var uuid = Vector128.Create<Byte>(data);
@@ -51,10 +49,11 @@ public static UUID Generate(DateTime timestamp, uint localIdentifier, ReadOnlySp
5149
/// <summary>
5250
/// Gets the current user's UID (Linux only). Returns 0 if not available.
5351
/// </summary>
54-
private static uint GetCurrentUid() {
52+
private static UInt32 GetCurrentUid() {
5553
try {
56-
return (uint)System.Convert.ToInt32(Environment.GetEnvironmentVariable("UID") ?? "0");
57-
} catch {
54+
return (UInt32)System.Convert.ToInt32(Environment.GetEnvironmentVariable("UID") ?? "0");
55+
}
56+
catch {
5857
return 0;
5958
}
6059
}
@@ -65,11 +64,11 @@ private static uint GetCurrentUid() {
6564
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
6665
public static UUID Generate(ReadOnlySpan<Byte> source) {
6766
// Not standard for V2, but for API compatibility: hash and overlay version/variant
68-
Span<byte> data = stackalloc byte[16];
67+
Span<Byte> data = stackalloc Byte[16];
6968
System.Security.Cryptography.MD5.HashData(source, data);
7069
// Set version and variant using V2.Version and V2.Variant constants
71-
data[6] = (byte)((data[6] & 0x0F) | (byte)Version.V2);
72-
data[8] = (byte)((data[8] & 0x3F) | (byte)Variant.V1);
70+
data[6] = (Byte)((data[6] & 0x0F) | (Byte)Version.V2);
71+
data[8] = (Byte)((data[8] & 0x3F) | (Byte)Variant.V1);
7372

7473
var uuid = Vector128.Create<Byte>(data);
7574
return new UUID(uuid);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Net.NetworkInformation;
2+
3+
namespace DaanV2.UUID;
4+
5+
public static partial class V2 {
6+
7+
/// <inheritdoc cref="V1.GetMacAddress"/>
8+
public static PhysicalAddress? GetMacAddress() {
9+
return V1.GetMacAddress();
10+
}
11+
12+
/// <inheritdoc cref="V1.GetMacAddressBytes"/>
13+
public static Byte[] GetMacAddressBytes() {
14+
return V1.GetMacAddressBytes();
15+
}
16+
}

0 commit comments

Comments
 (0)