mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-23 18:18:00 +02:00
Improvements to Number parsing
This commit is contained in:
parent
4c65652955
commit
5d28f8e1f0
10 changed files with 418 additions and 90 deletions
|
@ -9,11 +9,12 @@ namespace System
|
||||||
{
|
{
|
||||||
case Ok;
|
case Ok;
|
||||||
case NoValue;
|
case NoValue;
|
||||||
|
case Overflow;
|
||||||
case InvalidChar(int partialResult);
|
case InvalidChar(int partialResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
public const int MaxValue = (sizeof(uint) == 8) ? 0x7FFFFFFFFFFFFFFFL : 0x7FFFFFFF;
|
public const int MaxValue = (sizeof(int) == 8) ? 0x7FFFFFFFFFFFFFFFL : 0x7FFFFFFF;
|
||||||
public const int MinValue = (sizeof(uint) == 8) ? -0x8000000000000000L : -0x80000000;
|
public const int MinValue = (sizeof(int) == 8) ? -0x8000000000000000L : -0x80000000;
|
||||||
|
|
||||||
public static int operator<=>(Self a, Self b)
|
public static int operator<=>(Self a, Self b)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,10 +1,20 @@
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
namespace System
|
namespace System
|
||||||
{
|
{
|
||||||
#unwarn
|
#unwarn
|
||||||
struct Int16 : int16, IInteger, ISigned, IHashable, IFormattable, IIsNaN
|
struct Int16 : int16, IInteger, ISigned, IHashable, IFormattable, IIsNaN
|
||||||
{
|
{
|
||||||
public const int32 MaxValue = 0x7FFF;
|
public enum ParseError
|
||||||
public const int32 MinValue = -0x8000;
|
{
|
||||||
|
case Ok;
|
||||||
|
case NoValue;
|
||||||
|
case Overflow;
|
||||||
|
case InvalidChar(int16 partialResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
public const int16 MaxValue = 0x7FFF;
|
||||||
|
public const int16 MinValue = -0x8000;
|
||||||
|
|
||||||
public static int operator<=>(Self a, Self b)
|
public static int operator<=>(Self a, Self b)
|
||||||
{
|
{
|
||||||
|
@ -66,5 +76,68 @@ namespace System
|
||||||
NumberFormatter.NumberToString(format, (int32)this, formatProvider, outString);
|
NumberFormatter.NumberToString(format, (int32)this, formatProvider, outString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Result<int16, ParseError> Parse(StringView val, NumberStyles style = .Number, CultureInfo cultureInfo = null)
|
||||||
|
{
|
||||||
|
if (val.IsEmpty)
|
||||||
|
return .Err(.NoValue);
|
||||||
|
|
||||||
|
bool isNeg = false;
|
||||||
|
int16 result = 0;
|
||||||
|
|
||||||
|
int16 radix = style.HasFlag(.AllowHexSpecifier) ? 0x10 : 10;
|
||||||
|
|
||||||
|
for (int32 i = 0; i < val.Length; i++)
|
||||||
|
{
|
||||||
|
char8 c = val[i];
|
||||||
|
|
||||||
|
if ((i == 0) && (c == '-'))
|
||||||
|
{
|
||||||
|
isNeg = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((c >= '0') && (c <= '9'))
|
||||||
|
{
|
||||||
|
result &*= radix;
|
||||||
|
result &+= (int16)(c - '0');
|
||||||
|
}
|
||||||
|
else if ((c >= 'a') && (c <= 'f'))
|
||||||
|
{
|
||||||
|
if (radix != 0x10)
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
result &*= radix;
|
||||||
|
result &+= c - 'a' + 10;
|
||||||
|
}
|
||||||
|
else if ((c >= 'A') && (c <= 'F'))
|
||||||
|
{
|
||||||
|
if (radix != 0x10)
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
result &*= radix;
|
||||||
|
result &+= c - 'A' + 10;
|
||||||
|
}
|
||||||
|
else if ((c == 'X') || (c == 'x'))
|
||||||
|
{
|
||||||
|
if ((!style.HasFlag(.AllowHexSpecifier)) || (i == 0) || (result != 0))
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
radix = 0x10;
|
||||||
|
}
|
||||||
|
else if (c == '\'')
|
||||||
|
{
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
else if ((c == '+') && (i == 0))
|
||||||
|
{
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
|
||||||
|
if (isNeg ? (uint16)result > (uint16)MinValue : (uint16)result > (uint16)MaxValue)
|
||||||
|
return .Err(.Overflow);
|
||||||
|
}
|
||||||
|
|
||||||
|
return isNeg ? -result : result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ namespace System
|
||||||
{
|
{
|
||||||
case Ok;
|
case Ok;
|
||||||
case NoValue;
|
case NoValue;
|
||||||
|
case Overflow;
|
||||||
case InvalidChar(int32 partialResult);
|
case InvalidChar(int32 partialResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,7 +126,7 @@ namespace System
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Result<int32, ParseError> Parse(StringView val, NumberStyles style)
|
public static Result<int32, ParseError> Parse(StringView val, NumberStyles style = .Number, CultureInfo cultureInfo = null)
|
||||||
{
|
{
|
||||||
if (val.IsEmpty)
|
if (val.IsEmpty)
|
||||||
return .Err(.NoValue);
|
return .Err(.NoValue);
|
||||||
|
@ -147,26 +148,26 @@ namespace System
|
||||||
|
|
||||||
if ((c >= '0') && (c <= '9'))
|
if ((c >= '0') && (c <= '9'))
|
||||||
{
|
{
|
||||||
result *= radix;
|
result &*= radix;
|
||||||
result += (int32)(c - '0');
|
result &+= (int32)(c - '0');
|
||||||
}
|
}
|
||||||
else if ((c >= 'a') && (c <= 'f'))
|
else if ((c >= 'a') && (c <= 'f'))
|
||||||
{
|
{
|
||||||
if (radix != 0x10)
|
if (radix != 0x10)
|
||||||
return .Err(.InvalidChar(result));
|
return .Err(.InvalidChar(result));
|
||||||
result *= radix;
|
result &*= radix;
|
||||||
result += c - 'a' + 10;
|
result &+= c - 'a' + 10;
|
||||||
}
|
}
|
||||||
else if ((c >= 'A') && (c <= 'F'))
|
else if ((c >= 'A') && (c <= 'F'))
|
||||||
{
|
{
|
||||||
if (radix != 0x10)
|
if (radix != 0x10)
|
||||||
return .Err(.InvalidChar(result));
|
return .Err(.InvalidChar(result));
|
||||||
result *= radix;
|
result &*= radix;
|
||||||
result += c - 'A' + 10;
|
result &+= c - 'A' + 10;
|
||||||
}
|
}
|
||||||
else if ((c == 'X') || (c == 'x'))
|
else if ((c == 'X') || (c == 'x'))
|
||||||
{
|
{
|
||||||
if (result != 0)
|
if ((!style.HasFlag(.AllowHexSpecifier)) || (i == 0) || (result != 0))
|
||||||
return .Err(.InvalidChar(result));
|
return .Err(.InvalidChar(result));
|
||||||
radix = 0x10;
|
radix = 0x10;
|
||||||
}
|
}
|
||||||
|
@ -180,14 +181,12 @@ namespace System
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return .Err(.InvalidChar(result));
|
return .Err(.InvalidChar(result));
|
||||||
|
|
||||||
|
if (isNeg ? (uint32)result > (uint32)MinValue : (uint32)result > (uint32)MaxValue)
|
||||||
|
return .Err(.Overflow);
|
||||||
}
|
}
|
||||||
|
|
||||||
return isNeg ? -result : result;
|
return isNeg ? -result : result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Result<int32, ParseError> Parse(StringView val)
|
|
||||||
{
|
|
||||||
return Parse(val, .Any);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ namespace System
|
||||||
{
|
{
|
||||||
case Ok;
|
case Ok;
|
||||||
case NoValue;
|
case NoValue;
|
||||||
|
case Overflow;
|
||||||
case InvalidChar(int64 partialResult);
|
case InvalidChar(int64 partialResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +105,7 @@ namespace System
|
||||||
strBuffer.Append(char8Ptr);
|
strBuffer.Append(char8Ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Result<int64, ParseError> Parse(StringView val, NumberStyles style)
|
public static Result<int64, ParseError> Parse(StringView val, NumberStyles style = .Number, CultureInfo cultureInfo = null)
|
||||||
{
|
{
|
||||||
//TODO: Use Number.ParseNumber
|
//TODO: Use Number.ParseNumber
|
||||||
|
|
||||||
|
@ -128,22 +129,26 @@ namespace System
|
||||||
|
|
||||||
if ((c >= '0') && (c <= '9'))
|
if ((c >= '0') && (c <= '9'))
|
||||||
{
|
{
|
||||||
result *= radix;
|
result &*= radix;
|
||||||
result += (int32)(c - '0');
|
result &+= (int64)(c - '0');
|
||||||
}
|
}
|
||||||
else if ((c >= 'a') && (c <= 'f'))
|
else if ((c >= 'a') && (c <= 'f'))
|
||||||
{
|
{
|
||||||
result *= radix;
|
if (radix != 0x10)
|
||||||
result += c - 'a' + 10;
|
return .Err(.InvalidChar(result));
|
||||||
|
result &*= radix;
|
||||||
|
result &+= c - 'a' + 10;
|
||||||
}
|
}
|
||||||
else if ((c >= 'A') && (c <= 'F'))
|
else if ((c >= 'A') && (c <= 'F'))
|
||||||
{
|
{
|
||||||
result *= radix;
|
if (radix != 0x10)
|
||||||
result += c - 'A' + 10;
|
return .Err(.InvalidChar(result));
|
||||||
|
result &*= radix;
|
||||||
|
result &+= c - 'A' + 10;
|
||||||
}
|
}
|
||||||
else if ((c == 'X') || (c == 'x'))
|
else if ((c == 'X') || (c == 'x'))
|
||||||
{
|
{
|
||||||
if (result != 0)
|
if ((!style.HasFlag(.AllowHexSpecifier)) || (i == 0) || (result != 0))
|
||||||
return .Err(.InvalidChar(result));
|
return .Err(.InvalidChar(result));
|
||||||
radix = 0x10;
|
radix = 0x10;
|
||||||
}
|
}
|
||||||
|
@ -157,14 +162,12 @@ namespace System
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return .Err(.InvalidChar(result));
|
return .Err(.InvalidChar(result));
|
||||||
|
|
||||||
|
if (isNeg ? (uint64)result > (uint64)MinValue : (uint64)result > (uint64)MaxValue)
|
||||||
|
return .Err(.Overflow);
|
||||||
}
|
}
|
||||||
|
|
||||||
return isNeg ? -result : result;
|
return isNeg ? -result : result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Result<int64, ParseError> Parse(StringView val)
|
|
||||||
{
|
|
||||||
return Parse(val, .Any);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,20 @@
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
namespace System
|
namespace System
|
||||||
{
|
{
|
||||||
#unwarn
|
#unwarn
|
||||||
struct Int8 : int8, IInteger, ISigned, IHashable, IFormattable, IIsNaN
|
struct Int8 : int8, IInteger, ISigned, IHashable, IFormattable, IIsNaN
|
||||||
{
|
{
|
||||||
public const int32 MaxValue = 0x7F;
|
public enum ParseError
|
||||||
public const int32 MinValue = -0x80;
|
{
|
||||||
|
case Ok;
|
||||||
|
case NoValue;
|
||||||
|
case Overflow;
|
||||||
|
case InvalidChar(int8 partialResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
public const int8 MaxValue = 0x7F;
|
||||||
|
public const int8 MinValue = -0x80;
|
||||||
|
|
||||||
public static int operator<=>(Self a, Self b)
|
public static int operator<=>(Self a, Self b)
|
||||||
{
|
{
|
||||||
|
@ -66,5 +76,68 @@ namespace System
|
||||||
NumberFormatter.NumberToString(format, (int32)this, formatProvider, outString);
|
NumberFormatter.NumberToString(format, (int32)this, formatProvider, outString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Result<int8, ParseError> Parse(StringView val, NumberStyles style = .Number, CultureInfo cultureInfo = null)
|
||||||
|
{
|
||||||
|
if (val.IsEmpty)
|
||||||
|
return .Err(.NoValue);
|
||||||
|
|
||||||
|
bool isNeg = false;
|
||||||
|
int8 result = 0;
|
||||||
|
|
||||||
|
int8 radix = style.HasFlag(.AllowHexSpecifier) ? 0x10 : 10;
|
||||||
|
|
||||||
|
for (int32 i = 0; i < val.Length; i++)
|
||||||
|
{
|
||||||
|
char8 c = val[i];
|
||||||
|
|
||||||
|
if ((i == 0) && (c == '-'))
|
||||||
|
{
|
||||||
|
isNeg = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((c >= '0') && (c <= '9'))
|
||||||
|
{
|
||||||
|
result &*= radix;
|
||||||
|
result &+= (int8)(c - '0');
|
||||||
|
}
|
||||||
|
else if ((c >= 'a') && (c <= 'f'))
|
||||||
|
{
|
||||||
|
if (radix != 0x10)
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
result &*= radix;
|
||||||
|
result &+= (int8)(c - 'a' + 10);
|
||||||
|
}
|
||||||
|
else if ((c >= 'A') && (c <= 'F'))
|
||||||
|
{
|
||||||
|
if (radix != 0x10)
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
result &*= radix;
|
||||||
|
result &+= (int8)(c - 'A' + 10);
|
||||||
|
}
|
||||||
|
else if ((c == 'X') || (c == 'x'))
|
||||||
|
{
|
||||||
|
if ((!style.HasFlag(.AllowHexSpecifier)) || (i == 0) || (result != 0))
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
radix = 0x10;
|
||||||
|
}
|
||||||
|
else if (c == '\'')
|
||||||
|
{
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
else if ((c == '+') && (i == 0))
|
||||||
|
{
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
|
||||||
|
if (isNeg ? (uint8)result > (uint8)MaxValue + 1 : (uint8)result > (uint8)MaxValue)
|
||||||
|
return .Err(.Overflow);
|
||||||
|
}
|
||||||
|
|
||||||
|
return isNeg ? -result : result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,11 +7,12 @@ namespace System
|
||||||
{
|
{
|
||||||
case Ok;
|
case Ok;
|
||||||
case NoValue;
|
case NoValue;
|
||||||
|
case Overflow;
|
||||||
case InvalidChar(uint partialResult);
|
case InvalidChar(uint partialResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
public const uint64 MaxValue = (sizeof(uint) == 8) ? 0xFFFFFFFFFFFFFFFFUL : 0xFFFFFFFFL;
|
public const uint MaxValue = (sizeof(uint) == 8) ? 0xFFFFFFFFFFFFFFFFUL : 0xFFFFFFFFL;
|
||||||
public const uint64 MinValue = 0;
|
public const uint MinValue = 0;
|
||||||
|
|
||||||
public bool IsNull()
|
public bool IsNull()
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,9 +1,19 @@
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
namespace System
|
namespace System
|
||||||
{
|
{
|
||||||
#unwarn
|
#unwarn
|
||||||
struct UInt16 : uint16, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN
|
struct UInt16 : uint16, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN
|
||||||
{
|
{
|
||||||
public const uint16 MaxValue = (uint16)0xFFFF;
|
public enum ParseError
|
||||||
|
{
|
||||||
|
case Ok;
|
||||||
|
case NoValue;
|
||||||
|
case Overflow;
|
||||||
|
case InvalidChar(uint16 partialResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
public const uint16 MaxValue = 0xFFFF;
|
||||||
public const uint16 MinValue = 0;
|
public const uint16 MinValue = 0;
|
||||||
|
|
||||||
public static int operator<=>(Self a, Self b)
|
public static int operator<=>(Self a, Self b)
|
||||||
|
@ -66,5 +76,63 @@ namespace System
|
||||||
NumberFormatter.NumberToString(format, (uint32)this, formatProvider, outString);
|
NumberFormatter.NumberToString(format, (uint32)this, formatProvider, outString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Result<uint16, ParseError> Parse(StringView val, NumberStyles style = .Number, CultureInfo cultureInfo = null)
|
||||||
|
{
|
||||||
|
if (val.IsEmpty)
|
||||||
|
return .Err(.NoValue);
|
||||||
|
|
||||||
|
uint16 result = 0;
|
||||||
|
uint16 prevResult = 0;
|
||||||
|
|
||||||
|
uint16 radix = style.HasFlag(.AllowHexSpecifier) ? 0x10 : 10;
|
||||||
|
|
||||||
|
for (int32 i = 0; i < val.Length; i++)
|
||||||
|
{
|
||||||
|
char8 c = val[i];
|
||||||
|
|
||||||
|
if ((c >= '0') && (c <= '9'))
|
||||||
|
{
|
||||||
|
result &*= radix;
|
||||||
|
result &+= (uint16)(c - '0');
|
||||||
|
}
|
||||||
|
else if ((c >= 'a') && (c <= 'f'))
|
||||||
|
{
|
||||||
|
if (radix != 0x10)
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
result &*= radix;
|
||||||
|
result &+= (uint16)(c - 'a' + 10);
|
||||||
|
}
|
||||||
|
else if ((c >= 'A') && (c <= 'F'))
|
||||||
|
{
|
||||||
|
if (radix != 0x10)
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
result &*= radix;
|
||||||
|
result &+= (uint16)(c - 'A' + 10);
|
||||||
|
}
|
||||||
|
else if ((c == 'X') || (c == 'x'))
|
||||||
|
{
|
||||||
|
if ((!style.HasFlag(.AllowHexSpecifier)) || (i == 0) || (result != 0))
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
radix = 0x10;
|
||||||
|
}
|
||||||
|
else if (c == '\'')
|
||||||
|
{
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
else if ((c == '+') && (i == 0))
|
||||||
|
{
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
|
||||||
|
if (result < prevResult)
|
||||||
|
return .Err(.Overflow);
|
||||||
|
prevResult = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
namespace System
|
namespace System
|
||||||
{
|
{
|
||||||
#unwarn
|
#unwarn
|
||||||
|
@ -7,11 +9,12 @@ namespace System
|
||||||
{
|
{
|
||||||
case Ok;
|
case Ok;
|
||||||
case NoValue;
|
case NoValue;
|
||||||
|
case Overflow;
|
||||||
case InvalidChar(uint32 partialResult);
|
case InvalidChar(uint32 partialResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
public const uint MaxValue = 0xFFFFFFFFL;
|
public const uint32 MaxValue = 0xFFFFFFFFL;
|
||||||
public const uint MinValue = 0;
|
public const uint32 MinValue = 0;
|
||||||
|
|
||||||
public static int operator<=>(Self a, Self b)
|
public static int operator<=>(Self a, Self b)
|
||||||
{
|
{
|
||||||
|
@ -102,31 +105,62 @@ namespace System
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Result<uint32, ParseError> Parse(StringView val)
|
public static Result<uint32, ParseError> Parse(StringView val, NumberStyles style = .Number, CultureInfo cultureInfo = null)
|
||||||
{
|
{
|
||||||
if (val.Length == 0)
|
if (val.IsEmpty)
|
||||||
return .Err(.NoValue);
|
return .Err(.NoValue);
|
||||||
|
|
||||||
uint32 result = 0;
|
uint32 result = 0;
|
||||||
//TODO: Use Number.ParseNumber
|
uint32 prevResult = 0;
|
||||||
|
|
||||||
|
uint32 radix = style.HasFlag(.AllowHexSpecifier) ? 0x10 : 10;
|
||||||
|
|
||||||
for (int32 i = 0; i < val.Length; i++)
|
for (int32 i = 0; i < val.Length; i++)
|
||||||
{
|
{
|
||||||
char8 c = val.Ptr[i];
|
char8 c = val[i];
|
||||||
|
|
||||||
if ((i == 0) && (c == '-'))
|
|
||||||
{
|
|
||||||
return .Err(.InvalidChar(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((c >= '0') && (c <= '9'))
|
if ((c >= '0') && (c <= '9'))
|
||||||
{
|
{
|
||||||
result *= 10;
|
result &*= radix;
|
||||||
result += (uint32)(c - '0');
|
result &+= (uint32)(c - '0');
|
||||||
|
}
|
||||||
|
else if ((c >= 'a') && (c <= 'f'))
|
||||||
|
{
|
||||||
|
if (radix != 0x10)
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
result &*= radix;
|
||||||
|
result &+= (uint32)(c - 'a' + 10);
|
||||||
|
}
|
||||||
|
else if ((c >= 'A') && (c <= 'F'))
|
||||||
|
{
|
||||||
|
if (radix != 0x10)
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
result &*= radix;
|
||||||
|
result &+= (uint32)(c - 'A' + 10);
|
||||||
|
}
|
||||||
|
else if ((c == 'X') || (c == 'x'))
|
||||||
|
{
|
||||||
|
if ((!style.HasFlag(.AllowHexSpecifier)) || (i == 0) || (result != 0))
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
radix = 0x10;
|
||||||
|
}
|
||||||
|
else if (c == '\'')
|
||||||
|
{
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
else if ((c == '+') && (i == 0))
|
||||||
|
{
|
||||||
|
// Ignore
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return .Err(.InvalidChar(result));
|
return .Err(.InvalidChar(result));
|
||||||
|
|
||||||
|
if (result < prevResult)
|
||||||
|
return .Err(.Overflow);
|
||||||
|
prevResult = result;
|
||||||
}
|
}
|
||||||
return .Ok(result);
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,16 +5,17 @@ namespace System
|
||||||
#unwarn
|
#unwarn
|
||||||
struct UInt64 : uint64, IInteger, IUnsigned, IHashable, IIsNaN, IFormattable
|
struct UInt64 : uint64, IInteger, IUnsigned, IHashable, IIsNaN, IFormattable
|
||||||
{
|
{
|
||||||
public const uint64 MaxValue = 0xFFFFFFFFFFFFFFFFUL;
|
|
||||||
public const uint64 MinValue = 0;
|
|
||||||
|
|
||||||
public enum ParseError
|
public enum ParseError
|
||||||
{
|
{
|
||||||
case Ok;
|
case Ok;
|
||||||
case NoValue;
|
case NoValue;
|
||||||
|
case Overflow;
|
||||||
case InvalidChar(uint64 partialResult);
|
case InvalidChar(uint64 partialResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public const uint64 MaxValue = 0xFFFFFFFFFFFFFFFFUL;
|
||||||
|
public const uint64 MinValue = 0;
|
||||||
|
|
||||||
public static int operator<=>(UInt64 a, UInt64 b)
|
public static int operator<=>(UInt64 a, UInt64 b)
|
||||||
{
|
{
|
||||||
return (SelfBase)a <=> (SelfBase)b;
|
return (SelfBase)a <=> (SelfBase)b;
|
||||||
|
@ -86,57 +87,64 @@ namespace System
|
||||||
strBuffer.Append(char8Ptr);
|
strBuffer.Append(char8Ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Result<uint64, ParseError> Parse(StringView val, NumberStyles numberStyles = .Number, CultureInfo cultureInfo = null)
|
public static Result<uint64, ParseError> Parse(StringView val, NumberStyles style = .Number, CultureInfo cultureInfo = null)
|
||||||
{
|
{
|
||||||
if (val.Length == 0)
|
//TODO: Use Number.ParseNumber
|
||||||
|
|
||||||
|
if (val.IsEmpty)
|
||||||
return .Err(.NoValue);
|
return .Err(.NoValue);
|
||||||
|
|
||||||
uint64 result = 0;
|
uint64 result = 0;
|
||||||
if (numberStyles.HasFlag(.AllowHexSpecifier))
|
uint64 prevResult = 0;
|
||||||
{
|
|
||||||
int numDigits = 0;
|
|
||||||
|
|
||||||
for (int32 i = 0; i < val.Length; i++)
|
uint64 radix = style.HasFlag(.AllowHexSpecifier) ? 0x10 : 10;
|
||||||
{
|
|
||||||
char8 c = val.Ptr[i];
|
|
||||||
|
|
||||||
if (c == '\'')
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if ((c == 'X') || (c == 'x'))
|
|
||||||
{
|
|
||||||
if ((numDigits == 1) && (result == 0))
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
numDigits++;
|
|
||||||
if ((c >= '0') && (c <= '9'))
|
|
||||||
result = result*0x10 + (uint64)(c - '0');
|
|
||||||
else if ((c >= 'A') && (c <= 'F'))
|
|
||||||
result = result*0x10 + (uint64)(c - 'A') + 10;
|
|
||||||
else if ((c >= 'a') && (c <= 'f'))
|
|
||||||
result = result*0x10 + (uint64)(c - 'a') + 10;
|
|
||||||
else
|
|
||||||
return .Err(.InvalidChar(result));
|
|
||||||
}
|
|
||||||
|
|
||||||
return .Ok(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: Use Number.ParseNumber
|
|
||||||
for (int32 i = 0; i < val.Length; i++)
|
for (int32 i = 0; i < val.Length; i++)
|
||||||
{
|
{
|
||||||
char8 c = val.Ptr[i];
|
char8 c = val[i];
|
||||||
|
|
||||||
if ((c >= '0') && (c <= '9'))
|
if ((c >= '0') && (c <= '9'))
|
||||||
{
|
{
|
||||||
result *= 10;
|
result &*= radix;
|
||||||
result += (uint64)(c - '0');
|
result &+= (uint64)(c - '0');
|
||||||
|
}
|
||||||
|
else if ((c >= 'a') && (c <= 'f'))
|
||||||
|
{
|
||||||
|
if (radix != 0x10)
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
result &*= radix;
|
||||||
|
result &+= (uint64)(c - 'a' + 10);
|
||||||
|
}
|
||||||
|
else if ((c >= 'A') && (c <= 'F'))
|
||||||
|
{
|
||||||
|
if (radix != 0x10)
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
result &*= radix;
|
||||||
|
result &+= (uint64)(c - 'A' + 10);
|
||||||
|
}
|
||||||
|
else if ((c == 'X') || (c == 'x'))
|
||||||
|
{
|
||||||
|
if ((!style.HasFlag(.AllowHexSpecifier)) || (i == 0) || (result != 0))
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
radix = 0x10;
|
||||||
|
}
|
||||||
|
else if (c == '\'')
|
||||||
|
{
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
else if ((c == '+') && (i == 0))
|
||||||
|
{
|
||||||
|
// Ignore
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return .Err(.InvalidChar(result));
|
return .Err(.InvalidChar(result));
|
||||||
|
|
||||||
|
if (result < prevResult)
|
||||||
|
return .Err(.Overflow);
|
||||||
|
prevResult = result;
|
||||||
}
|
}
|
||||||
return .Ok(result);
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,19 @@
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
namespace System
|
namespace System
|
||||||
{
|
{
|
||||||
#unwarn
|
#unwarn
|
||||||
struct UInt8 : uint8, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN
|
struct UInt8 : uint8, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN
|
||||||
{
|
{
|
||||||
public const uint8 MaxValue = (uint8)0xFF;
|
public enum ParseError
|
||||||
|
{
|
||||||
|
case Ok;
|
||||||
|
case NoValue;
|
||||||
|
case Overflow;
|
||||||
|
case InvalidChar(uint8 partialResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
public const uint8 MaxValue = 0xFF;
|
||||||
public const uint8 MinValue = 0;
|
public const uint8 MinValue = 0;
|
||||||
|
|
||||||
public static int operator<=>(Self a, Self b)
|
public static int operator<=>(Self a, Self b)
|
||||||
|
@ -66,5 +76,63 @@ namespace System
|
||||||
NumberFormatter.NumberToString(format, (uint32)this, formatProvider, outString);
|
NumberFormatter.NumberToString(format, (uint32)this, formatProvider, outString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Result<uint8, ParseError> Parse(StringView val, NumberStyles style = .Number, CultureInfo cultureInfo = null)
|
||||||
|
{
|
||||||
|
if (val.IsEmpty)
|
||||||
|
return .Err(.NoValue);
|
||||||
|
|
||||||
|
uint8 result = 0;
|
||||||
|
uint8 prevResult = 0;
|
||||||
|
|
||||||
|
uint8 radix = style.HasFlag(.AllowHexSpecifier) ? 0x10 : 10;
|
||||||
|
|
||||||
|
for (int32 i = 0; i < val.Length; i++)
|
||||||
|
{
|
||||||
|
char8 c = val[i];
|
||||||
|
|
||||||
|
if ((c >= '0') && (c <= '9'))
|
||||||
|
{
|
||||||
|
result &*= radix;
|
||||||
|
result &+= (uint8)(c - '0');
|
||||||
|
}
|
||||||
|
else if ((c >= 'a') && (c <= 'f'))
|
||||||
|
{
|
||||||
|
if (radix != 0x10)
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
result &*= radix;
|
||||||
|
result &+= (uint8)(c - 'a' + 10);
|
||||||
|
}
|
||||||
|
else if ((c >= 'A') && (c <= 'F'))
|
||||||
|
{
|
||||||
|
if (radix != 0x10)
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
result &*= radix;
|
||||||
|
result &+= (uint8)(c - 'A' + 10);
|
||||||
|
}
|
||||||
|
else if ((c == 'X') || (c == 'x'))
|
||||||
|
{
|
||||||
|
if ((!style.HasFlag(.AllowHexSpecifier)) || (i == 0) || (result != 0))
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
radix = 0x10;
|
||||||
|
}
|
||||||
|
else if (c == '\'')
|
||||||
|
{
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
else if ((c == '+') && (i == 0))
|
||||||
|
{
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return .Err(.InvalidChar(result));
|
||||||
|
|
||||||
|
if (result < prevResult)
|
||||||
|
return .Err(.Overflow);
|
||||||
|
prevResult = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue