Skip to content

Commit a40a091

Browse files
authored
Merge pull request #40 from Cysharp/hadashiA/iformattable-iparsable
Support System.IFormattable/IParsable etc
2 parents ad13a7c + a769849 commit a40a091

File tree

9 files changed

+391
-46
lines changed

9 files changed

+391
-46
lines changed

sandbox/FileGenerate/SimplePrimitive.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public readonly partial struct B
1212
{
1313
}
1414

15-
[UnitOf(typeof(int), UnitGenerateOptions.Comparable | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator)]
15+
[UnitOf(typeof(int), UnitGenerateOptions.Comparable | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.ParseMethod)]
1616
public readonly partial struct C
1717
{
1818
}

sandbox/Generated/UnitGenerator/UnitGenerator.SourceGenerator/FileGenerate.A.g.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ namespace FileGenerate
1212
[System.ComponentModel.TypeConverter(typeof(ATypeConverter))]
1313
readonly partial struct A
1414
: IEquatable<A>
15-
#if NET7_0_OR_GREATER
15+
, IFormattable
16+
#if NET6_0_OR_GREATER
17+
, ISpanFormattable
18+
#endif
19+
#if NET8_0_OR_GREATER
1620
, IEqualityOperators<A, A, bool>
17-
#endif
21+
#endif
1822
{
1923
readonly int value;
2024

@@ -73,6 +77,12 @@ public override int GetHashCode()
7377

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

80+
public string ToString(string? format, IFormatProvider? formatProvider) => value.ToString(format, formatProvider);
81+
82+
#if NET6_0_OR_GREATER
83+
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
84+
((ISpanFormattable)value).TryFormat(destination, out charsWritten, format, provider);
85+
#endif
7686
// Default
7787

7888
private class ATypeConverter : System.ComponentModel.TypeConverter

sandbox/Generated/UnitGenerator/UnitGenerator.SourceGenerator/FileGenerate.Aa.g.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ readonly partial struct Aa
1515
#if NET7_0_OR_GREATER
1616
, IEqualityOperators<Aa, Aa, bool>
1717
#endif
18+
, IFormattable
1819
{
1920
readonly int value;
2021

@@ -73,6 +74,8 @@ public override int GetHashCode()
7374

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

77+
public string ToString(string? format, IFormatProvider? formatProvider) => value.ToString(format, formatProvider);
78+
7679
// Default
7780

7881
private class AaTypeConverter : System.ComponentModel.TypeConverter

sandbox/Generated/UnitGenerator/UnitGenerator.SourceGenerator/FileGenerate.B.g.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ namespace FileGenerate
1212
[System.ComponentModel.TypeConverter(typeof(BTypeConverter))]
1313
readonly partial struct B
1414
: IEquatable<B>
15-
#if NET7_0_OR_GREATER
15+
#if NET8_0_OR_GREATER
1616
, IEqualityOperators<B, B, bool>
17-
#endif
17+
#endif
1818
{
1919
readonly string value;
2020

sandbox/Generated/UnitGenerator/UnitGenerator.SourceGenerator/FileGenerate.C.g.cs

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ namespace FileGenerate
1212
[System.ComponentModel.TypeConverter(typeof(CTypeConverter))]
1313
readonly partial struct C
1414
: IEquatable<C>
15-
#if NET7_0_OR_GREATER
16-
, IEqualityOperators<C, C, bool>
17-
#endif
1815
, IComparable<C>
19-
#if NET7_0_OR_GREATER
20-
, IComparisonOperators<C, C, bool>
16+
, IFormattable
17+
#if NET6_0_OR_GREATER
18+
, ISpanFormattable
2119
#endif
2220
#if NET7_0_OR_GREATER
21+
, IComparisonOperators<C, C, bool>
22+
, IParsable<C>
23+
, ISpanParsable<C>
2324
, IAdditionOperators<C, C, C>
2425
, ISubtractionOperators<C, C, C>
2526
, IMultiplyOperators<C, C, C>
@@ -28,6 +29,10 @@ readonly partial struct C
2829
, IUnaryNegationOperators<C, C>
2930
, IIncrementOperators<C>
3031
, IDecrementOperators<C>
32+
#endif
33+
#if NET8_0_OR_GREATER
34+
, IEqualityOperators<C, C, bool>
35+
, IUtf8SpanParsable<C>
3136
#endif
3237
{
3338
readonly int value;
@@ -87,6 +92,96 @@ public override int GetHashCode()
8792

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

95+
public string ToString(string? format, IFormatProvider? formatProvider) => value.ToString(format, formatProvider);
96+
97+
#if NET6_0_OR_GREATER
98+
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
99+
((ISpanFormattable)value).TryFormat(destination, out charsWritten, format, provider);
100+
#endif
101+
// UnitGenerateOptions.ParseMethod
102+
103+
public static C Parse(string s)
104+
{
105+
return new C(int.Parse(s));
106+
}
107+
108+
public static bool TryParse(string s, out C result)
109+
{
110+
if (int.TryParse(s, out var r))
111+
{
112+
result = new C(r);
113+
return true;
114+
}
115+
else
116+
{
117+
result = default(C);
118+
return false;
119+
}
120+
}
121+
122+
#if NET7_0_OR_GREATER
123+
public static C Parse(string s, IFormatProvider? provider)
124+
{
125+
return new C(int.Parse(s, provider));
126+
}
127+
128+
public static bool TryParse(string s, IFormatProvider? provider, out C result)
129+
{
130+
if (int.TryParse(s, provider, out var r))
131+
{
132+
result = new C(r);
133+
return true;
134+
}
135+
else
136+
{
137+
result = default(C);
138+
return false;
139+
}
140+
}
141+
#endif
142+
143+
#if NET7_0_OR_GREATER
144+
public static C Parse(ReadOnlySpan<char> s, IFormatProvider? provider)
145+
{
146+
return new C(int.Parse(s, provider));
147+
}
148+
149+
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out C result)
150+
{
151+
if (int.TryParse(s, provider, out var r))
152+
{
153+
result = new C(r);
154+
return true;
155+
}
156+
else
157+
{
158+
result = default(C);
159+
return false;
160+
}
161+
}
162+
#endif
163+
164+
#if NET8_0_OR_GREATER
165+
public static C Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider)
166+
{
167+
return new C(int.Parse(utf8Text, provider));
168+
}
169+
170+
public static bool TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out C result)
171+
{
172+
if (int.TryParse(utf8Text, provider, out var r))
173+
{
174+
result = new C(r);
175+
return true;
176+
}
177+
else
178+
{
179+
result = default(C);
180+
return false;
181+
}
182+
}
183+
#endif
184+
90185
// UnitGenerateOptions.ArithmeticOperator
91186

92187
public static C operator +(C x, C y)

sandbox/Generated/UnitGenerator/UnitGenerator.SourceGenerator/FileGenerate.Cc.g.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ readonly partial struct Cc
1919
#if NET7_0_OR_GREATER
2020
, IComparisonOperators<Cc, Cc, bool>
2121
#endif
22+
, IFormattable
2223
#if NET7_0_OR_GREATER
2324
, IAdditionOperators<Cc, Cc, Cc>
2425
, ISubtractionOperators<Cc, Cc, Cc>
@@ -87,6 +88,8 @@ public override int GetHashCode()
8788

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

91+
public string ToString(string? format, IFormatProvider? formatProvider) => value.ToString(format, formatProvider);
92+
9093
// UnitGenerateOptions.ArithmeticOperator
9194

9295
public static Cc operator +(Cc x, Cc y)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using Microsoft.CodeAnalysis;
3+
4+
namespace UnitGenerator;
5+
6+
class ReferenceSymbols
7+
{
8+
public static ReferenceSymbols Create(Compilation compilation)
9+
{
10+
return new ReferenceSymbols
11+
{
12+
GuidType = compilation.GetTypeByMetadataName("System.Guid")!,
13+
UlidType = compilation.GetTypeByMetadataName("System.Ulid"),
14+
FormattableInterface = compilation.GetTypeByMetadataName("System.IFormattable")!,
15+
ParsableInterface = compilation.GetTypeByMetadataName("System.IParsable`1"),
16+
SpanFormattableInterface = compilation.GetTypeByMetadataName("System.ISpanFormattable"),
17+
SpanParsableInterface = compilation.GetTypeByMetadataName("System.ISpanParsable`1"),
18+
Utf8SpanFormattableInterface = compilation.GetTypeByMetadataName("System.IUtf8SpanFormattable"),
19+
Utf8SpanParsableInterface = compilation.GetTypeByMetadataName("System.IUtf8SpanParsable`1"),
20+
};
21+
}
22+
23+
public INamedTypeSymbol GuidType { get; private set; } = default!;
24+
public INamedTypeSymbol? UlidType { get; private set; }
25+
public INamedTypeSymbol FormattableInterface { get; private set; } = default!;
26+
public INamedTypeSymbol? ParsableInterface { get; private set; }
27+
public INamedTypeSymbol? SpanFormattableInterface { get; private set; }
28+
public INamedTypeSymbol? SpanParsableInterface { get; private set; }
29+
public INamedTypeSymbol? Utf8SpanFormattableInterface { get; private set; }
30+
public INamedTypeSymbol? Utf8SpanParsableInterface { get; private set; }
31+
}

0 commit comments

Comments
 (0)