2019-08-23 11:56:54 -07:00
|
|
|
namespace System
|
|
|
|
{
|
2020-08-10 13:27:48 -07:00
|
|
|
#unwarn
|
2020-08-29 07:10:04 -07:00
|
|
|
struct UInt32 : uint32, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN
|
2019-08-23 11:56:54 -07:00
|
|
|
{
|
|
|
|
public enum ParseError
|
|
|
|
{
|
|
|
|
case Ok;
|
|
|
|
case NoValue;
|
|
|
|
case InvalidChar(uint32 partialResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
public const uint MaxValue = 0xFFFFFFFFL;
|
|
|
|
public const uint MinValue = 0;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-20 22:01:18 +00:00
|
|
|
public static Self operator-(Self lhs, Self rhs)
|
|
|
|
{
|
|
|
|
return (SelfBase)lhs - (SelfBase)rhs;
|
|
|
|
}
|
|
|
|
|
2020-06-05 15:46:31 +02: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;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2020-10-28 23:17:51 +01:00
|
|
|
// 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
|
|
|
{
|
2020-10-28 23:17:51 +01:00
|
|
|
strChars[char8Idx] = (char8)('0' + (valLeft % 10));
|
|
|
|
valLeft /= 10;
|
|
|
|
char8Idx--;
|
2019-08-23 11:56:54 -07:00
|
|
|
}
|
2020-10-28 23:17:51 +01: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)
|
|
|
|
{
|
2020-10-28 23:17:51 +01:00
|
|
|
// 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;
|
2020-10-28 23:17:51 +01:00
|
|
|
while ((valLeft > 0) || (minNumeralsLeft > 0))
|
2019-08-23 11:56:54 -07:00
|
|
|
{
|
2020-10-28 23:17:51 +01:00
|
|
|
strChars[char8Idx] = (char8)('0' + (valLeft % 10));
|
|
|
|
valLeft /= 10;
|
|
|
|
char8Idx--;
|
2019-08-23 11:56:54 -07:00
|
|
|
minNumeralsLeft--;
|
2020-10-28 23:17:51 +01:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2020-05-31 11:55:36 -07:00
|
|
|
if(format == null || format.IsEmpty)
|
2019-08-23 11:56:54 -07:00
|
|
|
{
|
2020-05-31 11:55:36 -07:00
|
|
|
ToString(outString);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NumberFormatter.NumberToString(format, (uint32)this, formatProvider, outString);
|
2019-08-23 11:56:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Result<uint32, ParseError> Parse(StringView val)
|
|
|
|
{
|
|
|
|
if (val.Length == 0)
|
|
|
|
return .Err(.NoValue);
|
|
|
|
|
|
|
|
uint32 result = 0;
|
|
|
|
//TODO: Use Number.ParseNumber
|
|
|
|
for (int32 i = 0; i < val.Length; i++)
|
|
|
|
{
|
|
|
|
char8 c = val.Ptr[i];
|
|
|
|
|
|
|
|
if ((i == 0) && (c == '-'))
|
|
|
|
{
|
|
|
|
return .Err(.InvalidChar(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((c >= '0') && (c <= '9'))
|
|
|
|
{
|
|
|
|
result *= 10;
|
|
|
|
result += (uint32)(c - '0');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return .Err(.InvalidChar(result));
|
|
|
|
}
|
|
|
|
return .Ok(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|