Skip to content

Commit

Permalink
Merge pull request #40 from Cysharp/hadashiA/iformattable-iparsable
Browse files Browse the repository at this point in the history
Support System.IFormattable/IParsable etc
  • Loading branch information
neuecc committed Feb 2, 2024
2 parents ad13a7c + a769849 commit a40a091
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 46 deletions.
2 changes: 1 addition & 1 deletion sandbox/FileGenerate/SimplePrimitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace FileGenerate
{
}

[UnitOf(typeof(int), UnitGenerateOptions.Comparable | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator)]
[UnitOf(typeof(int), UnitGenerateOptions.Comparable | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.ParseMethod)]
public readonly partial struct C
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ namespace FileGenerate
[System.ComponentModel.TypeConverter(typeof(ATypeConverter))]
readonly partial struct A
: IEquatable<A>
#if NET7_0_OR_GREATER
, IFormattable
#if NET6_0_OR_GREATER
, ISpanFormattable
#endif
#if NET8_0_OR_GREATER
, IEqualityOperators<A, A, bool>
#endif
#endif
{
readonly int value;

Expand Down Expand Up @@ -73,6 +77,12 @@ public override int GetHashCode()

public override string ToString() => value.ToString();

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 @@ -15,6 +15,7 @@ namespace FileGenerate
#if NET7_0_OR_GREATER
, IEqualityOperators<Aa, Aa, bool>
#endif
, IFormattable
{
readonly int value;

Expand Down Expand Up @@ -73,6 +74,8 @@ public override int GetHashCode()

public override string ToString() => value.ToString();

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

// Default

private class AaTypeConverter : 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,14 +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>
, ISpanParsable<C>
, IAdditionOperators<C, C, C>
, ISubtractionOperators<C, C, C>
, IMultiplyOperators<C, C, C>
Expand All @@ -28,6 +29,10 @@ namespace FileGenerate
, 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 @@ -87,6 +92,96 @@ public override int GetHashCode()

public override string ToString() => value.ToString();

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)
{
return new C(int.Parse(s));
}

public static bool TryParse(string s, out C result)
{
if (int.TryParse(s, out var r))
{
result = new C(r);
return true;
}
else
{
result = default(C);
return false;
}
}

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

public static bool TryParse(string 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 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

public static C operator +(C x, C y)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace FileGenerate
#if NET7_0_OR_GREATER
, IComparisonOperators<Cc, Cc, bool>
#endif
, IFormattable
#if NET7_0_OR_GREATER
, IAdditionOperators<Cc, Cc, Cc>
, ISubtractionOperators<Cc, Cc, Cc>
Expand Down Expand Up @@ -87,6 +88,8 @@ public override int GetHashCode()

public override string ToString() => value.ToString();

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

// UnitGenerateOptions.ArithmeticOperator

public static Cc operator +(Cc x, Cc y)
Expand Down
31 changes: 31 additions & 0 deletions src/UnitGenerator/ReferenceSymbols.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using Microsoft.CodeAnalysis;

namespace UnitGenerator;

class ReferenceSymbols
{
public static ReferenceSymbols Create(Compilation compilation)
{
return new ReferenceSymbols
{
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 GuidType { get; private set; } = default!;
public INamedTypeSymbol? UlidType { 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; }
}

0 comments on commit a40a091

Please sign in to comment.