mirror of
https://github.com/nsnail/ns-ext.git
synced 2025-04-15 23:42:51 +08:00
Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
c9975472bf | ||
![]() |
97c93bd854 | ||
![]() |
322cbc32df | ||
![]() |
fa84cc315b | ||
![]() |
d4a92aca6f | ||
![]() |
a9f3416c37 | ||
![]() |
890b229683 | ||
c3c6819a99 | |||
![]() |
efa6a564dc | ||
7c5b5443cb | |||
![]() |
689d9560a8 | ||
![]() |
49de51d649 | ||
6676323b26 | |||
![]() |
c7c978fe5c | ||
![]() |
892df78c26 | ||
![]() |
7a1c8db6e4 | ||
![]() |
2b6c7adff8 |
@ -19,6 +19,10 @@ indent_size = 2 # 对于 JSON 和 YAML 文件,缩进大小为 2 个空格
|
||||
[*.cs]
|
||||
dotnet_analyzer_diagnostic.severity = warning # 设置 C# 文件中所有 dotnet_analyzer_diagnostic 的严重性级别为 warning
|
||||
|
||||
[*.g.cs]
|
||||
dotnet_analyzer_diagnostic.severity = none # 禁用所有代码分析规则
|
||||
|
||||
|
||||
# ReSharper properties
|
||||
resharper_align_linq_query = true # 启用对LINQ查询的对齐
|
||||
resharper_align_multiline_argument = true # 启用多行参数的对齐
|
||||
|
1
.github/workflows/README.md
vendored
Normal file
1
.github/workflows/README.md
vendored
Normal file
@ -0,0 +1 @@
|
||||
github workflows
|
@ -13,13 +13,11 @@ jobs:
|
||||
filter: tree:0
|
||||
- uses: actions/setup-dotnet@v3
|
||||
with:
|
||||
dotnet-version: 8.0.x
|
||||
- working-directory: ./src/backend/NSExt
|
||||
run: dotnet build -c Release -f net6.0
|
||||
- working-directory: ./src/backend/NSExt
|
||||
run: dotnet build -c Release -f net7.0
|
||||
dotnet-version: 9.0.x
|
||||
- working-directory: ./src/backend/NSExt
|
||||
run: dotnet build -c Release -f net8.0
|
||||
- working-directory: ./src/backend/NSExt
|
||||
run: dotnet build -c Release -f net9.0
|
||||
- working-directory: ./src/backend/NSExt
|
||||
run: dotnet pack -c Release --no-build
|
||||
- uses: actions/create-release@v1
|
24
CHANGELOG.md
24
CHANGELOG.md
@ -2,6 +2,30 @@
|
||||
|
||||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
||||
|
||||
### [2.3.5](https://github.com/nsnail/NSExt/compare/v2.3.2...v2.3.5) (2025-03-26)
|
||||
|
||||
### [2.3.4](https://github.com/nsnail/NSExt/compare/v2.3.2...v2.3.4) (2025-03-06)
|
||||
|
||||
### [2.3.3](https://github.com/nsnail/NSExt/compare/v2.3.2...v2.3.3) (2025-01-02)
|
||||
|
||||
### [2.3.2](https://github.com/nsnail/NSExt/compare/v2.3.1...v2.3.2) (2024-11-25)
|
||||
|
||||
### [2.3.1](https://github.com/nsnail/NSExt/compare/v2.3.0...v2.3.1) (2024-11-25)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* 🐛 a stable release of a package should not have a prerelease dependency ([#27](https://github.com/nsnail/NSExt/issues/27)) ([689d956](https://github.com/nsnail/NSExt/commit/689d9560a858c0ac308ec198b041fdc23720beba))
|
||||
|
||||
## [2.3.0](https://github.com/nsnail/NSExt/compare/v2.2.1...v2.3.0) (2024-11-25)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* ✨ 新增一些扩展方法 ([#25](https://github.com/nsnail/NSExt/issues/25)) ([c7c978f](https://github.com/nsnail/NSExt/commit/c7c978fe5cf931e085e40ad1347bdb3f3450dd0a))
|
||||
|
||||
### [2.2.2](https://github.com/nsnail/NSExt/compare/v2.2.1...v2.2.2) (2024-11-25)
|
||||
|
||||
### [2.2.1](https://github.com/nsnail/NSExt/compare/v2.2.0...v2.2.1) (2024-11-25)
|
||||
|
||||
## [2.2.0](https://github.com/nsnail/NSExt/compare/v2.1.0...v2.2.0) (2024-07-02)
|
||||
|
@ -17,7 +17,7 @@
|
||||
<LangVersion>preview</LangVersion>
|
||||
<MinVerDefaultPreReleaseIdentifiers>beta</MinVerDefaultPreReleaseIdentifiers>
|
||||
<MinVerTagPrefix>v</MinVerTagPrefix>
|
||||
<NoWarn>CA1707;IDE0005;IDE0008;IDE0010;IDE0028;IDE0055;IDE0160;IDE0300;IDE0305;RCS1141;RCS1142;RCS1181;S101;S1121;S1135;S125;S2094;S3604;S4663;SYSLIB1045;SA1010;RCS1123;SA1407;IDE0048;S1075;S3928</NoWarn>
|
||||
<NoWarn>CA1707;CA1720;CA5350;CA5351;IDE0005;IDE0008;IDE0010;IDE0028;IDE0048;IDE0055;IDE0160;IDE0300;IDE0305;RCS1123;RCS1141;RCS1142;RCS1181;S101;S1075;S1121;S1135;S125;S2094;S3604;S3928;S4663;SA1010;SA1407;SYSLIB1045</NoWarn>
|
||||
<Product>NSExt</Product>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<RepositoryUrl>https://github.com/nsnail/NSExt.git</RepositoryUrl>
|
||||
@ -26,7 +26,7 @@
|
||||
<Title>$(AssemblyName)</Title>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MinVer" Version="6.0.0">
|
||||
<PackageReference Include="MinVer" Version="6.1.0-beta.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
@ -51,7 +51,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{1129
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{3C6F049E-3EE8-4D66-9AFF-E8A369032487}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
ci.yml = .github/workflows/ci.yml
|
||||
README.md = .github/workflows/README.md
|
||||
release.yml = .github/workflows/release.yml
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{8E4C93BA-9493-4892-80C4-5E174C504829}"
|
||||
|
1
assets/README.md
Normal file
1
assets/README.md
Normal file
@ -0,0 +1 @@
|
||||
# 资源文件目录
|
@ -15,15 +15,15 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.12.19">
|
||||
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.13.61">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Roslynator.Analyzers" Version="4.12.9">
|
||||
<PackageReference Include="Roslynator.Analyzers" Version="4.13.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167">
|
||||
<PackageReference Include="SonarAnalyzer.CSharp" Version="10.7.0.110445">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
@ -3,8 +3,8 @@
|
||||
<ItemGroup>
|
||||
<PackageReferenceFiles
|
||||
Condition="%(PackageReference.CopyToOutputDirectory) != ''"
|
||||
Include="$(NugetPackageRoot)\%(PackageReference.Identity)\%(PackageReference.Version)\%(PackageReference.CopyToOutputDirectory)"/>
|
||||
Include="$(NugetPackageRoot)\%(PackageReference.Identity)\%(PackageReference.Version)\%(PackageReference.CopyToOutputDirectory)" />
|
||||
</ItemGroup>
|
||||
<Copy SourceFiles="@(PackageReferenceFiles)" DestinationFolder="$(OutDir)"/>
|
||||
<Copy SourceFiles="@(PackageReferenceFiles)" DestinationFolder="$(OutDir)" />
|
||||
</Target>
|
||||
</Project>
|
1
docker/README.md
Normal file
1
docker/README.md
Normal file
@ -0,0 +1 @@
|
||||
docker
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "2.2.1",
|
||||
"version": "2.3.5",
|
||||
"devDependencies": {
|
||||
"cz-git": "^1.11.0",
|
||||
"commitizen": "^4.3.1",
|
||||
|
@ -1,12 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/>
|
||||
<PackageReference Include="xunit" Version="2.9.2"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0-pre.49">
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.0-preview-25107-01"/>
|
||||
<PackageReference Include="xunit" Version="2.9.3"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2">
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
|
@ -27,7 +27,7 @@ public static class EnumExtensions
|
||||
var resDescAttr = typeOfField!.GetCustomAttribute<ResourceDescriptionAttribute<T>>(true);
|
||||
return resDescAttr is null
|
||||
? Enum.GetName(typeOfEnum, e)
|
||||
: typeof(T).GetProperty(resDescAttr.ResourceName)?.GetValue(default) as string;
|
||||
: typeof(T).GetProperty(resDescAttr.ResourceName)?.GetValue(null) as string;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -36,6 +36,8 @@ public static class IntExtensions
|
||||
/// </summary>
|
||||
public static string ToIpV4(this int me)
|
||||
{
|
||||
return string.Join(".", BitConverter.GetBytes(me).Reverse());
|
||||
var bytes = BitConverter.GetBytes(me);
|
||||
Array.Reverse(bytes);
|
||||
return string.Join('.', bytes);
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
// ReSharper disable UnusedMember.Global
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
|
||||
#pragma warning disable CA1720
|
||||
using System.Numerics;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.Json;
|
||||
using System.Web;
|
||||
using Microsoft.CodeAnalysis.CSharp.Scripting;
|
||||
using Microsoft.CodeAnalysis.Scripting;
|
||||
using NSExt.Constant;
|
||||
|
||||
namespace NSExt.Extensions;
|
||||
@ -12,9 +12,11 @@ namespace NSExt.Extensions;
|
||||
/// <summary>
|
||||
/// StringExtensions
|
||||
/// </summary>
|
||||
#pragma warning disable CodeLinesAnalyzer
|
||||
public static class StringExtensions
|
||||
public static partial class StringExtensions
|
||||
{
|
||||
private const string _CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
private static readonly Regex _regexIpV4 = RegexIpV4();
|
||||
|
||||
/// <summary>
|
||||
/// aes加密
|
||||
/// </summary>
|
||||
@ -24,11 +26,11 @@ public static class StringExtensions
|
||||
{
|
||||
using var aes = System.Security.Cryptography.Aes.Create();
|
||||
aes.Padding = PaddingMode.PKCS7;
|
||||
aes.Mode = CipherMode.ECB;
|
||||
aes.Key = key.Hex();
|
||||
aes.Mode = CipherMode.ECB;
|
||||
aes.Key = key.Hex();
|
||||
using var encryptor = aes.CreateEncryptor();
|
||||
var bytes = me.Hex();
|
||||
var decrypted = encryptor.TransformFinalBlock(bytes, 0, bytes.Length);
|
||||
var bytes = me.Hex();
|
||||
var decrypted = encryptor.TransformFinalBlock(bytes, 0, bytes.Length);
|
||||
return decrypted.Base64();
|
||||
}
|
||||
|
||||
@ -41,14 +43,72 @@ public static class StringExtensions
|
||||
{
|
||||
using var aes = System.Security.Cryptography.Aes.Create();
|
||||
aes.Padding = PaddingMode.PKCS7;
|
||||
aes.Mode = CipherMode.ECB;
|
||||
aes.Key = key.Hex();
|
||||
aes.Mode = CipherMode.ECB;
|
||||
aes.Key = key.Hex();
|
||||
using var encryptor = aes.CreateDecryptor();
|
||||
var bytes = me.Base64De();
|
||||
var decrypted = encryptor.TransformFinalBlock(bytes, 0, bytes.Length);
|
||||
var bytes = me.Base64De();
|
||||
var decrypted = encryptor.TransformFinalBlock(bytes, 0, bytes.Length);
|
||||
return decrypted.HexDe();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将指定的输入字符串进行Base62解码
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentException">ArgumentException</exception>
|
||||
public static string Base62Decode(this string me)
|
||||
{
|
||||
BigInteger result = 0;
|
||||
|
||||
foreach (var index in me.Select(c => _CHARACTERS.IndexOf(c)))
|
||||
{
|
||||
if (index < 0)
|
||||
{
|
||||
throw new ArgumentException("Invalid character in Base62 string.");
|
||||
}
|
||||
|
||||
result = result * 62 + index;
|
||||
}
|
||||
|
||||
// Convert BigInteger back to byte array and then to string
|
||||
var bytes = result.ToByteArray();
|
||||
|
||||
// Handle the sign bit
|
||||
if (bytes[^1] == 0)
|
||||
{
|
||||
Array.Resize(ref bytes, bytes.Length - 1);
|
||||
}
|
||||
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将指定的输入字符串进行Base62编码
|
||||
/// </summary>
|
||||
public static string Base62Encode(this string me)
|
||||
{
|
||||
// Convert string to byte array
|
||||
var bytes = Encoding.UTF8.GetBytes(me);
|
||||
|
||||
// Convert byte array to BigInteger for easier processing
|
||||
var bigInteger = new BigInteger(bytes);
|
||||
|
||||
if (bigInteger == 0)
|
||||
{
|
||||
return _CHARACTERS[0].ToString();
|
||||
}
|
||||
|
||||
var result = new StringBuilder();
|
||||
|
||||
while (bigInteger > 0)
|
||||
{
|
||||
var remainder = (int)(bigInteger % 62);
|
||||
bigInteger /= 62;
|
||||
_ = result.Insert(0, _CHARACTERS[remainder]);
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// base64编码
|
||||
/// </summary>
|
||||
@ -81,6 +141,22 @@ public static class StringExtensions
|
||||
return e.GetString(me.Base64De());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解码避免转义的Base64
|
||||
/// </summary>
|
||||
public static string Base64InUrlDecode(this string me)
|
||||
{
|
||||
return me.Replace("-", "+").Replace("_", "/");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 编码避免转义的Base64
|
||||
/// </summary>
|
||||
public static string Base64InUrlEncode(this string me)
|
||||
{
|
||||
return me.Replace("+", "-").Replace("/", "_");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将易于web传输的base64web字符串转换为原生base64
|
||||
/// </summary>
|
||||
@ -99,6 +175,14 @@ public static class StringExtensions
|
||||
return me.Replace("+", "-").Replace("/", "_").Replace("=", ".");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算Crc32
|
||||
/// </summary>
|
||||
public static int Crc32(this string me)
|
||||
{
|
||||
return BitConverter.ToInt32(System.IO.Hashing.Crc32.Hash(Encoding.UTF8.GetBytes(me)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换成日期对象
|
||||
/// </summary>
|
||||
@ -129,9 +213,7 @@ public static class StringExtensions
|
||||
/// <returns>转换后的日期对象</returns>
|
||||
public static DateTime DateTimeExactTry(this string me, string format, DateTime def)
|
||||
{
|
||||
return !System.DateTime.TryParseExact(me, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out var ret)
|
||||
? def
|
||||
: ret;
|
||||
return !System.DateTime.TryParseExact(me, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out var ret) ? def : ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -142,9 +224,7 @@ public static class StringExtensions
|
||||
/// <returns>转换后的日期对象</returns>
|
||||
public static DateTime DateTimeTry(this string me, DateTime def)
|
||||
{
|
||||
return !System.DateTime.TryParse(me, CultureInfo.InvariantCulture, DateTimeStyles.None, out var ret)
|
||||
? def
|
||||
: ret;
|
||||
return !System.DateTime.TryParse(me, CultureInfo.InvariantCulture, DateTimeStyles.None, out var ret) ? def : ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -196,6 +276,15 @@ public static class StringExtensions
|
||||
return !System.Enum.TryParse(typeof(T), name, out var ret) ? def : (T)ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行C#代码
|
||||
/// </summary>
|
||||
public static Task<T> ExecuteCSharpCodeAsync<T>(this string me, Assembly[] assemblies, params string[] importNamespaces)
|
||||
{
|
||||
// 使用 Roslyn 编译并执行代码
|
||||
return CSharpScript.EvaluateAsync<T>(me, ScriptOptions.Default.WithReferences(assemblies).WithImports(importNamespaces));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// string to float
|
||||
/// </summary>
|
||||
@ -255,16 +344,13 @@ public static class StringExtensions
|
||||
/// <returns>hash摘要的16进制文本形式(无连字符小写)</returns>
|
||||
public static string HmacSha1(this string me, string secret, Encoding e)
|
||||
{
|
||||
#pragma warning disable CA5350
|
||||
using var hmacSha1 = new HMACSHA1(e.GetBytes(secret));
|
||||
#pragma warning restore CA5350
|
||||
#if NET9_0_OR_GREATER
|
||||
|
||||
#if NET9_0_OR_GREATER
|
||||
return Convert.ToHexStringLower(hmacSha1.ComputeHash(e.GetBytes(me)));
|
||||
#else
|
||||
return BitConverter.ToString(hmacSha1.ComputeHash(e.GetBytes(me)))
|
||||
.Replace("-", string.Empty)
|
||||
.ToLower(CultureInfo.CurrentCulture);
|
||||
#endif
|
||||
#else
|
||||
return BitConverter.ToString(hmacSha1.ComputeHash(e.GetBytes(me))).Replace("-", string.Empty).ToLower(CultureInfo.CurrentCulture);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -344,16 +430,19 @@ public static class StringExtensions
|
||||
// 一个合法的Base64,有着以下特征:
|
||||
// 字符串的长度为4的整数倍。
|
||||
// 字符串的符号取值只能在A -Z, a -z, 0 -9, +, /, =共计65个字符中,且 = 如果出现就必须在结尾出现。
|
||||
if (!me.All(x => x.IsBase64Character())) {
|
||||
if (!me.All(x => x.IsBase64Character()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (me.Length % 4 != 0) {
|
||||
if (me.Length % 4 != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var firstEqualSignPos = me.IndexOf('=');
|
||||
if (firstEqualSignPos < 0) {
|
||||
if (firstEqualSignPos < 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -361,20 +450,31 @@ public static class StringExtensions
|
||||
return lastEqualSignPos == me.Length - 1 && me[firstEqualSignPos..lastEqualSignPos].All(x => x == '=');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否IPV4地址
|
||||
/// </summary>
|
||||
public static bool IsIpV4(this string me)
|
||||
{
|
||||
return _regexIpV4.IsMatch(me);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否json字符串
|
||||
/// </summary>
|
||||
/// <param name="me">me</param>
|
||||
public static bool IsJsonString(this string me)
|
||||
{
|
||||
if (me.NullOrEmpty()) {
|
||||
if (me.NullOrEmpty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
try
|
||||
{
|
||||
_ = JsonDocument.Parse(me);
|
||||
}
|
||||
catch {
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -407,15 +507,13 @@ public static class StringExtensions
|
||||
/// <returns>hash摘要的16进制文本形式(无连字符小写)</returns>
|
||||
public static string Md5(this string me, Encoding e)
|
||||
{
|
||||
#pragma warning disable CA5351
|
||||
#if NET9_0_OR_GREATER
|
||||
#if NET9_0_OR_GREATER
|
||||
return Convert.ToHexStringLower(MD5.HashData(e.GetBytes(me)));
|
||||
#else
|
||||
#else
|
||||
return BitConverter.ToString(MD5.HashData(e.GetBytes(me)))
|
||||
#pragma warning restore CA5351
|
||||
.Replace("-", string.Empty)
|
||||
.ToLower(CultureInfo.CurrentCulture);
|
||||
#endif
|
||||
.Replace("-", string.Empty)
|
||||
.ToLower(CultureInfo.CurrentCulture);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -496,15 +594,13 @@ public static class StringExtensions
|
||||
/// <returns>hash摘要的16进制文本形式(无连字符小写)</returns>
|
||||
public static string Sha1(this string me, Encoding e)
|
||||
{
|
||||
#pragma warning disable CA5350
|
||||
#if NET9_0_OR_GREATER
|
||||
#if NET9_0_OR_GREATER
|
||||
return Convert.ToHexStringLower(SHA1.HashData(e.GetBytes(me)));
|
||||
#else
|
||||
#else
|
||||
return BitConverter.ToString(SHA1.HashData(e.GetBytes(me)))
|
||||
#pragma warning restore CA5350
|
||||
.Replace("-", string.Empty)
|
||||
.ToLower(CultureInfo.CurrentCulture);
|
||||
#endif
|
||||
.Replace("-", string.Empty)
|
||||
.ToLower(CultureInfo.CurrentCulture);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -520,7 +616,8 @@ public static class StringExtensions
|
||||
/// </summary>
|
||||
public static string Sub(this string me, int startIndex, int length)
|
||||
{
|
||||
if (startIndex + length > me.Length) {
|
||||
if (startIndex + length > me.Length)
|
||||
{
|
||||
length = me.Length - startIndex;
|
||||
}
|
||||
|
||||
@ -540,10 +637,7 @@ public static class StringExtensions
|
||||
/// </summary>
|
||||
public static string ToLowerCamelCase(this string me)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(me)
|
||||
? me
|
||||
: string.Concat( //
|
||||
me[0].ToString(CultureInfo.InvariantCulture).ToLowerInvariant(), me.AsSpan(1));
|
||||
return string.IsNullOrWhiteSpace(me) ? me : string.Concat(me[0].ToString(CultureInfo.InvariantCulture).ToLowerInvariant(), me.AsSpan(1));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -554,6 +648,14 @@ public static class StringExtensions
|
||||
return string.IsNullOrWhiteSpace(me) ? me : string.Concat(me[0].ToString().ToUpperInvariant(), me.AsSpan(1));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 去掉前部字符串
|
||||
/// </summary>
|
||||
public static string TrimPrefix(this string me, string clearStr)
|
||||
{
|
||||
return Regex.Replace(me, $"^{clearStr}", string.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将连续多个空格替换成一个空格
|
||||
/// </summary>
|
||||
@ -565,24 +667,25 @@ public static class StringExtensions
|
||||
return ret == me ? ret : ret.TrimSpaces();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 去掉尾部字符串
|
||||
/// </summary>
|
||||
public static string TrimSuffix(this string me, string clearStr)
|
||||
{
|
||||
return Regex.Replace(me, $"{clearStr}$", string.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将\ux0000 、 %u0000 、 &#x0000; 编码转换成可读字符串
|
||||
/// </summary>
|
||||
public static string UnicodeDe(this string me)
|
||||
{
|
||||
#pragma warning disable S3358, RCS1238
|
||||
const string replacement = "&#x$1;";
|
||||
if (me.Contains(@"\u")) {
|
||||
return Regexes.RegexBacksLantUnicode.Replace(me, replacement).HtmlDe();
|
||||
}
|
||||
|
||||
// ReSharper disable once ConvertIfStatementToReturnStatement
|
||||
#pragma warning disable IDE0046
|
||||
if (me.Contains("%u")) {
|
||||
#pragma warning restore IDE0046
|
||||
return Regexes.RegexPercentUnicode.Replace(me, replacement).HtmlDe();
|
||||
}
|
||||
|
||||
return me.HtmlDe();
|
||||
return !me.Contains(@"\u")
|
||||
? me.Contains("%u") ? Regexes.RegexPercentUnicode.Replace(me, replacement).HtmlDe() : me.HtmlDe()
|
||||
: Regexes.RegexBacksLantUnicode.Replace(me, replacement).HtmlDe();
|
||||
#pragma warning restore S3358, RCS1238
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -614,16 +717,15 @@ public static class StringExtensions
|
||||
/// <returns>hash摘要的16进制文本形式(无连字符小写)</returns>
|
||||
private static string Md5Hmac(this string me, string key, Encoding e)
|
||||
{
|
||||
#pragma warning disable CA5351
|
||||
using var md5Hmac = new HMACMD5(e.GetBytes(key));
|
||||
#pragma warning restore CA5351
|
||||
#if NET9_0_OR_GREATER
|
||||
|
||||
#if NET9_0_OR_GREATER
|
||||
return Convert.ToHexStringLower(md5Hmac.ComputeHash(e.GetBytes(me)));
|
||||
#else
|
||||
return BitConverter.ToString(md5Hmac.ComputeHash(e.GetBytes(me)))
|
||||
.Replace("-", string.Empty)
|
||||
.ToLower(CultureInfo.CurrentCulture);
|
||||
#endif
|
||||
#else
|
||||
return BitConverter.ToString(md5Hmac.ComputeHash(e.GetBytes(me))).Replace("-", string.Empty).ToLower(CultureInfo.CurrentCulture);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#pragma warning restore CodeLinesAnalyzer
|
||||
|
||||
[GeneratedRegex(@"^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})(\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})){3}$")]
|
||||
private static partial Regex RegexIpV4();
|
||||
}
|
@ -7,7 +7,9 @@
|
||||
<Import Project="$(SolutionDir)/build/copy.pkg.xml.comment.files.targets"/>
|
||||
<Import Project="$(SolutionDir)/build/prebuild.targets"/>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0"/>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.13.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.3"/>
|
||||
<PackageReference Include="System.IO.Hashing" Version="9.0.3"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Update="*.json">
|
||||
|
Loading…
x
Reference in New Issue
Block a user