♻️ Extract the Storage service to DysonNetwork.Drive microservice
This commit is contained in:
43
DysonNetwork.Drive/Extensions/StringExtensions.cs
Normal file
43
DysonNetwork.Drive/Extensions/StringExtensions.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace DysonNetwork.Drive.Extensions;
|
||||
|
||||
public static class StringExtensions
|
||||
{
|
||||
private static readonly Regex _matchFirstCap = new(@"(.)([A-Z][a-z])");
|
||||
private static readonly Regex _matchAllCap = new(@"([a-z0-9])([A-Z])");
|
||||
|
||||
public static string ToSnakeCase(this string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
return input;
|
||||
|
||||
// Handle the first character
|
||||
var result = new StringBuilder();
|
||||
result.Append(char.ToLowerInvariant(input[0]));
|
||||
|
||||
// Process the rest of the string
|
||||
for (int i = 1; i < input.Length; i++)
|
||||
{
|
||||
if (char.IsUpper(input[i]))
|
||||
{
|
||||
result.Append('_');
|
||||
result.Append(char.ToLowerInvariant(input[i]));
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Append(input[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Replace any remaining uppercase letters with lowercase
|
||||
var output = result.ToString().ToLowerInvariant();
|
||||
|
||||
// Handle special cases (acronyms)
|
||||
output = _matchFirstCap.Replace(output, "$1_$2");
|
||||
output = _matchAllCap.Replace(output, "$1_$2");
|
||||
|
||||
return output.ToLowerInvariant();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user