diff --git a/README.md b/README.md index c137c1f..0e53b79 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,27 @@ # ns-ext + [中](README.zh-CN.md) | **En** The **ns-ext** is a .NET extension function library, containing the following types of extension modules: - -| Features | File name | -| -------- | ---------------------------------- | -| Byte type extension | ByteExtensions.cs | -| Character Type Extensions | CharExtensions.cs | -| Date Type Extensions | DateTimeExtensions.cs | -| Database command type extension | DbCommandExtensions.cs | -| Decimal Number Type extension | DecimalExtensions.cs | -| Enumable type extension | EnumerableExtensions.cs | -| Enumeration type extension | EnumExtensions.cs | -| General type extension | GenericExtensions.cs | -| Integer type extension | IntExtensions.cs | +| Features | File name | +|------------------------------------------|------------------------------------| +| Byte type extension | ByteExtensions.cs | +| Character Type Extensions | CharExtensions.cs | +| Date Type Extensions | DateTimeExtensions.cs | +| Database command type extension | DbCommandExtensions.cs | +| Decimal Number Type extension | DecimalExtensions.cs | +| Enumable type extension | EnumerableExtensions.cs | +| Enumeration type extension | EnumExtensions.cs | +| General type extension | GenericExtensions.cs | +| Integer type extension | IntExtensions.cs | | Json Serialization option type extension | JsonSerializerOptionsExtensions.cs | -| Log type extension | LoggerExtensions.cs | -| Long integer extension | LongExtensions.cs | -| Object type extension | ObjectExtensions.cs | -| Stream type extension | StreamExtensions.cs | -| String type extension | StringExtensions.cs | -| Prototype type extension | TypeExtensions.cs | -| Resource locator type extension | UriExtensions.cs | +| Log type extension | LoggerExtensions.cs | +| Long integer extension | LongExtensions.cs | +| Object type extension | ObjectExtensions.cs | +| Stream type extension | StreamExtensions.cs | +| String type extension | StringExtensions.cs | +| Prototype type extension | TypeExtensions.cs | +| Resource locator type extension | UriExtensions.cs | ## Quick start diff --git a/src/Constant/Regexes.cs b/src/Constant/Regexes.cs index 5981ac8..ec823f0 100644 --- a/src/Constant/Regexes.cs +++ b/src/Constant/Regexes.cs @@ -4,10 +4,16 @@ namespace NSExt.Constant; // 使用 RegexGenerator 新特性会生成重复key值的xmlcomment导致出错 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 RegexMobile = 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); } \ No newline at end of file diff --git a/src/Extensions/ByteExtensions.cs b/src/Extensions/ByteExtensions.cs index 46cb98c..fcf562c 100644 --- a/src/Extensions/ByteExtensions.cs +++ b/src/Extensions/ByteExtensions.cs @@ -42,17 +42,20 @@ public static class ByteExtensions /// me /// 是否大写 /// 字节间分隔符 - public static string String(this byte[] me, bool upperCase = true, string splitShar = null) + /// 分隔符跳跃字节数 + public static string String(this IEnumerable me, bool upperCase = true, string splitShar = "" + , int splitInterval = 1) { - var ret = BitConverter.ToString(me); - if (!upperCase) { - ret = ret.ToLower(CultureInfo.InvariantCulture); + var sb = new StringBuilder(); + var i = 0; + foreach (var c in me.Select(x => x.ToString(upperCase ? "X2" : "x2", CultureInfo.InvariantCulture))) { + if (i++ % splitInterval == 0) { + sb.Append(splitShar); + } + + sb.Append(c); } - if (splitShar != "-") { - ret = ret.Replace("-", splitShar ?? string.Empty); - } - - return ret; + return sb.ToString(); } } \ No newline at end of file diff --git a/src/Extensions/StringExtensions.cs b/src/Extensions/StringExtensions.cs index 9c6d758..221ab85 100644 --- a/src/Extensions/StringExtensions.cs +++ b/src/Extensions/StringExtensions.cs @@ -508,6 +508,16 @@ public static class StringExtensions return ret == me ? ret : ret.TrimSpaces(); } + /// + /// 将\ux0000 、 %u0000 、 &#x0000; 编码转换成可读字符串 + /// + 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(); + } + /// /// url编码 ///