mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-09 03:52:19 +02:00
Merge pull request #1452 from disarray2077/patch-1
Improve `Float.Parse`
This commit is contained in:
commit
d3cf94abfe
1 changed files with 24 additions and 10 deletions
|
@ -166,10 +166,30 @@ namespace System
|
||||||
|
|
||||||
public static Result<float> Parse(StringView val)
|
public static Result<float> Parse(StringView val)
|
||||||
{
|
{
|
||||||
bool isNeg = false;
|
return Parse(val, NumberFormatInfo.CurrentInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Result<float> Parse(StringView val, NumberFormatInfo info)
|
||||||
|
{
|
||||||
|
if (val.IsEmpty)
|
||||||
|
return .Err;
|
||||||
|
|
||||||
|
bool isNeg = val[0] == '-';
|
||||||
|
bool isPos = val[0] == '+';
|
||||||
double result = 0;
|
double result = 0;
|
||||||
double decimalMultiplier = 0;
|
double decimalMultiplier = 0;
|
||||||
|
|
||||||
|
var val;
|
||||||
|
if (isNeg || isPos)
|
||||||
|
val.RemoveFromStart(1);
|
||||||
|
|
||||||
|
if (@val.Equals(info.NegativeInfinitySymbol, true))
|
||||||
|
return NegativeInfinity;
|
||||||
|
else if (val.Equals(info.PositiveInfinitySymbol, true))
|
||||||
|
return PositiveInfinity;
|
||||||
|
else if (val.Equals(info.NaNSymbol, true))
|
||||||
|
return NaN;
|
||||||
|
|
||||||
//TODO: Use Number.ParseNumber
|
//TODO: Use Number.ParseNumber
|
||||||
for (int32 i = 0; i < val.Length; i++)
|
for (int32 i = 0; i < val.Length; i++)
|
||||||
{
|
{
|
||||||
|
@ -186,7 +206,7 @@ namespace System
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c == '.')
|
if (c == info.NumberDecimalSeparator[0])
|
||||||
{
|
{
|
||||||
if (decimalMultiplier != 0)
|
if (decimalMultiplier != 0)
|
||||||
return .Err;
|
return .Err;
|
||||||
|
@ -207,12 +227,6 @@ namespace System
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((i == 0) && (c == '-'))
|
|
||||||
{
|
|
||||||
isNeg = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((c >= '0') && (c <= '9'))
|
if ((c >= '0') && (c <= '9'))
|
||||||
{
|
{
|
||||||
result *= 10;
|
result *= 10;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue