Skip to content

Commit

Permalink
Merge pull request #54 from Cysharp/hadashiA/utf8-span-formattable
Browse files Browse the repository at this point in the history
Support IUtf8SpanFormattable
  • Loading branch information
hadashiA authored Sep 14, 2023
2 parents a57d785 + 3f9af09 commit 50eab7e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 25 additions & 6 deletions src/Ulid.Unity/Assets/Scripts/Ulid/Ulid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,9 @@ namespace System // wa-o, System Namespace!?
#endif
#if NET7_0_OR_GREATER
, ISpanParsable<Ulid>
#endif
#if NET8_0_OR_GREATER
, IUtf8SpanFormattable
#endif

{
Expand Down Expand Up @@ -483,7 +487,6 @@ public override string ToString()
#endif
}

#if NET6_0_OR_GREATER
//
//ISpanFormattable
//
Expand All @@ -502,28 +505,44 @@ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan
}

public string ToString(string format, IFormatProvider formatProvider) => ToString();
#endif

#if NET7_0_OR_GREATER
//
// IParsable
//
/// <inheritdoc cref="IParsable{TSelf}.Parse(string, IFormatProvider?)" />
public static Ulid Parse(string s, IFormatProvider provider) => Parse(s);

/// <inheritdoc cref="IParsable{TSelf}.TryParse(string?, IFormatProvider?, out TSelf)" />
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
//

/// <inheritdoc cref="ISpanParsable{TSelf}.Parse(ReadOnlySpan{char}, IFormatProvider?)" />
public static Ulid Parse(ReadOnlySpan<char> s, IFormatProvider provider) => Parse(s);

/// <inheritdoc cref="ISpanParsable{TSelf}.TryParse(ReadOnlySpan{char}, IFormatProvider?, out TSelf)" />
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Ulid result) => TryParse(s, out result);
#endif

//
// IUtf8SpanFormattable
//
public bool TryFormat(Span<byte> destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider provider)
{
if (TryWriteStringify(destination))
{
bytesWritten = 26;
return true;
}
bytesWritten = 0;
return false;
}

// Comparable/Equatable

Expand Down
31 changes: 25 additions & 6 deletions src/Ulid/Ulid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,9 @@ namespace System // wa-o, System Namespace!?
#endif
#if NET7_0_OR_GREATER
, ISpanParsable<Ulid>
#endif
#if NET8_0_OR_GREATER
, IUtf8SpanFormattable
#endif

{
Expand Down Expand Up @@ -483,7 +487,6 @@ public override string ToString()
#endif
}

#if NET6_0_OR_GREATER
//
//ISpanFormattable
//
Expand All @@ -502,28 +505,44 @@ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan
}

public string ToString(string format, IFormatProvider formatProvider) => ToString();
#endif

#if NET7_0_OR_GREATER
//
// IParsable
//
/// <inheritdoc cref="IParsable{TSelf}.Parse(string, IFormatProvider?)" />
public static Ulid Parse(string s, IFormatProvider provider) => Parse(s);

/// <inheritdoc cref="IParsable{TSelf}.TryParse(string?, IFormatProvider?, out TSelf)" />
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
//

/// <inheritdoc cref="ISpanParsable{TSelf}.Parse(ReadOnlySpan{char}, IFormatProvider?)" />
public static Ulid Parse(ReadOnlySpan<char> s, IFormatProvider provider) => Parse(s);

/// <inheritdoc cref="ISpanParsable{TSelf}.TryParse(ReadOnlySpan{char}, IFormatProvider?, out TSelf)" />
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Ulid result) => TryParse(s, out result);
#endif

//
// IUtf8SpanFormattable
//
public bool TryFormat(Span<byte> destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider provider)
{
if (TryWriteStringify(destination))
{
bytesWritten = 26;
return true;
}
bytesWritten = 0;
return false;
}

// Comparable/Equatable

Expand Down
2 changes: 1 addition & 1 deletion src/Ulid/Ulid.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RootNamespace>System</RootNamespace>
<SignAssembly>true</SignAssembly>
Expand Down
12 changes: 12 additions & 0 deletions tests/Ulid.Tests/UlidTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>(), null).Should().BeTrue();
bytesWritten.Should().Be(26);
result.Should().Equal(utf8Value);
}
}
}

0 comments on commit 50eab7e

Please sign in to comment.