Skip to content

Commit a769849

Browse files
committed
Support ISpanFormattable,ISpanParsable,IUtf8SpanFormattable,IUtf8SpanParsable
1 parent 1ceec5c commit a769849

File tree

5 files changed

+283
-98
lines changed

5 files changed

+283
-98
lines changed

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

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

@@ -76,6 +79,10 @@ public override int GetHashCode()
7679

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

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
7986
// Default
8087

8188
private class ATypeConverter : 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: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +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>
2322
, IParsable<C>
24-
#endif
25-
, IFormattable
26-
#if NET7_0_OR_GREATER
23+
, ISpanParsable<C>
2724
, IAdditionOperators<C, C, C>
2825
, ISubtractionOperators<C, C, C>
2926
, IMultiplyOperators<C, C, C>
@@ -32,6 +29,10 @@ readonly partial struct C
3229
, IUnaryNegationOperators<C, C>
3330
, IIncrementOperators<C>
3431
, IDecrementOperators<C>
32+
#endif
33+
#if NET8_0_OR_GREATER
34+
, IEqualityOperators<C, C, bool>
35+
, IUtf8SpanParsable<C>
3536
#endif
3637
{
3738
readonly int value;
@@ -93,6 +94,10 @@ public override int GetHashCode()
9394

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

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
96101
// UnitGenerateOptions.ParseMethod
97102

98103
public static C Parse(string s)
@@ -114,14 +119,15 @@ public static bool TryParse(string s, out C result)
114119
}
115120
}
116121

122+
#if NET7_0_OR_GREATER
117123
public static C Parse(string s, IFormatProvider? provider)
118124
{
119-
return new C(int.Parse(s));
125+
return new C(int.Parse(s, provider));
120126
}
121127

122128
public static bool TryParse(string s, IFormatProvider? provider, out C result)
123129
{
124-
if (int.TryParse(s, out var r))
130+
if (int.TryParse(s, provider, out var r))
125131
{
126132
result = new C(r);
127133
return true;
@@ -132,6 +138,49 @@ public static bool TryParse(string s, IFormatProvider? provider, out C result)
132138
return false;
133139
}
134140
}
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
135184

136185
// UnitGenerateOptions.ArithmeticOperator
137186

src/UnitGenerator/ReferenceSymbols.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@ public static ReferenceSymbols Create(Compilation compilation)
99
{
1010
return new ReferenceSymbols
1111
{
12-
IParsableInterface = compilation.GetTypeByMetadataName("System.IParsable`1")!,
13-
IFormattableInterface = compilation.GetTypeByMetadataName("System.IFormattable")!,
14-
GuidType = compilation.GetTypeByMetadataName("System.Guid"),
12+
GuidType = compilation.GetTypeByMetadataName("System.Guid")!,
1513
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"),
1620
};
1721
}
1822

19-
public INamedTypeSymbol IParsableInterface { get; private set; } = default!;
20-
public INamedTypeSymbol IFormattableInterface { get; private set; } = default!;
23+
public INamedTypeSymbol GuidType { get; private set; } = default!;
2124
public INamedTypeSymbol? UlidType { get; private set; }
22-
public INamedTypeSymbol? GuidType { 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; }
2331
}

0 commit comments

Comments
 (0)