From 5719fbba78e99c9489bbecb5656630b96be48631 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E5=A4=B4?= Date: Mon, 16 Sep 2024 01:27:34 +0800 Subject: [PATCH] =?UTF-8?q?[imporv]=E4=BC=98=E5=8C=96=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E8=BD=AC=E4=B8=BA=E6=95=B0=E5=AD=97=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E5=86=85=E5=AD=98=E5=88=86=E9=85=8D=EF=BC=8C=E6=8F=90=E5=8D=87?= =?UTF-8?q?=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.Core/Common/Utility.cs | 81 ++++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 24 deletions(-) diff --git a/NewLife.Core/Common/Utility.cs b/NewLife.Core/Common/Utility.cs index 724b2e76e..2dbed6915 100644 --- a/NewLife.Core/Common/Utility.cs +++ b/NewLife.Core/Common/Utility.cs @@ -202,9 +202,15 @@ public virtual Int32 ToInt(Object? value, Int32 defaultValue) if (value is String str) { // 拷贝而来的逗号分隔整数 - str = str.Replace(",", null); - str = ToDBC(str).Trim(); - return str.IsNullOrEmpty() ? defaultValue : Int32.TryParse(str, out var n) ? n : defaultValue; + Span tmp = stackalloc Char[str.Length]; + var rs = TrimNumber(str.AsSpan(), tmp); + if (rs == 0) return defaultValue; + +#if NETCOREAPP || NETSTANDARD2_1_OR_GREATER + return Int32.TryParse(tmp[..rs], out var n) ? n : defaultValue; +#else + return Int32.TryParse(new String(tmp[..rs].ToArray()), out var n) ? n : defaultValue; +#endif } // 特殊处理时间,转Unix秒 @@ -278,9 +284,15 @@ public virtual Int64 ToLong(Object? value, Int64 defaultValue) if (value is String str) { // 拷贝而来的逗号分隔整数 - str = str.Replace(",", null); - str = ToDBC(str).Trim(); - return str.IsNullOrEmpty() ? defaultValue : Int64.TryParse(str, out var n) ? n : defaultValue; + Span tmp = stackalloc Char[str.Length]; + var rs = TrimNumber(str.AsSpan(), tmp); + if (rs == 0) return defaultValue; + +#if NETCOREAPP || NETSTANDARD2_1_OR_GREATER + return Int64.TryParse(tmp[..rs], out var n) ? n : defaultValue; +#else + return Int64.TryParse(new String(tmp[..rs].ToArray()), out var n) ? n : defaultValue; +#endif } // 特殊处理时间,转Unix毫秒 @@ -349,8 +361,15 @@ public virtual Double ToDouble(Object? value, Double defaultValue) // 特殊处理字符串,也是最常见的 if (value is String str) { - str = ToDBC(str).Trim(); - return str.IsNullOrEmpty() ? defaultValue : Double.TryParse(str, out var n) ? n : defaultValue; + Span tmp = stackalloc Char[str.Length]; + var rs = TrimNumber(str.AsSpan(), tmp); + if (rs == 0) return defaultValue; + +#if NETCOREAPP || NETSTANDARD2_1_OR_GREATER + return Double.TryParse(tmp[..rs], out var n) ? n : defaultValue; +#else + return Double.TryParse(new String(tmp[..rs].ToArray()), out var n) ? n : defaultValue; +#endif } if (value is Byte[] buf && buf.Length <= 8) @@ -383,8 +402,15 @@ public virtual Decimal ToDecimal(Object? value, Decimal defaultValue) // 特殊处理字符串,也是最常见的 if (value is String str) { - str = ToDBC(str).Trim(); - return str.IsNullOrEmpty() ? defaultValue : Decimal.TryParse(str, out var n) ? n : defaultValue; + Span tmp = stackalloc Char[str.Length]; + var rs = TrimNumber(str.AsSpan(), tmp); + if (rs == 0) return defaultValue; + +#if NETCOREAPP || NETSTANDARD2_1_OR_GREATER + return Decimal.TryParse(tmp[..rs], out var n) ? n : defaultValue; +#else + return Decimal.TryParse(new String(tmp[..rs].ToArray()), out var n) ? n : defaultValue; +#endif } if (value is Byte[] buf) @@ -450,7 +476,6 @@ public virtual Boolean ToBoolean(Object? value, Boolean defaultValue) // 特殊处理字符串,也是最常见的 if (value is String str) { - //str = ToDBC(str).Trim(); str = str.Trim(); if (str.IsNullOrEmpty()) return defaultValue; @@ -459,8 +484,6 @@ public virtual Boolean ToBoolean(Object? value, Boolean defaultValue) if (String.Equals(str, Boolean.TrueString, StringComparison.OrdinalIgnoreCase)) return true; if (String.Equals(str, Boolean.FalseString, StringComparison.OrdinalIgnoreCase)) return false; - // 特殊处理用数字0和1表示布尔型 - str = ToDBC(str); return Int32.TryParse(str, out var n) ? n > 0 : defaultValue; } @@ -604,22 +627,32 @@ public virtual DateTimeOffset ToDateTimeOffset(Object? value, DateTimeOffset def } } - /// 全角为半角 - /// 全角半角的关系是相差0xFEE0 - /// + /// 清理整数字符串,去掉常见分隔符,替换全角数字为半角数字 + /// + /// /// - private static String ToDBC(String str) + private static Int32 TrimNumber(ReadOnlySpan input, Span output) { - var ch = str.ToCharArray(); - for (var i = 0; i < ch.Length; i++) + var rs = 0; + for (var i = 0; i < input.Length; i++) { + // 去掉逗号分隔符 + var ch = input[i]; + if (ch == ',' || ch == '_' || ch == ' ') continue; + // 全角空格 - if (ch[i] == 0x3000) - ch[i] = (Char)0x20; - else if (ch[i] is > (Char)0xFF00 and < (Char)0xFF5F) - ch[i] = (Char)(ch[i] - 0xFEE0); + if (ch == 0x3000) + ch = (Char)0x20; + else if (ch is > (Char)0xFF00 and < (Char)0xFF5F) + ch = (Char)(input[i] - 0xFEE0); + + // 数字和小数点 以外字符,认为非数字 + if (ch is not '.' and (< '0' or > '9')) return 0; + + output[rs++] = ch; } - return new String(ch); + + return rs; } /// 去掉时间日期秒后面部分,可指定毫秒ms、秒s、分m、小时h