Skip to content

Commit

Permalink
Merge branch 'dev' of http://git.newlifex.com/NewLife/X into dev
Browse files Browse the repository at this point in the history
* 'dev' of http://git.newlifex.com/NewLife/X:
  简化TrimNumber逻辑,改进负号处理
  • Loading branch information
猿人易 committed Sep 20, 2024
2 parents 699e8ce + a3d8813 commit ee021be
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions NewLife.Core/Common/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -633,24 +633,15 @@ public virtual DateTimeOffset ToDateTimeOffset(Object? value, DateTimeOffset def
private static Int32 TrimNumber(ReadOnlySpan<Char> input, Span<Char> output)
{
var rs = 0;
var hasNegativeSign = false;

for (var i = 0; i < input.Length; i++)
{
// 去掉逗号分隔符
var ch = input[i];
if (ch == ',' || ch == '_' || ch == ' ') continue;

// 检查负号
if (ch == '-')
{
if (rs == 0) // 负号只能在开头
{
output[rs++] = ch;
hasNegativeSign = true;
}
continue; // 跳过负号的后续处理
}
// 支持负数
if (ch == '-' && rs > 0) return 0;

// 全角空格
if (ch == 0x3000)
Expand All @@ -659,14 +650,11 @@ private static Int32 TrimNumber(ReadOnlySpan<Char> input, Span<Char> output)
ch = (Char)(input[i] - 0xFEE0);

// 数字和小数点 以外字符,认为非数字
if (ch is not '.' and (< '0' or > '9')) return 0;
if (ch is not '.' and not '-' and (< '0' or > '9')) return 0;

output[rs++] = ch;
}

// 如果没有有效的数字,返回0
if (rs == 0 && !hasNegativeSign) return 0;

return rs;
}

Expand Down

0 comments on commit ee021be

Please sign in to comment.