mirror of
https://github.com/nsnail/NetAdmin.git
synced 2025-04-20 05:02:50 +08:00
parent
4d857f1861
commit
0e412db6ce
@ -1,4 +1,4 @@
|
||||
#r "nuget: NSExt, 2.2.0"
|
||||
#r "nuget: NSExt, 2.3.2"
|
||||
using NSExt.Extensions;
|
||||
|
||||
Console.WriteLine("请输入原始名称(NetAdmin):");
|
||||
|
@ -1,7 +1,3 @@
|
||||
using System.Numerics;
|
||||
using Microsoft.CodeAnalysis.CSharp.Scripting;
|
||||
using Microsoft.CodeAnalysis.Scripting;
|
||||
|
||||
namespace NetAdmin.Infrastructure.Extensions;
|
||||
|
||||
/// <summary>
|
||||
@ -9,105 +5,6 @@ namespace NetAdmin.Infrastructure.Extensions;
|
||||
/// </summary>
|
||||
public static class StringExtensions
|
||||
{
|
||||
private const string _CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
private static readonly Regex _regexIpV4 = new(Chars.RGXL_IP_V4);
|
||||
|
||||
/// <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.");
|
||||
}
|
||||
|
||||
#pragma warning disable IDE0048, RCS1123, SA1407
|
||||
result = result * 62 + index;
|
||||
#pragma warning restore SA1407, RCS1123, IDE0048
|
||||
}
|
||||
|
||||
// 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>
|
||||
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>
|
||||
/// 计算Crc32
|
||||
/// </summary>
|
||||
public static int Crc32(this string me)
|
||||
{
|
||||
return BitConverter.ToInt32(System.IO.Hashing.Crc32.Hash(Encoding.UTF8.GetBytes(me)));
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// 是否IPV4地址
|
||||
/// </summary>
|
||||
public static bool IsIpV4(this string me)
|
||||
{
|
||||
return _regexIpV4.IsMatch(me);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// object -> json
|
||||
/// </summary>
|
||||
@ -124,22 +21,6 @@ public static class StringExtensions
|
||||
return me.Object(toType, GlobalStatic.JsonSerializerOptions);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 去掉前部字符串
|
||||
/// </summary>
|
||||
public static string TrimPrefix(this string me, string clearStr)
|
||||
{
|
||||
return Regex.Replace(me, $"^{clearStr}", string.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 去掉尾部字符串
|
||||
/// </summary>
|
||||
public static string TrimSuffix(this string me, string clearStr)
|
||||
{
|
||||
return Regex.Replace(me, $"{clearStr}$", string.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 去掉尾部字符串“Async”
|
||||
/// </summary>
|
||||
@ -147,7 +28,7 @@ public static class StringExtensions
|
||||
public static string TrimSuffixAsync(this string me)
|
||||
#pragma warning restore VSTHRD200, ASA002, RCS1047
|
||||
{
|
||||
return TrimSuffix(me, "Async");
|
||||
return me.TrimSuffix("Async");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -155,6 +36,6 @@ public static class StringExtensions
|
||||
/// </summary>
|
||||
public static string TrimSuffixOptions(this string me)
|
||||
{
|
||||
return TrimSuffix(me, "Options");
|
||||
return me.TrimSuffix("Options");
|
||||
}
|
||||
}
|
@ -6,10 +6,9 @@
|
||||
<PackageReference Include="FreeSql.DbContext.NS" Version="3.2.833-ns6" Label="refs"/>
|
||||
<PackageReference Include="FreeSql.Provider.Sqlite.NS" Version="3.2.833-ns6" Label="refs"/>
|
||||
<PackageReference Include="Gurion" Version="1.2.2" Label="refs"/>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.12.0-3.final"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.0"/>
|
||||
<PackageReference Include="Minio" Version="6.0.3"/>
|
||||
<PackageReference Include="NSExt" Version="2.2.0"/>
|
||||
<PackageReference Include="NSExt" Version="2.3.2"/>
|
||||
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.4"/>
|
||||
<PackageReference Include="System.Drawing.Common" Version="9.0.0"/>
|
||||
</ItemGroup>
|
||||
|
Loading…
x
Reference in New Issue
Block a user