Merge branch 'dev' of https://github.com/nsnail/ns-ext into dev

This commit is contained in:
nsnail 2023-01-04 18:18:24 +08:00
commit 13f8ae51c2
4 changed files with 47 additions and 28 deletions

View File

@ -1,10 +1,10 @@
# ns-ext # ns-ext
[](README.zh-CN.md) | **En** [](README.zh-CN.md) | **En**
The **ns-ext** is a .NET extension function library, containing the following types of extension modules: The **ns-ext** is a .NET extension function library, containing the following types of extension modules:
| Features | File name | | Features | File name |
| -------- | ---------------------------------- | |------------------------------------------|------------------------------------|
| Byte type extension | ByteExtensions.cs | | Byte type extension | ByteExtensions.cs |
| Character Type Extensions | CharExtensions.cs | | Character Type Extensions | CharExtensions.cs |
| Date Type Extensions | DateTimeExtensions.cs | | Date Type Extensions | DateTimeExtensions.cs |

View File

@ -4,10 +4,16 @@ namespace NSExt.Constant;
// 使用 RegexGenerator 新特性会生成重复key值的xmlcomment导致出错 // 使用 RegexGenerator 新特性会生成重复key值的xmlcomment导致出错
internal static class Regexes internal static class Regexes
{ {
public static readonly Regex RegexBacksLantUnicode
= new("\\\\u([a-fA-F0-9]{4})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public static readonly Regex RegexHtmlTag = new("<[^>]*>", RegexOptions.Compiled | RegexOptions.IgnoreCase); public static readonly Regex RegexHtmlTag = new("<[^>]*>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public static readonly Regex RegexMobile public static readonly Regex RegexMobile
= new("^(\\d{3})\\d{4}(\\d{4})$", RegexOptions.Compiled | RegexOptions.IgnoreCase); = new("^(\\d{3})\\d{4}(\\d{4})$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public static readonly Regex RegexPercentUnicode
= new("\\\\u([a-fA-F0-9]{4})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public static readonly Regex RegexUpLetter = new("([A-Z])", RegexOptions.Compiled | RegexOptions.IgnoreCase); public static readonly Regex RegexUpLetter = new("([A-Z])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
} }

View File

@ -42,17 +42,20 @@ public static class ByteExtensions
/// <param name="me">me</param> /// <param name="me">me</param>
/// <param name="upperCase">是否大写</param> /// <param name="upperCase">是否大写</param>
/// <param name="splitShar">字节间分隔符</param> /// <param name="splitShar">字节间分隔符</param>
public static string String(this byte[] me, bool upperCase = true, string splitShar = null) /// <param name="splitInterval">分隔符跳跃字节数</param>
public static string String(this IEnumerable<byte> me, bool upperCase = true, string splitShar = ""
, int splitInterval = 1)
{ {
var ret = BitConverter.ToString(me); var sb = new StringBuilder();
if (!upperCase) { var i = 0;
ret = ret.ToLower(CultureInfo.InvariantCulture); foreach (var c in me.Select(x => x.ToString(upperCase ? "X2" : "x2", CultureInfo.InvariantCulture))) {
if (i++ % splitInterval == 0) {
sb.Append(splitShar);
} }
if (splitShar != "-") { sb.Append(c);
ret = ret.Replace("-", splitShar ?? string.Empty);
} }
return ret; return sb.ToString();
} }
} }

View File

@ -508,6 +508,16 @@ public static class StringExtensions
return ret == me ? ret : ret.TrimSpaces(); return ret == me ? ret : ret.TrimSpaces();
} }
/// <summary>
/// 将\ux0000 、 %u0000 、 &amp;#x0000; 编码转换成可读字符串
/// </summary>
public static string UnicodeDe(this string me)
{
const string replacement = "&#x$1;";
return me.Contains(@"\u") ? Regexes.RegexBacksLantUnicode.Replace(me, replacement).HtmlDe() :
me.Contains(@"%u") ? Regexes.RegexPercentUnicode.Replace(me, replacement).HtmlDe() : me.HtmlDe();
}
/// <summary> /// <summary>
/// url编码 /// url编码
/// </summary> /// </summary>