mirror of
https://github.com/nsnail/ns-ext.git
synced 2025-04-18 23:22:51 +08:00
..
This commit is contained in:
parent
0073162aed
commit
cb4b8bbafe
@ -24,3 +24,5 @@ public static class ByteExtensions
|
||||
return e.GetString(me);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
26
src/NSExt/Extensions/CharExtensions.cs
Normal file
26
src/NSExt/Extensions/CharExtensions.cs
Normal file
@ -0,0 +1,26 @@
|
||||
namespace NSExt.Extensions;
|
||||
|
||||
public static class CharExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否数字或大小写字母
|
||||
/// </summary>
|
||||
/// <param name="me"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsAsciiLetterOrDigit(this char me)
|
||||
{
|
||||
return (((uint)me - 'A') & ~0x20) < 26 || (uint)me - '0' < 10;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否base64字符
|
||||
/// </summary>
|
||||
/// <param name="me"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsBase64Character(this char me)
|
||||
{
|
||||
return IsAsciiLetterOrDigit(me) || me is '+' or '/' or '=';
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -43,19 +43,6 @@ public static class DateTimeExtensions
|
||||
}
|
||||
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static string yyyyMM(this DateTime me)
|
||||
{
|
||||
return me.ToString("yyyyMM");
|
||||
}
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static string yyyyMMdd(this DateTime me)
|
||||
{
|
||||
return me.ToString("yyyyMMdd");
|
||||
}
|
||||
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static string yyyy_MM(this DateTime me)
|
||||
{
|
||||
@ -86,4 +73,17 @@ public static class DateTimeExtensions
|
||||
{
|
||||
return me.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
||||
}
|
||||
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static string yyyyMM(this DateTime me)
|
||||
{
|
||||
return me.ToString("yyyyMM");
|
||||
}
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static string yyyyMMdd(this DateTime me)
|
||||
{
|
||||
return me.ToString("yyyyMMdd");
|
||||
}
|
||||
}
|
||||
|
@ -27,3 +27,4 @@ public static class DbCommandExtensions
|
||||
return sql;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,3 +14,5 @@ public static class DecimalExtensions
|
||||
return dec;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -15,3 +15,5 @@ public static class EnumExtensions
|
||||
return (attrs.Length != 0 ? attrs[0].Description : Enum.GetName(t, e)) ?? "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -24,3 +24,5 @@ public static class EnumerableExtensions
|
||||
return me is null || !me.Any();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -41,3 +41,5 @@ public static class GenericExtensions
|
||||
return me.Equals(compare) ? ret : me;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -24,8 +24,15 @@ public static class IntExtensions
|
||||
return new Random(Guid.NewGuid().GetHashCode()).Next(me[0], me[1]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换成ipv4
|
||||
/// </summary>
|
||||
/// <param name="me"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToIpV4(this int me)
|
||||
{
|
||||
return string.Join(".", BitConverter.GetBytes(me).Reverse());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -64,3 +64,5 @@ public static class LoggerExtensions
|
||||
me.LogWarning(CallerInfoMessage(message, callerName, callerFilePath, callerLineNumber));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -25,3 +25,6 @@ public static class LongExtensions
|
||||
return new DateTime(1970, 1, 1).AddMilliseconds(msFrom1970).ToLocalTime();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -31,3 +31,6 @@ public static class ObjectExtensions
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -7,6 +7,21 @@ namespace NSExt.Extensions;
|
||||
|
||||
public static class StringExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// MD5 hmac编码
|
||||
/// </summary>
|
||||
/// <param name="me">字符串</param>
|
||||
/// <param name="key">密钥</param>
|
||||
/// <param name="e">字符串使用的编码</param>
|
||||
/// <returns>hash摘要的16进制文本形式(无连字符小写)</returns>
|
||||
private static string Md5Hmac(this string me, string key, Encoding e)
|
||||
{
|
||||
using var md5Hmac = new HMACMD5(e.GetBytes(key));
|
||||
return BitConverter.ToString(md5Hmac.ComputeHash(e.GetBytes(me)))
|
||||
.Replace("-", string.Empty)
|
||||
.ToLower(CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// base64编码
|
||||
/// </summary>
|
||||
@ -80,18 +95,6 @@ public static class StringExtensions
|
||||
return System.DateTime.ParseExact(me, format, CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换成日期对象
|
||||
/// </summary>
|
||||
/// <param name="me">待转换字符串</param>
|
||||
/// <param name="def">转换失败时返回的日期对象</param>
|
||||
/// <returns>转换后的日期对象</returns>
|
||||
public static DateTime DateTimeTry(this string me, DateTime def)
|
||||
{
|
||||
return !System.DateTime.TryParse(me, out var ret) ? def : ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换成日期对象
|
||||
/// </summary>
|
||||
@ -106,6 +109,18 @@ public static class StringExtensions
|
||||
: ret;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换成日期对象
|
||||
/// </summary>
|
||||
/// <param name="me">待转换字符串</param>
|
||||
/// <param name="def">转换失败时返回的日期对象</param>
|
||||
/// <returns>转换后的日期对象</returns>
|
||||
public static DateTime DateTimeTry(this string me, DateTime def)
|
||||
{
|
||||
return !System.DateTime.TryParse(me, out var ret) ? def : ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// string to decimal
|
||||
/// </summary>
|
||||
@ -186,6 +201,22 @@ public static class StringExtensions
|
||||
return e.GetBytes(me);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对一个字符串进行sha1 hash运算
|
||||
/// </summary>
|
||||
/// <param name="me">对一个字符串进行sha1 hash运算</param>
|
||||
/// <param name="secret">密钥</param>
|
||||
/// <param name="e">使用的编码</param>
|
||||
/// <returns>hash摘要的16进制文本形式(无连字符小写)</returns>
|
||||
public static string HmacSha1(this string me, string secret, Encoding e)
|
||||
{
|
||||
using var hmacSha1 = new HMACSHA1(e.GetBytes(secret));
|
||||
|
||||
return BitConverter.ToString(hmacSha1.ComputeHash(e.GetBytes(me)))
|
||||
.Replace("-", string.Empty)
|
||||
.ToLower(CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解码html编码
|
||||
/// </summary>
|
||||
@ -240,6 +271,35 @@ public static class StringExtensions
|
||||
return !long.TryParse(me, out var ret) ? def : ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ipv4格式转int32格式
|
||||
/// </summary>
|
||||
/// <param name="me"></param>
|
||||
/// <returns></returns>
|
||||
public static int IpV4ToInt32(this string me)
|
||||
{
|
||||
return BitConverter.ToInt32(me.Split('.').Select(byte.Parse).Reverse().ToArray(), 0);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 是否base64字符串
|
||||
/// </summary>
|
||||
/// <param name="me">字符串</param>
|
||||
/// <returns></returns>
|
||||
public static bool IsBase64String(this string me)
|
||||
{
|
||||
// 一个合法的Base64,有着以下特征:
|
||||
// 字符串的长度为4的整数倍。
|
||||
// 字符串的符号取值只能在A -Z, a -z, 0 -9, +, /, =共计65个字符中,且 = 如果出现就必须在结尾出现。
|
||||
if (!me.All(x => x.IsBase64Character())) return false;
|
||||
if (me.Length % 4 != 0) return false;
|
||||
var firstEqualSignPos = me.IndexOf('=');
|
||||
if (firstEqualSignPos < 0) return true;
|
||||
var lastEqualSignPos = me.LastIndexOf('=');
|
||||
return lastEqualSignPos == me.Length - 1 && me[firstEqualSignPos..lastEqualSignPos].All(x => x == '=');
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 将一个json字符串反序列化成为jObject对象
|
||||
@ -251,6 +311,11 @@ public static class StringExtensions
|
||||
return Newtonsoft.Json.Linq.JObject.Parse(me);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 中文姓名打马赛克
|
||||
/// </summary>
|
||||
/// <param name="me"></param>
|
||||
/// <returns></returns>
|
||||
public static string MaskChineseName(this string me)
|
||||
{
|
||||
if (me.Length == 2) return "*" + me[1..];
|
||||
@ -269,37 +334,6 @@ public static class StringExtensions
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 对一个字符串进行sha1 hash运算
|
||||
/// </summary>
|
||||
/// <param name="me">字符串</param>
|
||||
/// <param name="e">字符串使用的编码</param>
|
||||
/// <returns>hash摘要的16进制文本形式(无连字符小写)</returns>
|
||||
public static string Sha1(this string me, Encoding e)
|
||||
{
|
||||
using var sha1 = SHA1.Create();
|
||||
return BitConverter.ToString(sha1.ComputeHash(e.GetBytes(me)))
|
||||
.Replace("-", string.Empty)
|
||||
.ToLower(CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对一个字符串进行sha1 hash运算
|
||||
/// </summary>
|
||||
/// <param name="me">对一个字符串进行sha1 hash运算</param>
|
||||
/// <param name="secret">密钥</param>
|
||||
/// <param name="e">使用的编码</param>
|
||||
/// <returns>hash摘要的16进制文本形式(无连字符小写)</returns>
|
||||
public static string HmacSha1(this string me, string secret, Encoding e)
|
||||
{
|
||||
using var hmacSha1 = new HMACSHA1(e.GetBytes(secret));
|
||||
|
||||
return BitConverter.ToString(hmacSha1.ComputeHash(e.GetBytes(me)))
|
||||
.Replace("-", string.Empty)
|
||||
.ToLower(CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 对一个字符串进行md5hash运算
|
||||
/// </summary>
|
||||
@ -314,22 +348,6 @@ public static class StringExtensions
|
||||
.ToLower(CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// MD5 hmac编码
|
||||
/// </summary>
|
||||
/// <param name="me">字符串</param>
|
||||
/// <param name="key">密钥</param>
|
||||
/// <param name="e">字符串使用的编码</param>
|
||||
/// <returns>hash摘要的16进制文本形式(无连字符小写)</returns>
|
||||
private static string Md5Hmac(this string me, string key, Encoding e)
|
||||
{
|
||||
using var md5Hmac = new HMACMD5(e.GetBytes(key));
|
||||
return BitConverter.ToString(md5Hmac.ComputeHash(e.GetBytes(me)))
|
||||
.Replace("-", string.Empty)
|
||||
.ToLower(CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断字符串是否为null或不存在子元素(如果为集合对象);如果为空,返回指定的默认值,否则返回字符串本身
|
||||
/// </summary>
|
||||
@ -341,6 +359,17 @@ public static class StringExtensions
|
||||
return me.AsEnumerable().NullOrEmpty() ? defVal : me;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// null或空白字符
|
||||
/// </summary>
|
||||
/// <param name="me"></param>
|
||||
/// <returns></returns>
|
||||
public static bool NullOrWhiteSpace(this string me)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(me);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化一个文件获得指定类型的数据对象
|
||||
/// </summary>
|
||||
@ -382,6 +411,21 @@ public static class StringExtensions
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 对一个字符串进行sha1 hash运算
|
||||
/// </summary>
|
||||
/// <param name="me">字符串</param>
|
||||
/// <param name="e">字符串使用的编码</param>
|
||||
/// <returns>hash摘要的16进制文本形式(无连字符小写)</returns>
|
||||
public static string Sha1(this string me, Encoding e)
|
||||
{
|
||||
using var sha1 = SHA1.Create();
|
||||
return BitConverter.ToString(sha1.ComputeHash(e.GetBytes(me)))
|
||||
.Replace("-", string.Empty)
|
||||
.ToLower(CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 截取指定长度的字符串,代替substring
|
||||
/// </summary>
|
||||
@ -427,15 +471,5 @@ public static class StringExtensions
|
||||
{
|
||||
return Uri.UnescapeDataString(me);
|
||||
}
|
||||
|
||||
public static int IpV4ToInt32(this string me)
|
||||
{
|
||||
return BitConverter.ToInt32(me.Split('.').Select(byte.Parse).Reverse().ToArray(), 0);
|
||||
}
|
||||
|
||||
|
||||
public static bool NullOrWhiteSpace(this string me)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(me);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,3 +12,6 @@ public static class UriExtensions
|
||||
return "//" + me.Authority + me.PathAndQuery;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -8,8 +8,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0-rc.2.22472.3" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0-rc.2.22472.3"/>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta2"/>
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user