diff --git a/.github/workflows/build-debug.yml b/.github/workflows/build-debug.yml index 4aa77e2..b98510c 100644 --- a/.github/workflows/build-debug.yml +++ b/.github/workflows/build-debug.yml @@ -19,6 +19,7 @@ jobs: dotnet-version: | 6.0.x 7.0.x + 8.0.x - run: dotnet build -c Debug - run: dotnet test -c Debug --no-build - run: dotnet test -c Debug --no-build --environment "COMPlus_EnableHWIntrinsic=0;COMPlus_EnableSSE2=0" diff --git a/src/Ulid.Unity/Assets/Scripts/Ulid/Ulid.cs b/src/Ulid.Unity/Assets/Scripts/Ulid/Ulid.cs index 05d34fb..9711b57 100644 --- a/src/Ulid.Unity/Assets/Scripts/Ulid/Ulid.cs +++ b/src/Ulid.Unity/Assets/Scripts/Ulid/Ulid.cs @@ -5,6 +5,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Serialization; +using System.Diagnostics.CodeAnalysis; using System.Text; #if NET6_0_OR_GREATER using System.Diagnostics.CodeAnalysis; @@ -30,6 +31,9 @@ namespace System // wa-o, System Namespace!? #endif #if NET7_0_OR_GREATER , ISpanParsable +#endif +#if NET8_0_OR_GREATER +, IUtf8SpanFormattable #endif { @@ -483,7 +487,6 @@ public override string ToString() #endif } -#if NET6_0_OR_GREATER // //ISpanFormattable // @@ -502,9 +505,7 @@ public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan } public string ToString(string format, IFormatProvider formatProvider) => ToString(); -#endif -#if NET7_0_OR_GREATER // // IParsable // @@ -512,18 +513,36 @@ public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan public static Ulid Parse(string s, IFormatProvider provider) => Parse(s); /// - public static bool TryParse([NotNullWhen(true)] string s, IFormatProvider provider, out Ulid result) => TryParse(s, out result); + public static bool TryParse( +#if NETSTANDARD2_1_OR_GREATER + [NotNullWhen(true)] string s, +#else + string s, +#endif + IFormatProvider provider, out Ulid result) => TryParse(s, out result); // // ISpanParsable // - /// public static Ulid Parse(ReadOnlySpan s, IFormatProvider provider) => Parse(s); /// public static bool TryParse(ReadOnlySpan s, IFormatProvider provider, out Ulid result) => TryParse(s, out result); -#endif + + // + // IUtf8SpanFormattable + // + public bool TryFormat(Span destination, out int bytesWritten, ReadOnlySpan format, IFormatProvider provider) + { + if (TryWriteStringify(destination)) + { + bytesWritten = 26; + return true; + } + bytesWritten = 0; + return false; + } // Comparable/Equatable diff --git a/src/Ulid/Ulid.cs b/src/Ulid/Ulid.cs index 05d34fb..9711b57 100644 --- a/src/Ulid/Ulid.cs +++ b/src/Ulid/Ulid.cs @@ -5,6 +5,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Serialization; +using System.Diagnostics.CodeAnalysis; using System.Text; #if NET6_0_OR_GREATER using System.Diagnostics.CodeAnalysis; @@ -30,6 +31,9 @@ namespace System // wa-o, System Namespace!? #endif #if NET7_0_OR_GREATER , ISpanParsable +#endif +#if NET8_0_OR_GREATER +, IUtf8SpanFormattable #endif { @@ -483,7 +487,6 @@ public override string ToString() #endif } -#if NET6_0_OR_GREATER // //ISpanFormattable // @@ -502,9 +505,7 @@ public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan } public string ToString(string format, IFormatProvider formatProvider) => ToString(); -#endif -#if NET7_0_OR_GREATER // // IParsable // @@ -512,18 +513,36 @@ public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan public static Ulid Parse(string s, IFormatProvider provider) => Parse(s); /// - public static bool TryParse([NotNullWhen(true)] string s, IFormatProvider provider, out Ulid result) => TryParse(s, out result); + public static bool TryParse( +#if NETSTANDARD2_1_OR_GREATER + [NotNullWhen(true)] string s, +#else + string s, +#endif + IFormatProvider provider, out Ulid result) => TryParse(s, out result); // // ISpanParsable // - /// public static Ulid Parse(ReadOnlySpan s, IFormatProvider provider) => Parse(s); /// public static bool TryParse(ReadOnlySpan s, IFormatProvider provider, out Ulid result) => TryParse(s, out result); -#endif + + // + // IUtf8SpanFormattable + // + public bool TryFormat(Span destination, out int bytesWritten, ReadOnlySpan format, IFormatProvider provider) + { + if (TryWriteStringify(destination)) + { + bytesWritten = 26; + return true; + } + bytesWritten = 0; + return false; + } // Comparable/Equatable diff --git a/src/Ulid/Ulid.csproj b/src/Ulid/Ulid.csproj index 32fa56b..cbe8ab1 100644 --- a/src/Ulid/Ulid.csproj +++ b/src/Ulid/Ulid.csproj @@ -1,7 +1,7 @@  - netstandard2.0;netstandard2.1;net6.0;net7.0 + netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0 true System true diff --git a/tests/Ulid.Tests/UlidTest.cs b/tests/Ulid.Tests/UlidTest.cs index e3a4a92..efad835 100644 --- a/tests/Ulid.Tests/UlidTest.cs +++ b/tests/Ulid.Tests/UlidTest.cs @@ -190,6 +190,18 @@ public void ISpanParsable() } } #endif + + [Fact] + public void TryFormatUtf8Bytes() + { + var value = Ulid.NewUlid(); + var utf8Value = System.Text.Encoding.UTF8.GetBytes(value.ToString()); + var result = new byte[26]; + value.TryFormat(result, out var bytesWritten, Array.Empty(), null).Should().BeTrue(); + bytesWritten.Should().Be(26); + result.Should().Equal(utf8Value); + } } } +