diff --git a/Assets/EnumExtensionsTests/MyEnum.cs b/Assets/EnumExtensionsTests/MyEnum.cs index f4be221..f487d00 100644 --- a/Assets/EnumExtensionsTests/MyEnum.cs +++ b/Assets/EnumExtensionsTests/MyEnum.cs @@ -25,4 +25,7 @@ public enum MyFlags : byte } static partial class MyFlagsExtensions { } + + [EnumExtensions] + public enum MyEnum2 { _FirstCase, SecondCase, Third_Case, forth_case, fifthCase } } diff --git a/Packages/ZBase.Foundation.EnumExtensions/SourceGenerator/ZBase.Foundation.EnumExtensions.SourceGen.dll b/Packages/ZBase.Foundation.EnumExtensions/SourceGenerator/ZBase.Foundation.EnumExtensions.SourceGen.dll index b9440a9..08edfba 100644 Binary files a/Packages/ZBase.Foundation.EnumExtensions/SourceGenerator/ZBase.Foundation.EnumExtensions.SourceGen.dll and b/Packages/ZBase.Foundation.EnumExtensions/SourceGenerator/ZBase.Foundation.EnumExtensions.SourceGen.dll differ diff --git a/Packages/ZBase.Foundation.EnumExtensions/Source~/ZBase.Foundation.EnumExtensions.SourceGen/SourceGen.Common/StringExtensions.cs b/Packages/ZBase.Foundation.EnumExtensions/Source~/ZBase.Foundation.EnumExtensions.SourceGen/SourceGen.Common/StringExtensions.cs index 860263b..ccce2ef 100644 --- a/Packages/ZBase.Foundation.EnumExtensions/Source~/ZBase.Foundation.EnumExtensions.SourceGen/SourceGen.Common/StringExtensions.cs +++ b/Packages/ZBase.Foundation.EnumExtensions/Source~/ZBase.Foundation.EnumExtensions.SourceGen/SourceGen.Common/StringExtensions.cs @@ -1,4 +1,9 @@ -namespace ZBase.Foundation.SourceGen +using System; +using System.Globalization; +using System.Linq; +using System.Text.RegularExpressions; + +namespace ZBase.Foundation.SourceGen { public static class StringExtensions { @@ -18,5 +23,65 @@ public static string ToValidNamespace(this string value) .Replace('>', 'ᐳ') .Replace("[]", "Array") ; + + public static string ToTitleCase(this string value) + { + // Remove leading and trailing underscores + value = value.Trim('_'); + + // Split the string at capital letters or underscores + string[] words = Regex.Split(value, @"(? !string.IsNullOrEmpty(word)) // Remove any empty strings resulting from split + .Select(word => textInfo.ToTitleCase(word.ToLower())) // Convert each word to title case + .ToArray(); + + // Join all words with a single space and return + return string.Join(" ", words).Trim(); + } + + public static string ToSentenceCase(this string value) + { + // Remove leading and trailing underscores + value = value.Trim('_'); + + // Split the string at capital letters or underscores + string[] words = Regex.Split(value, @"(? !string.IsNullOrEmpty(word)) // Remove any empty strings resulting from split + .Select(word => { + word = word.ToLower(); + return word.Length > 0 ? char.ToUpper(word[0]) + word.Substring(1) : word; + }) // Capitalize the first letter of each word + .ToArray(); + + // Join all words with a space and return + return string.Join(" ", words).Trim(); + } + + public static string ToSnakeCase(this string input) + { + // Remove leading and trailing underscores + input = input.Trim('_'); + + // Split the string at capital letters or underscores + string[] words = Regex.Split(input, @"(? !string.IsNullOrEmpty(word)) // Remove any empty strings resulting from split + .Select(word => word.ToLower()) // Convert each word to lower case + .ToArray(); + + // Join all words with an underscore and return + return string.Join("_", words); + } } } diff --git a/Packages/ZBase.Foundation.EnumExtensions/Source~/ZBase.Foundation.EnumExtensions.SourceGen/SourceGen.EnumExtensions/EnumDeclaration+WriteCode.cs b/Packages/ZBase.Foundation.EnumExtensions/Source~/ZBase.Foundation.EnumExtensions.SourceGen/SourceGen.EnumExtensions/EnumDeclaration+WriteCode.cs index cb02446..b9eb21e 100644 --- a/Packages/ZBase.Foundation.EnumExtensions/Source~/ZBase.Foundation.EnumExtensions.SourceGen/SourceGen.EnumExtensions/EnumDeclaration+WriteCode.cs +++ b/Packages/ZBase.Foundation.EnumExtensions/Source~/ZBase.Foundation.EnumExtensions.SourceGen/SourceGen.EnumExtensions/EnumDeclaration+WriteCode.cs @@ -6,20 +6,26 @@ namespace ZBase.Foundation.EnumExtensions partial class EnumDeclaration { private const string GENERATED_CODE = "[global::System.CodeDom.Compiler.GeneratedCode(\"ZBase.Foundation.EnumExtensions.EnumExtensionsGenerator\", \"1.2.2\")]"; - private const string AGGRESSIVE_INLINING = "[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]"; + + private const string AGGRESSIVE_INLINING = + "[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]"; + private const string EXCLUDE_COVERAGE = "[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]"; private const string UNITY_COLLECTIONS_ALLOCATOR = "global::Unity.Collections.AllocatorManager.AllocatorHandle"; private const string CLASS_VALUES = "Values"; private const string CLASS_UNDERLYING_VALUES = "UnderlyingValues"; private const string CLASS_WRAPPERS = "Wrappers"; private const string CLASS_NAMES = "Names"; + private const string CLASS_LOWER_CASE = "LowerCaseNames"; + private const string CLASS_UPPER_CASE = "UpperCaseNames"; + private const string CLASS_TITLE_CASE = "TitleCaseNames"; + private const string CLASS_SENTENCE_CASE = "SentenceCaseNames"; + private const string CLASS_SNAKE_CASE = "SnakeCaseNames"; private const string CLASS_DISPLAY_NAMES = "DisplayNames"; private const string CLASS_FIXED_NAMES = "FixedNames"; private const string CLASS_FIXED_DISPLAY_NAMES = "FixedDisplayNames"; - private static readonly string[] s_primitiveTypes = new string[] { - "byte", "sbyte", "short", "ushort", "int", "uint", "long", "ulong" - }; + private static readonly string[] s_primitiveTypes = new string[] {"byte", "sbyte", "short", "ushort", "int", "uint", "long", "ulong"}; public string WriteCode() { @@ -103,7 +109,7 @@ private void WriteStruct(ref Printer p) p.PrintLine("[global::System.Runtime.InteropServices.FieldOffset(0)]"); p.PrintBeginLine("public readonly ").Print(FullyQualifiedName).PrintEndLine(" Value;"); p.PrintEndLine(); - + p.PrintLine(GENERATED_CODE); p.PrintLine("[global::System.Runtime.InteropServices.FieldOffset(0)]"); p.PrintBeginLine("public readonly ").Print(UnderlyingTypeName).PrintEndLine(" UnderlyingValue;"); @@ -175,7 +181,7 @@ private void WriteStruct(ref Printer p) p.PrintBeginLine("public bool IsDefined() => ") .Print(ExtensionsName).PrintEndLine(".IsDefined(this.Value);"); p.PrintEndLine(); - + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); p.PrintBeginLine("public bool IsDefinedIn(string name) => ") .Print(ExtensionsName).Print(".IsDefinedIn(name, default(") @@ -197,7 +203,7 @@ private void WriteStruct(ref Printer p) p.PrintBeginLine("public override string ToString() => ") .Print(ExtensionsName).PrintEndLine(".ToStringFast(this.Value);"); p.PrintEndLine(); - + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); p.PrintLine("public override int GetHashCode() => this.UnderlyingValue.GetHashCode();"); p.PrintEndLine(); @@ -211,7 +217,7 @@ private void WriteStruct(ref Printer p) p.PrintBeginLine("public bool Equals(").Print(ExtensionsWrapperName) .PrintEndLine(" other) => this.UnderlyingValue == other.UnderlyingValue;"); p.PrintEndLine(); - + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); p.PrintBeginLine("public override bool Equals(object obj) => obj is ").Print(ExtensionsWrapperName) .PrintEndLine(" other && this.UnderlyingValue == other.UnderlyingValue;"); @@ -274,6 +280,11 @@ private void WriteClass(ref Printer p) p.PrintEndLine(); WriteToString(ref p, @this); + WriteToLowercase(ref p, @this); + WriteToUppercase(ref p, @this); + WriteToTitleCase(ref p, @this); + WriteToSentenceCase(ref p, @this); + WriteToSnakeCase(ref p, @this); if (OnlyNames == false) { @@ -319,6 +330,12 @@ private void WriteClass(ref Printer p) WriteClassDisplayNames(ref p); WriteClassFixedNames(ref p); WriteClassFixedDisplayNames(ref p); + + WriteClassLowercase(ref p); + WriteClassUppercase(ref p); + WriteClassTitleCase(ref p); + WriteClassSentenceCase(ref p); + WriteClassSnakeCase(ref p); } p.CloseScope(); p.PrintEndLine(); @@ -351,10 +368,12 @@ private void WriteClassFixedDisplayNames(ref Printer p) if (OnlyNames == false) { p.PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); - p.PrintLine($"public static global::Unity.Collections.NativeArray<{FixedStringTypeFullyQualifiedName}>.ReadOnly AsNativeArray({UNITY_COLLECTIONS_ALLOCATOR} allocator)"); + p.PrintLine( + $"public static global::Unity.Collections.NativeArray<{FixedStringTypeFullyQualifiedName}>.ReadOnly AsNativeArray({UNITY_COLLECTIONS_ALLOCATOR} allocator)"); p.OpenScope(); { - p.PrintLine($"var names = global::Unity.Collections.CollectionHelper.CreateNativeArray<{FixedStringTypeFullyQualifiedName}>({ExtensionsName}.Length, allocator, global::Unity.Collections.NativeArrayOptions.UninitializedMemory);"); + p.PrintLine( + $"var names = global::Unity.Collections.CollectionHelper.CreateNativeArray<{FixedStringTypeFullyQualifiedName}>({ExtensionsName}.Length, allocator, global::Unity.Collections.NativeArrayOptions.UninitializedMemory);"); var index = 0; @@ -422,10 +441,12 @@ private void WriteClassFixedNames(ref Printer p) if (OnlyNames == false) { p.PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); - p.PrintLine($"public static global::Unity.Collections.NativeArray<{FixedStringTypeFullyQualifiedName}>.ReadOnly AsNativeArray({UNITY_COLLECTIONS_ALLOCATOR} allocator)"); + p.PrintLine( + $"public static global::Unity.Collections.NativeArray<{FixedStringTypeFullyQualifiedName}>.ReadOnly AsNativeArray({UNITY_COLLECTIONS_ALLOCATOR} allocator)"); p.OpenScope(); { - p.PrintLine($"var names = global::Unity.Collections.CollectionHelper.CreateNativeArray<{FixedStringTypeFullyQualifiedName}>({ExtensionsName}.Length, allocator, global::Unity.Collections.NativeArrayOptions.UninitializedMemory);"); + p.PrintLine( + $"var names = global::Unity.Collections.CollectionHelper.CreateNativeArray<{FixedStringTypeFullyQualifiedName}>({ExtensionsName}.Length, allocator, global::Unity.Collections.NativeArrayOptions.UninitializedMemory);"); var index = 0; @@ -584,6 +605,281 @@ private void WriteClassNames(ref Printer p) p.CloseScope(); p.PrintEndLine(); } + + private void WriteClassLowercase(ref Printer p) + { + p.PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintBeginLine("public static partial class ").PrintEndLine(CLASS_LOWER_CASE); + p.OpenScope(); + { + foreach (var member in Members) + { + p.PrintLine(GENERATED_CODE); + p.PrintLine($"public const string {member.name} = \"{member.name.ToLowerInvariant()}\";"); + p.PrintEndLine(); + } + + if (OnlyNames == false) + { + p.PrintLine($"private static readonly string[] s_names = new string[]"); + p.OpenScope(); + { + foreach (var member in Members) + { + p.PrintLine($"{member.name},"); + } + } + p.CloseScope("};"); + p.PrintEndLine(); + + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine("public static global::System.ReadOnlyMemory AsMemory() => s_names;"); + p.PrintEndLine(); + + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine("public static global::System.ReadOnlySpan AsSpan() => s_names;"); + p.PrintEndLine(); + } + + p.PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine($"public static string GetLowercase({FullyQualifiedName} value)"); + p = p.IncreasedIndent(); + p.PrintLine("=> value switch"); + p.OpenScope(); + { + foreach (var member in Members) + { + p.PrintLine($"{FullyQualifiedName}.{member.name} => {member.name},"); + } + + p.PrintLine("_ => ToUnderlyingValue(value).ToString(),"); + } + p.CloseScope("};"); + p = p.DecreasedIndent(); + } + p.CloseScope(); + p.PrintEndLine(); + } + + private void WriteClassUppercase(ref Printer p) + { + p.PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintBeginLine("public static partial class ").PrintEndLine(CLASS_UPPER_CASE); + p.OpenScope(); + { + foreach (var member in Members) + { + p.PrintLine(GENERATED_CODE); + p.PrintLine($"public const string {member.name} = \"{member.name.ToUpperInvariant()}\";"); + p.PrintEndLine(); + } + + if (OnlyNames == false) + { + p.PrintLine($"private static readonly string[] s_names = new string[]"); + p.OpenScope(); + { + foreach (var member in Members) + { + p.PrintLine($"{member.name},"); + } + } + p.CloseScope("};"); + p.PrintEndLine(); + + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine("public static global::System.ReadOnlyMemory AsMemory() => s_names;"); + p.PrintEndLine(); + + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine("public static global::System.ReadOnlySpan AsSpan() => s_names;"); + p.PrintEndLine(); + } + + p.PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine($"public static string GetUppercase({FullyQualifiedName} value)"); + p = p.IncreasedIndent(); + p.PrintLine("=> value switch"); + p.OpenScope(); + { + foreach (var member in Members) + { + p.PrintLine($"{FullyQualifiedName}.{member.name} => {member.name},"); + } + + p.PrintLine("_ => ToUnderlyingValue(value).ToString(),"); + } + p.CloseScope("};"); + p = p.DecreasedIndent(); + } + p.CloseScope(); + p.PrintEndLine(); + } + + private void WriteClassTitleCase(ref Printer p) + { + p.PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintBeginLine("public static partial class ").PrintEndLine(CLASS_TITLE_CASE); + p.OpenScope(); + { + foreach (var member in Members) + { + p.PrintLine(GENERATED_CODE); + p.PrintLine($"public const string {member.name} = \"{member.name.ToTitleCase()}\";"); + p.PrintEndLine(); + } + + if (OnlyNames == false) + { + p.PrintLine($"private static readonly string[] s_names = new string[]"); + p.OpenScope(); + { + foreach (var member in Members) + { + p.PrintLine($"{member.name},"); + } + } + p.CloseScope("};"); + p.PrintEndLine(); + + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine("public static global::System.ReadOnlyMemory AsMemory() => s_names;"); + p.PrintEndLine(); + + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine("public static global::System.ReadOnlySpan AsSpan() => s_names;"); + p.PrintEndLine(); + } + + p.PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine($"public static string GetTitleCase({FullyQualifiedName} value)"); + p = p.IncreasedIndent(); + p.PrintLine("=> value switch"); + p.OpenScope(); + { + foreach (var member in Members) + { + p.PrintLine($"{FullyQualifiedName}.{member.name} => {member.name},"); + } + + p.PrintLine("_ => ToUnderlyingValue(value).ToString(),"); + } + p.CloseScope("};"); + p = p.DecreasedIndent(); + } + p.CloseScope(); + p.PrintEndLine(); + } + + private void WriteClassSentenceCase(ref Printer p) + { + p.PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintBeginLine("public static partial class ").PrintEndLine(CLASS_SENTENCE_CASE); + p.OpenScope(); + { + foreach (var member in Members) + { + p.PrintLine(GENERATED_CODE); + p.PrintLine($"public const string {member.name} = \"{member.name.ToSentenceCase()}\";"); + p.PrintEndLine(); + } + + if (OnlyNames == false) + { + p.PrintLine($"private static readonly string[] s_names = new string[]"); + p.OpenScope(); + { + foreach (var member in Members) + { + p.PrintLine($"{member.name},"); + } + } + p.CloseScope("};"); + p.PrintEndLine(); + + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine("public static global::System.ReadOnlyMemory AsMemory() => s_names;"); + p.PrintEndLine(); + + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine("public static global::System.ReadOnlySpan AsSpan() => s_names;"); + p.PrintEndLine(); + } + + p.PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine($"public static string GetSentenceCase({FullyQualifiedName} value)"); + p = p.IncreasedIndent(); + p.PrintLine("=> value switch"); + p.OpenScope(); + { + foreach (var member in Members) + { + p.PrintLine($"{FullyQualifiedName}.{member.name} => {member.name},"); + } + + p.PrintLine("_ => ToUnderlyingValue(value).ToString(),"); + } + p.CloseScope("};"); + p = p.DecreasedIndent(); + } + p.CloseScope(); + p.PrintEndLine(); + } + + private void WriteClassSnakeCase(ref Printer p) + { + p.PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintBeginLine("public static partial class ").PrintEndLine(CLASS_SNAKE_CASE); + p.OpenScope(); + { + foreach (var member in Members) + { + p.PrintLine(GENERATED_CODE); + p.PrintLine($"public const string {member.name} = \"{member.name.ToSnakeCase()}\";"); + p.PrintEndLine(); + } + + if (OnlyNames == false) + { + p.PrintLine($"private static readonly string[] s_names = new string[]"); + p.OpenScope(); + { + foreach (var member in Members) + { + p.PrintLine($"{member.name},"); + } + } + p.CloseScope("};"); + p.PrintEndLine(); + + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine("public static global::System.ReadOnlyMemory AsMemory() => s_names;"); + p.PrintEndLine(); + + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine("public static global::System.ReadOnlySpan AsSpan() => s_names;"); + p.PrintEndLine(); + } + + p.PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine($"public static string GetSnakeCase({FullyQualifiedName} value)"); + p = p.IncreasedIndent(); + p.PrintLine("=> value switch"); + p.OpenScope(); + { + foreach (var member in Members) + { + p.PrintLine($"{FullyQualifiedName}.{member.name} => {member.name},"); + } + + p.PrintLine("_ => ToUnderlyingValue(value).ToString(),"); + } + p.CloseScope("};"); + p = p.DecreasedIndent(); + } + p.CloseScope(); + p.PrintEndLine(); + } private void WriteClassUnderlyingValues(ref Printer p) { @@ -623,7 +919,7 @@ private void WriteClassUnderlyingValues(ref Printer p) p.CloseScope(); p.PrintEndLine(); } - + private void WriteClassValues(ref Printer p) { p.PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); @@ -978,7 +1274,8 @@ private void WriteTryParseSpan(ref Printer p, string @this) { if (member.displayName is not null) { - p.PrintLine($"case {SPAN} s when global::System.MemoryExtensions.Equals(s, global::System.MemoryExtensions.AsSpan({CLASS_DISPLAY_NAMES}.{member.name}), global::System.StringComparison.OrdinalIgnoreCase):"); + p.PrintLine( + $"case {SPAN} s when global::System.MemoryExtensions.Equals(s, global::System.MemoryExtensions.AsSpan({CLASS_DISPLAY_NAMES}.{member.name}), global::System.StringComparison.OrdinalIgnoreCase):"); p.OpenScope(); { p.PrintLine($"value = {FullyQualifiedName}.{member.name};"); @@ -1003,7 +1300,8 @@ private void WriteTryParseSpan(ref Printer p, string @this) { if (member.displayName is not null) { - p.PrintLine($"case {SPAN} s when global::System.MemoryExtensions.Equals(s, global::System.MemoryExtensions.AsSpan({CLASS_DISPLAY_NAMES}.{member.name}), global::System.StringComparison.Ordinal):"); + p.PrintLine( + $"case {SPAN} s when global::System.MemoryExtensions.Equals(s, global::System.MemoryExtensions.AsSpan({CLASS_DISPLAY_NAMES}.{member.name}), global::System.StringComparison.Ordinal):"); p.OpenScope(); { p.PrintLine($"value = {FullyQualifiedName}.{member.name};"); @@ -1030,7 +1328,8 @@ private void WriteTryParseSpan(ref Printer p, string @this) { foreach (var member in Members) { - p.PrintLine($"case {SPAN} s when global::System.MemoryExtensions.Equals(s, global::System.MemoryExtensions.AsSpan({CLASS_NAMES}.{member.name}), global::System.StringComparison.OrdinalIgnoreCase):"); + p.PrintLine( + $"case {SPAN} s when global::System.MemoryExtensions.Equals(s, global::System.MemoryExtensions.AsSpan({CLASS_NAMES}.{member.name}), global::System.StringComparison.OrdinalIgnoreCase):"); p.OpenScope(); { p.PrintLine($"value = {FullyQualifiedName}.{member.name};"); @@ -1066,7 +1365,8 @@ private void WriteTryParseSpan(ref Printer p, string @this) { foreach (var member in Members) { - p.PrintLine($"case {SPAN} s when global::System.MemoryExtensions.Equals(s, global::System.MemoryExtensions.AsSpan({CLASS_NAMES}.{member.name}), global::System.StringComparison.Ordinal):"); + p.PrintLine( + $"case {SPAN} s when global::System.MemoryExtensions.Equals(s, global::System.MemoryExtensions.AsSpan({CLASS_NAMES}.{member.name}), global::System.StringComparison.Ordinal):"); p.OpenScope(); { p.PrintLine($"value = {FullyQualifiedName}.{member.name};"); @@ -1111,7 +1411,8 @@ private void WriteFlags(ref Printer p, string @this) p.PrintLine("/// "); p.PrintLine("/// Determines whether some of the bit fields are set in the current instance."); p.PrintLine("/// "); - p.PrintLine("/// true if all of the bit fields that are set in flag are also set in the current instance; otherwise, false."); + p.PrintLine( + "/// true if all of the bit fields that are set in flag are also set in the current instance; otherwise, false."); } p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); @@ -1126,7 +1427,8 @@ private void WriteFlags(ref Printer p, string @this) p.PrintLine("/// "); p.PrintLine("/// Determines whether any of the bit fields are set in the current instance."); p.PrintLine("/// "); - p.PrintLine("/// true if any of the bit fields that are set in flag is also set in the current instance; otherwise, false."); + p.PrintLine( + "/// true if any of the bit fields that are set in flag is also set in the current instance; otherwise, false."); } p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); @@ -1434,6 +1736,101 @@ private void WriteToString(ref Printer p, string @this) p.CloseScope(); p.PrintEndLine(); } + + private void WriteToLowercase(ref Printer p, string @this) + { + if (NoDocumentation == false) + { + p.PrintLine("/// "); + p.PrintLine($"/// Returns the lowercase string representation of the value."); + p.PrintLine("/// "); + p.PrintLine("/// The value to retrieve the string value for"); + p.PrintLine("/// The lowercase string representation of the value"); + } + + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine($"public static string ToLowercaseFast({@this}{FullyQualifiedName} value)"); + p = p.IncreasedIndent(); + p.PrintBeginLine("=> ").Print(CLASS_LOWER_CASE).PrintEndLine(".GetLowercase(value);"); + p = p.DecreasedIndent(); + p.PrintEndLine(); + } + + private void WriteToUppercase(ref Printer p, string @this) + { + if (NoDocumentation == false) + { + p.PrintLine("/// "); + p.PrintLine($"/// Returns the uppercase string representation of the value."); + p.PrintLine("/// "); + p.PrintLine("/// The value to retrieve the string value for"); + p.PrintLine("/// The uppercase string representation of the value"); + } + + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine($"public static string ToUppercaseFast({@this}{FullyQualifiedName} value)"); + p = p.IncreasedIndent(); + p.PrintBeginLine("=> ").Print(CLASS_UPPER_CASE).PrintEndLine(".GetUppercase(value);"); + p = p.DecreasedIndent(); + p.PrintEndLine(); + } + + private void WriteToTitleCase(ref Printer p, string @this) + { + if (NoDocumentation == false) + { + p.PrintLine("/// "); + p.PrintLine($"/// Returns the title case string representation of the value."); + p.PrintLine("/// "); + p.PrintLine("/// The value to retrieve the string value for"); + p.PrintLine("/// The title case string representation of the value"); + } + + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine($"public static string ToTitleCaseFast({@this}{FullyQualifiedName} value)"); + p = p.IncreasedIndent(); + p.PrintBeginLine("=> ").Print(CLASS_TITLE_CASE).PrintEndLine(".GetTitleCase(value);"); + p = p.DecreasedIndent(); + p.PrintEndLine(); + } + + private void WriteToSentenceCase(ref Printer p, string @this) + { + if (NoDocumentation == false) + { + p.PrintLine("/// "); + p.PrintLine($"/// Returns the sentence case string representation of the value."); + p.PrintLine("/// "); + p.PrintLine("/// The value to retrieve the string value for"); + p.PrintLine("/// The sentence case string representation of the value"); + } + + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine($"public static string ToSentenceCaseFast({@this}{FullyQualifiedName} value)"); + p = p.IncreasedIndent(); + p.PrintBeginLine("=> ").Print(CLASS_SENTENCE_CASE).PrintEndLine(".GetSentenceCase(value);"); + p = p.DecreasedIndent(); + p.PrintEndLine(); + } + + private void WriteToSnakeCase(ref Printer p, string @this) + { + if (NoDocumentation == false) + { + p.PrintLine("/// "); + p.PrintLine($"/// Returns the snake case string representation of the value."); + p.PrintLine("/// "); + p.PrintLine("/// The value to retrieve the string value for"); + p.PrintLine("/// The snake case string representation of the value"); + } + + p.PrintLine(AGGRESSIVE_INLINING).PrintLine(GENERATED_CODE).PrintLine(EXCLUDE_COVERAGE); + p.PrintLine($"public static string ToSnakeCaseFast({@this}{FullyQualifiedName} value)"); + p = p.IncreasedIndent(); + p.PrintBeginLine("=> ").Print(CLASS_SNAKE_CASE).PrintEndLine(".GetSnakeCase(value);"); + p = p.DecreasedIndent(); + p.PrintEndLine(); + } private static string GetKeyword(Accessibility accessibility) => accessibility switch { @@ -1444,4 +1841,4 @@ private static string GetKeyword(Accessibility accessibility) _ => "public", }; } -} +} \ No newline at end of file