Skip to content

Commit c320d0d

Browse files
authored
Merge pull request #74 from dameng324/master
add `TryParseFast` and fix `<see cref=`
2 parents dd00eb7 + 99ba736 commit c320d0d

File tree

5 files changed

+86
-24
lines changed

5 files changed

+86
-24
lines changed

Supernova.Enum.Generators/EnumSourceGenerator.cs

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public sealed class {SourceGeneratorHelper.AttributeName}Attribute : Attribute
113113
namespace {SourceGeneratorHelper.NameSpace}
114114
{{
115115
/// <summary>
116-
/// Provides extension methods for operations related to the {symbol.Name} enumeration.
116+
/// Provides extension methods for operations related to the <see cref=""global::{symbol.FullName()}"" /> enumeration.
117117
/// </summary>
118118
{symbol.DeclaredAccessibility.ToString("G").ToLower()} static class {symbol.Name}EnumExtensions
119119
{{");
@@ -147,6 +147,10 @@ namespace {SourceGeneratorHelper.NameSpace}
147147

148148
//GetLength
149149
GetLengthFast(sourceBuilder, symbol, e);
150+
151+
152+
//TryParse
153+
TryParseFast(sourceBuilder, e, symbol);
150154

151155
sourceBuilder.Append(@"
152156
}
@@ -163,11 +167,11 @@ private static void ToDisplay(StringBuilder sourceBuilder, ISymbol symbol, EnumD
163167
{
164168
sourceBuilder.Append($@"
165169
/// <summary>
166-
/// Converts the <see cref=""{symbol.Name}"" /> enumeration value to its display string.
170+
/// Converts the <see cref=""global::{symbol.FullName()}"" /> enumeration value to its display string.
167171
/// </summary>
168-
/// <param name=""states"">The <see cref=""{symbol.Name}"" /> enumeration value.</param>
172+
/// <param name=""states"">The <see cref=""global::{symbol.FullName()}"" /> enumeration value.</param>
169173
/// <param name=""defaultValue"">The default value to return if the enumeration value is not recognized.</param>
170-
/// <returns>The display string of the <see cref=""{symbol.Name}"" /> value.</returns>
174+
/// <returns>The display string of the <see cref=""global::{symbol.FullName()}"" /> value.</returns>
171175
public static string {SourceGeneratorHelper.ExtensionMethodNameToDisplay}(this {symbol.FullName()} states, string defaultValue = null)
172176
{{
173177
return states switch
@@ -195,11 +199,11 @@ private static void ToDescription(StringBuilder sourceBuilder, ISymbol symbol, E
195199
{
196200
sourceBuilder.Append($@"
197201
/// <summary>
198-
/// Gets the description of the <see cref=""{symbol.Name}"" /> enumeration value.
202+
/// Gets the description of the <see cref=""global::{symbol.FullName()}"" /> enumeration value.
199203
/// </summary>
200-
/// <param name=""states"">The <see cref=""{symbol.Name}"" /> enumeration value.</param>
204+
/// <param name=""states"">The <see cref=""global::{symbol.FullName()}"" /> enumeration value.</param>
201205
/// <param name=""defaultValue"">The default value to return if the enumeration value is not recognized.</param>
202-
/// <returns>The description of the <see cref=""{symbol.Name}"" /> value.</returns>
206+
/// <returns>The description of the <see cref=""global::{symbol.FullName()}"" /> value.</returns>
203207
public static string {SourceGeneratorHelper.ExtensionMethodNameToDescription}(this {symbol.FullName()} states, string defaultValue = null)
204208
{{
205209
return states switch
@@ -226,10 +230,10 @@ private static void IsDefinedString(StringBuilder sourceBuilder, EnumDeclaration
226230
{
227231
sourceBuilder.Append($@"
228232
/// <summary>
229-
/// Checks if the specified string represents a defined <see cref=""{symbol.Name}"" /> value.
233+
/// Checks if the specified string represents a defined <see cref=""global::{symbol.FullName()}"" /> value.
230234
/// </summary>
231-
/// <param name=""states"">The string representing a <see cref=""{symbol.Name}"" /> value.</param>
232-
/// <returns>True if the string represents a defined <see cref=""{symbol.Name}"" /> value; otherwise, false.</returns>
235+
/// <param name=""states"">The string representing a <see cref=""global::{symbol.FullName()}"" /> value.</param>
236+
/// <returns>True if the string represents a defined <see cref=""global::{symbol.FullName()}"" /> value; otherwise, false.</returns>
233237
public static bool {SourceGeneratorHelper.ExtensionMethodNameIsDefined}(string states)
234238
{{
235239
return states switch
@@ -248,10 +252,10 @@ private static void IsDefinedEnum(StringBuilder sourceBuilder, ISymbol symbol, E
248252
{
249253
sourceBuilder.Append($@"
250254
/// <summary>
251-
/// Checks if the specified <see cref=""{symbol.Name}"" /> value is defined.
255+
/// Checks if the specified <see cref=""global::{symbol.FullName()}"" /> value is defined.
252256
/// </summary>
253-
/// <param name=""states"">The <see cref=""{symbol.Name}"" /> value to check.</param>
254-
/// <returns>True if the <see cref=""{symbol.Name}"" /> value is defined; otherwise, false.</returns>
257+
/// <param name=""states"">The <see cref=""global::{symbol.FullName()}"" /> value to check.</param>
258+
/// <returns>True if the <see cref=""global::{symbol.FullName()}"" /> value is defined; otherwise, false.</returns>
255259
public static bool {SourceGeneratorHelper.ExtensionMethodNameIsDefined}({symbol.FullName()} states)
256260
{{
257261
return states switch
@@ -270,11 +274,11 @@ private static void ToStringFast(StringBuilder sourceBuilder, ISymbol symbol, En
270274
{
271275
sourceBuilder.Append($@"
272276
/// <summary>
273-
/// Converts the <see cref=""{symbol.Name}"" /> enumeration value to its string representation.
277+
/// Converts the <see cref=""global::{symbol.FullName()}"" /> enumeration value to its string representation.
274278
/// </summary>
275-
/// <param name=""states"">The <see cref=""{symbol.Name}"" /> enumeration value.</param>
279+
/// <param name=""states"">The <see cref=""global::{symbol.FullName()}"" /> enumeration value.</param>
276280
/// <param name=""defaultValue"">The default value to return if the enumeration value is not recognized.</param>
277-
/// <returns>The string representation of the <see cref=""{symbol.Name}"" /> value.</returns>
281+
/// <returns>The string representation of the <see cref=""global::{symbol.FullName()}"" /> value.</returns>
278282
public static string {SourceGeneratorHelper.ExtensionMethodNameToString}(this {symbol.FullName()} states, string defaultValue = null)
279283
{{
280284
return states switch
@@ -294,7 +298,7 @@ private static void DisplayNamesDictionary(StringBuilder sourceBuilder, ISymbol
294298
{
295299
sourceBuilder.Append($@"
296300
/// <summary>
297-
/// Provides a dictionary that maps <see cref=""{symbol.Name}"" /> values to their corresponding display names.
301+
/// Provides a dictionary that maps <see cref=""global::{symbol.FullName()}"" /> values to their corresponding display names.
298302
/// </summary>
299303
public static readonly ImmutableDictionary<{symbol.FullName()}, string> {SourceGeneratorHelper.PropertyDisplayNamesDictionary} = new Dictionary<{symbol.FullName()}, string>
300304
{{
@@ -319,7 +323,7 @@ private static void DisplayDescriptionsDictionary(StringBuilder sourceBuilder, I
319323
{
320324
sourceBuilder.Append($@"
321325
/// <summary>
322-
/// Provides a dictionary that maps <see cref=""{symbol.Name}"" /> values to their corresponding descriptions.
326+
/// Provides a dictionary that maps <see cref=""global::{symbol.FullName()}"" /> values to their corresponding descriptions.
323327
/// </summary>
324328
public static readonly ImmutableDictionary<{symbol.FullName()}, string> {SourceGeneratorHelper.PropertyDisplayDescriptionsDictionary} = new Dictionary<{symbol.FullName()}, string>
325329
{{
@@ -343,9 +347,9 @@ private static void GetValuesFast(StringBuilder sourceBuilder, ISymbol symbol, E
343347
{
344348
sourceBuilder.Append($@"
345349
/// <summary>
346-
/// Retrieves an array of all <see cref=""{symbol.Name}"" /> enumeration values.
350+
/// Retrieves an array of all <see cref=""global::{symbol.FullName()}"" /> enumeration values.
347351
/// </summary>
348-
/// <returns>An array containing all <see cref=""{symbol.Name}"" /> enumeration values.</returns>
352+
/// <returns>An array containing all <see cref=""global::{symbol.FullName()}"" /> enumeration values.</returns>
349353
public static {symbol.FullName()}[] {SourceGeneratorHelper.ExtensionMethodNameGetValues}()
350354
{{
351355
return new[]
@@ -363,9 +367,9 @@ private static void GetNamesFast(StringBuilder sourceBuilder, ISymbol symbol, En
363367
{
364368
sourceBuilder.Append($@"
365369
/// <summary>
366-
/// Retrieves an array of strings containing the names of all <see cref=""{symbol.Name}"" /> enumeration values.
370+
/// Retrieves an array of strings containing the names of all <see cref=""global::{symbol.FullName()}"" /> enumeration values.
367371
/// </summary>
368-
/// <returns>An array of strings containing the names of all <see cref=""{symbol.Name}"" /> enumeration values.</returns>
372+
/// <returns>An array of strings containing the names of all <see cref=""global::{symbol.FullName()}"" /> enumeration values.</returns>
369373
public static string[] {SourceGeneratorHelper.ExtensionMethodNameGetNames}()
370374
{{
371375
return new[]
@@ -383,9 +387,9 @@ private static void GetLengthFast(StringBuilder sourceBuilder, ISymbol symbol, E
383387
{
384388
sourceBuilder.Append($@"
385389
/// <summary>
386-
/// Gets the length of the <see cref=""{symbol.Name}"" /> enumeration.
390+
/// Gets the length of the <see cref=""global::{symbol.FullName()}"" /> enumeration.
387391
/// </summary>
388-
/// <returns>The length of the <see cref=""{symbol.Name}"" /> enumeration.</returns>
392+
/// <returns>The length of the <see cref=""global::{symbol.FullName()}"" /> enumeration.</returns>
389393
public static int {SourceGeneratorHelper.ExtensionMethodNameGetLength}()
390394
{{
391395
return {e.Members.Count};
@@ -395,4 +399,39 @@ private static void GetLengthFast(StringBuilder sourceBuilder, ISymbol symbol, E
395399
}
396400
");
397401
}
402+
403+
private static void TryParseFast(StringBuilder sourceBuilder, EnumDeclarationSyntax e, ISymbol symbol)
404+
{
405+
sourceBuilder.Append($@"
406+
/// <summary>
407+
/// Try parse a string to <see cref=""global::{symbol.FullName()}"" /> value.
408+
/// </summary>
409+
/// <param name=""states"">The string representing a <see cref=""global::{symbol.FullName()}"" /> value.</param>
410+
/// <param name=""result"">The enum <see cref=""global::{symbol.FullName()}"" /> parse result.</param>
411+
/// <returns>True if the string is parsed successfully; otherwise, false.</returns>
412+
public static bool {SourceGeneratorHelper.ExtensionMethodNameTryParse}(string states, out {symbol.FullName()} result)
413+
{{
414+
switch (states)
415+
{{
416+
");
417+
foreach (var member in e.Members.Select(x => x.Identifier.ValueText))
418+
sourceBuilder.AppendLine(
419+
$$"""
420+
case "{{member}}":
421+
{
422+
result = {{symbol.FullName()}}.{{member}};
423+
return true;
424+
}
425+
""");
426+
sourceBuilder.Append(
427+
"""
428+
default: {
429+
result = default;
430+
return false;
431+
}
432+
}
433+
}
434+
"""
435+
);
436+
}
398437
}

Supernova.Enum.Generators/SourceGeneratorHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public static class SourceGeneratorHelper
1111
public const string ExtensionMethodNameGetValues = "GetValuesFast";
1212
public const string ExtensionMethodNameGetNames = "GetNamesFast";
1313
public const string ExtensionMethodNameGetLength = "GetLengthFast";
14+
public const string ExtensionMethodNameTryParse = "TryParseFast";
1415
public const string PropertyDisplayNamesDictionary = "DisplayNamesDictionary";
1516
public const string PropertyDisplayDescriptionsDictionary = "DisplayDescriptionsDictionary";
1617

Supernova.Enum.Generators/Supernova.Enum.Generators.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
fix doc warning
1818
add GeneratedCodeAttribute attrbiute
1919
</PackageReleaseNotes>
20+
<LangVersion>latest</LangVersion>
2021
</PropertyGroup>
2122

2223
<ItemGroup>

test/Console.Test.Benchmark/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ public int FastGetLength()
144144
{
145145
return UserTypeEnumExtensions.GetLengthFast();
146146
}
147+
[Benchmark]
148+
public bool NativeTryParse()
149+
{
150+
return Enum.TryParse<UserType>("Men", out _);
151+
}
152+
[Benchmark]
153+
public bool FastTryParse()
154+
{
155+
return UserTypeEnumExtensions.TryParseFast("Men",out _);
156+
}
147157
}
148158

149159
public static class Ext

test/UnitTests/EnumGeneratorTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,16 @@ public void TestEnumGetLength()
177177
Assert.AreEqual(Enum.GetValues<UserTypeTest>().Length, length);
178178
}
179179

180+
[TestMethod]
181+
public void TestEnumTryParse()
182+
{
183+
var menString = "Men";
184+
bool result = UserTypeTestEnumExtensions.TryParseFast(menString, out var enumValue);
185+
186+
Assert.IsTrue(result);
187+
Assert.AreEqual(enumValue,UserTypeTest.Men);
188+
}
189+
190+
180191
private UserTypeTest GetUndefinedEnumValue() => (UserTypeTest)(-1);
181192
}

0 commit comments

Comments
 (0)