1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 11:38:21 +02:00
Beef/BeefLibs/corlib/src/UInt64.bf

174 lines
3.7 KiB
Beef
Raw Normal View History

2019-08-23 11:56:54 -07:00
using System.Globalization;
namespace System
{
2020-08-10 13:27:48 -07:00
#unwarn
struct UInt64 : uint64, IInteger, IUnsigned, IHashable, IIsNaN, IFormattable, IParseable<uint64, ParseError>, IParseable<uint64>, IMinMaxValue<uint64>
2019-08-23 11:56:54 -07:00
{
public enum ParseError
{
case Ok;
case NoValue;
2023-05-14 16:54:26 -03:00
case Overflow;
2019-08-23 11:56:54 -07:00
case InvalidChar(uint64 partialResult);
}
2023-05-14 16:54:26 -03:00
public const uint64 MaxValue = 0xFFFFFFFFFFFFFFFFUL;
public const uint64 MinValue = 0;
public static uint64 IMinMaxValue<uint64>.MinValue => MinValue;
public static uint64 IMinMaxValue<uint64>.MaxValue => MaxValue;
2019-08-23 11:56:54 -07:00
public static int operator<=>(UInt64 a, UInt64 b)
{
2020-01-28 10:58:52 -08:00
return (SelfBase)a <=> (SelfBase)b;
}
public static Self operator+(Self lhs, Self rhs)
{
return (SelfBase)lhs + (SelfBase)rhs;
2019-08-23 11:56:54 -07:00
}
public static Self operator-(Self lhs, Self rhs)
{
return (SelfBase)lhs - (SelfBase)rhs;
}
public static Self operator*(Self lhs, Self rhs)
{
return (SelfBase)lhs * (SelfBase)rhs;
}
public static Self operator/(Self lhs, Self rhs)
{
return (SelfBase)lhs / (SelfBase)rhs;
}
2020-02-18 08:41:14 -08:00
public int GetHashCode()
2019-08-23 11:56:54 -07:00
{
return (int)this;
}
bool IIsNaN.IsNaN
{
[SkipCall]
get
{
return false;
}
}
public void ToString(String outString, String format, IFormatProvider formatProvider)
{
if(format == null || format.IsEmpty)
2019-08-23 11:56:54 -07:00
{
ToString(outString);
}
else
{
NumberFormatter.NumberToString(format, (uint64)this, formatProvider, outString);
2019-08-23 11:56:54 -07:00
}
}
public override void ToString(String strBuffer)
{
// Dumb, make better.
char8[] strChars = scope:: char8[22];
int32 char8Idx = 20;
uint64 valLeft = (uint64)this;
while (valLeft > 0)
{
strChars[char8Idx] = (char8)('0' + (valLeft % 10));
valLeft /= 10;
char8Idx--;
}
if (char8Idx == 20)
strChars[char8Idx--] = '0';
char8* char8Ptr = &strChars[char8Idx + 1];
strBuffer.Append(char8Ptr);
}
2023-05-14 16:54:26 -03:00
public static Result<uint64, ParseError> Parse(StringView val, NumberStyles style = .Number, CultureInfo cultureInfo = null)
2019-08-23 11:56:54 -07:00
{
2023-05-14 16:54:26 -03:00
//TODO: Use Number.ParseNumber
if (val.IsEmpty)
2019-08-23 11:56:54 -07:00
return .Err(.NoValue);
2024-01-14 17:06:43 -06:00
bool digitsFound = false;
2019-08-23 11:56:54 -07:00
uint64 result = 0;
2023-05-14 16:54:26 -03:00
uint64 prevResult = 0;
2019-08-23 11:56:54 -07:00
uint64 radix = style.HasFlag(.Hex) ? 0x10 : 10;
2019-08-23 11:56:54 -07:00
for (int32 i = 0; i < val.Length; i++)
{
2023-05-14 16:54:26 -03:00
char8 c = val[i];
2019-08-23 11:56:54 -07:00
if ((c >= '0') && (c <= '9'))
{
2023-05-14 16:54:26 -03:00
result &*= radix;
result &+= (uint64)(c - '0');
2024-01-14 17:06:43 -06:00
digitsFound = true;
2023-05-14 16:54:26 -03:00
}
else if ((c >= 'a') && (c <= 'f'))
{
if (radix != 0x10)
return .Err(.InvalidChar(result));
result &*= radix;
result &+= (uint64)(c - 'a' + 10);
2024-01-14 17:06:43 -06:00
digitsFound = true;
2023-05-14 16:54:26 -03:00
}
else if ((c >= 'A') && (c <= 'F'))
{
if (radix != 0x10)
return .Err(.InvalidChar(result));
result &*= radix;
result &+= (uint64)(c - 'A' + 10);
2024-01-14 17:06:43 -06:00
digitsFound = true;
2023-05-14 16:54:26 -03:00
}
else if ((c == 'X') || (c == 'x'))
{
if ((!style.HasFlag(.AllowHexSpecifier)) || (i == 0) || (result != 0))
return .Err(.InvalidChar(result));
radix = 0x10;
2024-01-14 17:06:43 -06:00
digitsFound = false;
2023-05-14 16:54:26 -03:00
}
else if (c == '\'')
{
// Ignore
}
else if ((c == '+') && (i == 0))
{
// Ignore
2019-08-23 11:56:54 -07:00
}
else
return .Err(.InvalidChar(result));
2023-05-14 16:54:26 -03:00
if (result < prevResult)
return .Err(.Overflow);
prevResult = result;
2019-08-23 11:56:54 -07:00
}
2023-05-14 16:54:26 -03:00
2024-01-14 17:06:43 -06:00
if (!digitsFound)
return .Err(.NoValue);
2023-05-14 16:54:26 -03:00
return result;
2019-08-23 11:56:54 -07:00
}
public static Result<uint64, ParseError> IParseable<uint64, ParseError>.Parse(StringView val)
{
return Parse(val);
}
public static Result<uint64> IParseable<uint64>.Parse(StringView val)
{
var res = Parse(val);
if(res case .Err)
return .Err;
else
return .Ok(res.Value);
}
2019-08-23 11:56:54 -07:00
}
}