-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
106 lines (97 loc) · 3.79 KB
/
StringExtensions.cs
File metadata and controls
106 lines (97 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.Text;
namespace GDScriptInterface.SourceGenerator;
/// <summary>
/// Provides extension methods for string manipulation, including converting to snake_case and PascalCase.
/// </summary>
internal static class StringExtensions
{
/// <summary>
/// Converts the input string to snake_case format.
/// </summary>
/// <param name="input">The string to convert.</param>
/// <returns>The converted string in snake_case format.</returns>
public static string ToSnakeCase(this string input)
{
StringBuilder sb = new();
char[] chars = input.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if (chars[i] == ' ')
{
sb.Append('_'); // Replace spaces with underscores
}
else if (char.IsUpper(chars[i]))
{
// Handle uppercase letters that need an underscore prefix
if (i + 1 < chars.Length && char.IsLower(chars[i + 1]) &&
i > 0 && char.IsUpper(chars[i - 1]))
{
sb.Append('_'); // Add underscore before a single uppercase letter surrounded by uppercase letters
}
sb.Append(char.ToLower(chars[i])); // Convert uppercase letter to lowercase
}
else if (char.IsLower(chars[i]))
{
// Handle lowercase letters that may need an underscore prefix
if (i > 0 && char.IsDigit(chars[i - 1]) && i + 1 < chars.Length && char.IsLower(chars[i + 1]))
{
sb.Append('_'); // Add underscore between digits and lowercase letters
}
sb.Append(chars[i]); // Append the lowercase letter
if (i + 1 < chars.Length && char.IsUpper(chars[i + 1]))
{
sb.Append('_'); // Add underscore before an uppercase letter following a lowercase letter
}
}
else if (char.IsDigit(chars[i]))
{
// Handle digits that may need an underscore prefix
if (i > 0 && (char.IsUpper(chars[i - 1]) || char.IsLower(chars[i - 1])))
{
sb.Append('_'); // Add underscore between letters and digits
}
sb.Append(chars[i]); // Append the digit
}
else
{
sb.Append(chars[i]); // Append other characters as-is
}
}
return sb.ToString(); // Return the final snake_case string
}
/// <summary>
/// Converts the input string to PascalCase format.
/// </summary>
/// <param name="input">The string to convert.</param>
/// <returns>The converted string in PascalCase format.</returns>
public static string ToPascalCase(this string input)
{
StringBuilder sb = new();
char[] chars = input.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if (chars[i] == '_' || chars[i] == ' ')
{
continue; // Skip underscores and spaces
}
else if (char.IsLower(chars[i]))
{
// Capitalize lowercase letters at the start or after underscores/spaces
if ((i == 0) ||
(i > 0 && (chars[i - 1] == '_' || chars[i - 1] == ' ')))
{
sb.Append(char.ToUpper(chars[i])); // Convert to uppercase
}
else
{
sb.Append(chars[i]); // Append lowercase letter as-is
}
}
else
{
sb.Append(chars[i]); // Append other characters as-is
}
}
return sb.ToString(); // Return the final PascalCase string
}
}