Browse Source

Merge pull request #19472 from abpframework/liangshiwei/tosnakecase

Use the ToSnakeCase method provided by .NET8
pull/19475/head
maliming 2 years ago
committed by GitHub
parent
commit
803e0443d3
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 60
      framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs

60
framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs

@ -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>

Loading…
Cancel
Save