1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-07-01 05:45:59 +02:00
Beef/BeefLibs/corlib/src/UInt32.bf

192 lines
4.1 KiB
Beef
Raw Normal View History

2023-05-14 16:54:26 -03:00
using System.Globalization;
2019-08-23 11:56:54 -07:00
namespace System
{
2020-08-10 13:27:48 -07:00
#unwarn
struct UInt32 : uint32, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint32, ParseError>, IParseable<uint32>, IMinMaxValue<uint32>
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(uint32 partialResult);
}
2023-05-14 16:54:26 -03:00
public const uint32 MaxValue = 0xFFFFFFFFL;
public const uint32 MinValue = 0;
2019-08-23 11:56:54 -07:00
public static uint32 IMinMaxValue<uint32>.MinValue => MinValue;
public static uint32 IMinMaxValue<uint32>.MaxValue => MaxValue;
2019-08-23 11:56:54 -07:00
public static int operator<=>(Self a, Self b)
{
return (SelfBase)a <=> (SelfBase)b;
}
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;
}
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 override void ToString(String strBuffer)
{
// Dumb, make better.
char8[] strChars = scope:: char8[16];
int32 char8Idx = 14;
uint32 valLeft = (uint32)this;
while (valLeft > 0)
2019-08-23 11:56:54 -07:00
{
strChars[char8Idx] = (char8)('0' + (valLeft % 10));
valLeft /= 10;
char8Idx--;
2019-08-23 11:56:54 -07:00
}
if (char8Idx == 14)
strChars[char8Idx--] = '0';
char8* char8Ptr = &strChars[char8Idx + 1];
strBuffer.Append(char8Ptr);
2019-08-23 11:56:54 -07:00
}
void ToString(String strBuffer, int minNumerals)
{
// Dumb, make better.
char8[] strChars = scope:: char8[16];
int32 char8Idx = 14;
uint32 valLeft = (uint32)this;
2019-08-23 11:56:54 -07:00
int minNumeralsLeft = minNumerals;
while ((valLeft > 0) || (minNumeralsLeft > 0))
2019-08-23 11:56:54 -07:00
{
strChars[char8Idx] = (char8)('0' + (valLeft % 10));
valLeft /= 10;
char8Idx--;
2019-08-23 11:56:54 -07:00
minNumeralsLeft--;
}
if (char8Idx == 14)
strChars[char8Idx--] = '0';
char8* char8Ptr = &strChars[char8Idx + 1];
strBuffer.Append(char8Ptr);
2019-08-23 11:56:54 -07:00
}
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, (uint32)this, formatProvider, outString);
2019-08-23 11:56:54 -07:00
}
}
2023-05-14 16:54:26 -03:00
public static Result<uint32, 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
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;
2023-05-14 16:54:26 -03:00
uint32 result = 0;
uint32 prevResult = 0;
uint32 radix = style.HasFlag(.Hex) ? 0x10 : 10;
2023-05-14 16:54:26 -03:00
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
2023-05-14 16:54:26 -03:00
if ((c >= '0') && (c <= '9'))
2019-08-23 11:56:54 -07:00
{
2023-05-14 16:54:26 -03:00
result &*= radix;
result &+= (uint32)(c - '0');
2024-01-14 17:06:43 -06:00
digitsFound = true;
2019-08-23 11:56:54 -07:00
}
2023-05-14 16:54:26 -03:00
else if ((c >= 'a') && (c <= 'f'))
{
if (radix != 0x10)
return .Err(.InvalidChar(result));
result &*= radix;
result &+= (uint32)(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'))
2019-08-23 11:56:54 -07:00
{
2023-05-14 16:54:26 -03:00
if (radix != 0x10)
return .Err(.InvalidChar(result));
result &*= radix;
result &+= (uint32)(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<uint32, ParseError> IParseable<uint32, ParseError>.Parse(StringView val)
{
return Parse(val);
}
public static Result<uint32> IParseable<uint32>.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
}
}