Skip to content

Commit

Permalink
Support ISpanFormattable,ISpanParsable,IUtf8SpanFormattable,IUtf8Span…
Browse files Browse the repository at this point in the history
…Parsable
  • Loading branch information
hadashiA committed Feb 2, 2024
1 parent 1ceec5c commit a769849
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ namespace FileGenerate
[System.ComponentModel.TypeConverter(typeof(ATypeConverter))]
readonly partial struct A
: IEquatable<A>
#if NET7_0_OR_GREATER
, IEqualityOperators<A, A, bool>
#endif
, IFormattable
#if NET6_0_OR_GREATER
, ISpanFormattable
#endif
#if NET8_0_OR_GREATER
, IEqualityOperators<A, A, bool>
#endif
{
readonly int value;

Expand Down Expand Up @@ -76,6 +79,10 @@ public override int GetHashCode()

public string ToString(string? format, IFormatProvider? formatProvider) => value.ToString(format, formatProvider);

#if NET6_0_OR_GREATER
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
((ISpanFormattable)value).TryFormat(destination, out charsWritten, format, provider);
#endif
// Default

private class ATypeConverter : System.ComponentModel.TypeConverter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace FileGenerate
[System.ComponentModel.TypeConverter(typeof(BTypeConverter))]
readonly partial struct B
: IEquatable<B>
#if NET7_0_OR_GREATER
#if NET8_0_OR_GREATER
, IEqualityOperators<B, B, bool>
#endif
#endif
{
readonly string value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ namespace FileGenerate
[System.ComponentModel.TypeConverter(typeof(CTypeConverter))]
readonly partial struct C
: IEquatable<C>
#if NET7_0_OR_GREATER
, IEqualityOperators<C, C, bool>
#endif
, IComparable<C>
#if NET7_0_OR_GREATER
, IComparisonOperators<C, C, bool>
, IFormattable
#if NET6_0_OR_GREATER
, ISpanFormattable
#endif
#if NET7_0_OR_GREATER
, IComparisonOperators<C, C, bool>
, IParsable<C>
#endif
, IFormattable
#if NET7_0_OR_GREATER
, ISpanParsable<C>
, IAdditionOperators<C, C, C>
, ISubtractionOperators<C, C, C>
, IMultiplyOperators<C, C, C>
Expand All @@ -32,6 +29,10 @@ readonly partial struct C
, IUnaryNegationOperators<C, C>
, IIncrementOperators<C>
, IDecrementOperators<C>
#endif
#if NET8_0_OR_GREATER
, IEqualityOperators<C, C, bool>
, IUtf8SpanParsable<C>
#endif
{
readonly int value;
Expand Down Expand Up @@ -93,6 +94,10 @@ public override int GetHashCode()

public string ToString(string? format, IFormatProvider? formatProvider) => value.ToString(format, formatProvider);

#if NET6_0_OR_GREATER
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
((ISpanFormattable)value).TryFormat(destination, out charsWritten, format, provider);
#endif
// UnitGenerateOptions.ParseMethod

public static C Parse(string s)
Expand All @@ -114,14 +119,15 @@ public static bool TryParse(string s, out C result)
}
}

#if NET7_0_OR_GREATER
public static C Parse(string s, IFormatProvider? provider)
{
return new C(int.Parse(s));
return new C(int.Parse(s, provider));
}

public static bool TryParse(string s, IFormatProvider? provider, out C result)
{
if (int.TryParse(s, out var r))
if (int.TryParse(s, provider, out var r))
{
result = new C(r);
return true;
Expand All @@ -132,6 +138,49 @@ public static bool TryParse(string s, IFormatProvider? provider, out C result)
return false;
}
}
#endif

#if NET7_0_OR_GREATER
public static C Parse(ReadOnlySpan<char> s, IFormatProvider? provider)
{
return new C(int.Parse(s, provider));
}

public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out C result)
{
if (int.TryParse(s, provider, out var r))
{
result = new C(r);
return true;
}
else
{
result = default(C);
return false;
}
}
#endif

#if NET8_0_OR_GREATER
public static C Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider)
{
return new C(int.Parse(utf8Text, provider));
}

public static bool TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out C result)
{
if (int.TryParse(utf8Text, provider, out var r))
{
result = new C(r);
return true;
}
else
{
result = default(C);
return false;
}
}
#endif

// UnitGenerateOptions.ArithmeticOperator

Expand Down
20 changes: 14 additions & 6 deletions src/UnitGenerator/ReferenceSymbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@ public static ReferenceSymbols Create(Compilation compilation)
{
return new ReferenceSymbols
{
IParsableInterface = compilation.GetTypeByMetadataName("System.IParsable`1")!,
IFormattableInterface = compilation.GetTypeByMetadataName("System.IFormattable")!,
GuidType = compilation.GetTypeByMetadataName("System.Guid"),
GuidType = compilation.GetTypeByMetadataName("System.Guid")!,
UlidType = compilation.GetTypeByMetadataName("System.Ulid"),
FormattableInterface = compilation.GetTypeByMetadataName("System.IFormattable")!,
ParsableInterface = compilation.GetTypeByMetadataName("System.IParsable`1"),
SpanFormattableInterface = compilation.GetTypeByMetadataName("System.ISpanFormattable"),
SpanParsableInterface = compilation.GetTypeByMetadataName("System.ISpanParsable`1"),
Utf8SpanFormattableInterface = compilation.GetTypeByMetadataName("System.IUtf8SpanFormattable"),
Utf8SpanParsableInterface = compilation.GetTypeByMetadataName("System.IUtf8SpanParsable`1"),
};
}

public INamedTypeSymbol IParsableInterface { get; private set; } = default!;
public INamedTypeSymbol IFormattableInterface { get; private set; } = default!;
public INamedTypeSymbol GuidType { get; private set; } = default!;
public INamedTypeSymbol? UlidType { get; private set; }
public INamedTypeSymbol? GuidType { get; private set; }
public INamedTypeSymbol FormattableInterface { get; private set; } = default!;
public INamedTypeSymbol? ParsableInterface { get; private set; }
public INamedTypeSymbol? SpanFormattableInterface { get; private set; }
public INamedTypeSymbol? SpanParsableInterface { get; private set; }
public INamedTypeSymbol? Utf8SpanFormattableInterface { get; private set; }
public INamedTypeSymbol? Utf8SpanParsableInterface { get; private set; }
}
Loading

0 comments on commit a769849

Please sign in to comment.