1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 19:48:20 +02:00

Merge pull request #1452 from disarray2077/patch-1

Improve `Float.Parse`
This commit is contained in:
Brian Fiete 2022-02-15 11:25:41 -08:00 committed by GitHub
commit d3cf94abfe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -166,10 +166,30 @@ namespace System
public static Result<float> Parse(StringView val)
{
bool isNeg = false;
double result = 0;
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 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
for (int32 i = 0; i < val.Length; i++)
{
@ -186,7 +206,7 @@ namespace System
break;
}
if (c == '.')
if (c == info.NumberDecimalSeparator[0])
{
if (decimalMultiplier != 0)
return .Err;
@ -207,12 +227,6 @@ namespace System
continue;
}
if ((i == 0) && (c == '-'))
{
isNeg = true;
continue;
}
if ((c >= '0') && (c <= '9'))
{
result *= 10;