|
|
|
@ -2,6 +2,7 @@ using System.Collections.Generic; |
|
|
|
using System.Globalization; |
|
|
|
using System.Security.Cryptography; |
|
|
|
using System.Text; |
|
|
|
using System.Text.Json; |
|
|
|
using System.Text.RegularExpressions; |
|
|
|
using JetBrains.Annotations; |
|
|
|
using Volo.Abp; |
|
|
|
@ -352,64 +353,7 @@ public static class AbpStringExtensions |
|
|
|
/// <returns></returns>
|
|
|
|
public static string ToSnakeCase(this string str) |
|
|
|
{ |
|
|
|
if (string.IsNullOrWhiteSpace(str)) |
|
|
|
{ |
|
|
|
return str; |
|
|
|
} |
|
|
|
|
|
|
|
var builder = new StringBuilder(str.Length + Math.Min(2, str.Length / 5)); |
|
|
|
var previousCategory = default(UnicodeCategory?); |
|
|
|
|
|
|
|
for (var currentIndex = 0; currentIndex < str.Length; currentIndex++) |
|
|
|
{ |
|
|
|
var currentChar = str[currentIndex]; |
|
|
|
if (currentChar == '_') |
|
|
|
{ |
|
|
|
builder.Append('_'); |
|
|
|
previousCategory = null; |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
var currentCategory = char.GetUnicodeCategory(currentChar); |
|
|
|
switch (currentCategory) |
|
|
|
{ |
|
|
|
case UnicodeCategory.UppercaseLetter: |
|
|
|
case UnicodeCategory.TitlecaseLetter: |
|
|
|
if (previousCategory == UnicodeCategory.SpaceSeparator || |
|
|
|
previousCategory == UnicodeCategory.LowercaseLetter || |
|
|
|
previousCategory != UnicodeCategory.DecimalDigitNumber && |
|
|
|
previousCategory != null && |
|
|
|
currentIndex > 0 && |
|
|
|
currentIndex + 1 < str.Length && |
|
|
|
char.IsLower(str[currentIndex + 1])) |
|
|
|
{ |
|
|
|
builder.Append('_'); |
|
|
|
} |
|
|
|
|
|
|
|
currentChar = char.ToLower(currentChar); |
|
|
|
break; |
|
|
|
|
|
|
|
case UnicodeCategory.LowercaseLetter: |
|
|
|
case UnicodeCategory.DecimalDigitNumber: |
|
|
|
if (previousCategory == UnicodeCategory.SpaceSeparator) |
|
|
|
{ |
|
|
|
builder.Append('_'); |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
if (previousCategory != null) |
|
|
|
{ |
|
|
|
previousCategory = UnicodeCategory.SpaceSeparator; |
|
|
|
} |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
builder.Append(currentChar); |
|
|
|
previousCategory = currentCategory; |
|
|
|
} |
|
|
|
|
|
|
|
return builder.ToString(); |
|
|
|
return str.IsNullOrWhiteSpace() ? str : JsonNamingPolicy.SnakeCaseLower.ConvertName(str); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|