Skip to content

Commit

Permalink
Add ISpanParsable<TSelf> implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kzu committed Nov 26, 2024
1 parent b713d87 commit d2cbcbd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/StructId/Templates/Parsable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,32 @@
using System.Diagnostics.CodeAnalysis;
using System;

readonly partial record struct Self : IParsable<Self>
readonly partial record struct Self : ISpanParsable<Self>
{
/// <inheritdoc cref="IParsable{TSelf}"/>
public static Self Parse(string s, IFormatProvider? provider)
=> s is null ? throw new ArgumentNullException(nameof(s)) : new(s);

/// <inheritdoc cref="IParsable{TSelf}"/>
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out Self result)
{
if (s is not null)
{
result = new Self(s);
result = new(s);
return true;
}
result = default;
return false;
}

/// <inheritdoc cref="ISpanParsable{TSelf}"/>
public static Self Parse(ReadOnlySpan<char> input, global::System.IFormatProvider? provider)
=> new(input.ToString());

/// <inheritdoc cref="ISpanParsable{TSelf}"/>
public static bool TryParse(ReadOnlySpan<char> input, IFormatProvider? provider, out Self result)
{
result = new(input.ToString());
return true;
}
}
19 changes: 18 additions & 1 deletion src/StructId/Templates/ParsableT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
using System.Diagnostics.CodeAnalysis;
using System;

readonly partial record struct TSelf : IParsable<TSelf>
readonly partial record struct TSelf : ISpanParsable<TSelf>
{
/// <inheritdoc cref="IParsable{TSelf}"/>
public static TSelf Parse(string s, IFormatProvider? provider) => new(TId.Parse(s, provider));

/// <inheritdoc cref="IParsable{TSelf}"/>
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out TSelf result)
{
if (TId.TryParse(s, provider, out var value))
Expand All @@ -18,4 +20,19 @@ public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? prov
result = default;
return false;
}

/// <inheritdoc cref="ISpanParsable{TSelf}"/>
public static TSelf Parse(ReadOnlySpan<char> input, global::System.IFormatProvider? provider) => new(TId.Parse(input, provider));

/// <inheritdoc cref="ISpanParsable{TSelf}"/>
public static bool TryParse(ReadOnlySpan<char> input, IFormatProvider? provider, out TSelf result)
{
if (TId.TryParse(input, provider, out var value))
{
result = new TSelf(value);
return true;
}
result = default;
return false;
}
}
7 changes: 6 additions & 1 deletion src/StructId/Templates/TSelf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
using System.Diagnostics.CodeAnalysis;
using StructId;

partial record struct TId : IParsable<TId>, IComparable<TId>
partial record struct TId : ISpanParsable<TId>, IComparable<TId>
{
public static TId Parse(string s, IFormatProvider? provider)
=> s is null ? throw new ArgumentNullException(nameof(s)) : new TId();

public static TId Parse(ReadOnlySpan<char> s, IFormatProvider? provider) => new();

public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out TId result)
{
if (s is not null)
Expand All @@ -18,6 +20,9 @@ public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? prov
return false;
}

public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, [MaybeNullWhen(false)] out TId result)
=> TryParse(s.ToString(), provider, out result);

public int CompareTo(TId other) => other.CompareTo(this);
}

Expand Down

0 comments on commit d2cbcbd

Please sign in to comment.