2019-09-27 13:03:47 -07:00
|
|
|
using System.Globalization;
|
|
|
|
|
2019-08-23 11:56:54 -07:00
|
|
|
namespace System
|
|
|
|
{
|
2020-03-20 22:01:18 +00:00
|
|
|
struct Int32 : int32, IInteger, ISigned, IHashable, IFormattable, IOpComparable, IIsNaN, IOpNegatable, IOpAddable, IOpSubtractable
|
2019-08-23 11:56:54 -07:00
|
|
|
{
|
|
|
|
public enum ParseError
|
|
|
|
{
|
|
|
|
case Ok;
|
|
|
|
case NoValue;
|
|
|
|
case InvalidChar(int32 partialResult);
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:18:13 +02:00
|
|
|
public const int32 MaxValue = 0x7FFFFFFF;
|
|
|
|
public const int32 MinValue = -0x80000000;
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-20 22:01:18 +00:00
|
|
|
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 value)
|
|
|
|
{
|
|
|
|
return (SelfBase)value;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:18:13 +02:00
|
|
|
public override void ToString(String strBuffer)
|
|
|
|
{
|
|
|
|
// Dumb, make better.
|
|
|
|
char8[] strChars = scope:: char8[16];
|
|
|
|
int32 char8Idx = 14;
|
|
|
|
int32 valLeft = (int32)this;
|
2020-05-25 22:32:15 +02:00
|
|
|
bool isNeg = true;
|
|
|
|
if (valLeft >= 0)
|
2019-08-23 11:56:54 -07:00
|
|
|
{
|
|
|
|
valLeft = -valLeft;
|
2020-05-25 22:32:15 +02:00
|
|
|
isNeg = false;
|
2019-08-23 11:56:54 -07:00
|
|
|
}
|
2020-05-25 23:18:13 +02:00
|
|
|
while (valLeft < 0)
|
|
|
|
{
|
|
|
|
strChars[char8Idx] = (char8)('0' - (valLeft % 10));
|
|
|
|
valLeft /= 10;
|
|
|
|
char8Idx--;
|
|
|
|
}
|
|
|
|
if (char8Idx == 14)
|
|
|
|
strChars[char8Idx--] = '0';
|
2019-08-23 11:56:54 -07:00
|
|
|
if (isNeg)
|
|
|
|
strChars[char8Idx--] = '-';
|
2020-05-25 23:18:13 +02:00
|
|
|
char8* char8Ptr = &strChars[char8Idx + 1];
|
|
|
|
strBuffer.Append(char8Ptr);
|
|
|
|
}
|
2019-08-23 11:56:54 -07:00
|
|
|
|
|
|
|
void ToString(String strBuffer, int minNumerals)
|
|
|
|
{
|
2020-05-25 23:18:13 +02:00
|
|
|
// Dumb, make better.
|
|
|
|
char8[] strChars = scope:: char8[16];
|
|
|
|
int32 char8Idx = 14;
|
|
|
|
int32 valLeft = (int32)this;
|
2020-05-25 22:32:15 +02:00
|
|
|
bool isNeg = true;
|
2019-08-23 11:56:54 -07:00
|
|
|
int minNumeralsLeft = minNumerals;
|
2020-05-25 22:32:15 +02:00
|
|
|
if (valLeft >= 0)
|
2019-08-23 11:56:54 -07:00
|
|
|
{
|
|
|
|
valLeft = -valLeft;
|
2020-05-25 22:32:15 +02:00
|
|
|
isNeg = false;
|
2019-08-23 11:56:54 -07:00
|
|
|
}
|
2020-05-25 23:18:13 +02:00
|
|
|
while ((valLeft < 0) || (minNumeralsLeft > 0))
|
|
|
|
{
|
|
|
|
strChars[char8Idx] = (char8)('0' - (valLeft % 10));
|
|
|
|
valLeft /= 10;
|
|
|
|
char8Idx--;
|
2019-08-23 11:56:54 -07:00
|
|
|
minNumeralsLeft--;
|
2020-05-25 23:18:13 +02:00
|
|
|
}
|
|
|
|
if (char8Idx == 14)
|
|
|
|
strChars[char8Idx--] = '0';
|
2019-08-23 11:56:54 -07:00
|
|
|
if (isNeg)
|
|
|
|
strChars[char8Idx--] = '-';
|
2020-05-25 23:18:13 +02:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
int minNumerals = -1;
|
|
|
|
|
|
|
|
//TOTAL HACK:
|
|
|
|
if (format != null)
|
|
|
|
{
|
|
|
|
if (format.StartsWith("X"))
|
|
|
|
{
|
|
|
|
((UInt64)(uint32)this).ToString(outString, format, formatProvider);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((format.Length > 0) && (format[0] == '0'))
|
|
|
|
{
|
|
|
|
if (Int32.Parse(format) case .Ok(let wantLen))
|
2020-05-25 23:18:13 +02:00
|
|
|
{
|
2019-08-23 11:56:54 -07:00
|
|
|
minNumerals = wantLen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ToString(outString, minNumerals);
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:18:13 +02:00
|
|
|
public static Result<int32, ParseError> Parse(StringView val, NumberStyles style)
|
|
|
|
{
|
2019-09-27 13:03:47 -07:00
|
|
|
if (val.IsEmpty)
|
2019-08-23 11:56:54 -07:00
|
|
|
return .Err(.NoValue);
|
|
|
|
|
|
|
|
bool isNeg = false;
|
2019-09-27 13:03:47 -07:00
|
|
|
int32 result = 0;
|
|
|
|
|
|
|
|
int32 radix = style.HasFlag(.AllowHexSpecifier) ? 0x10 : 10;
|
2020-05-25 23:18:13 +02:00
|
|
|
|
2019-08-23 11:56:54 -07:00
|
|
|
for (int32 i = 0; i < val.Length; i++)
|
|
|
|
{
|
2019-09-27 13:03:47 -07:00
|
|
|
char8 c = val[i];
|
2019-08-23 11:56:54 -07:00
|
|
|
|
|
|
|
if ((i == 0) && (c == '-'))
|
|
|
|
{
|
|
|
|
isNeg = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((c >= '0') && (c <= '9'))
|
|
|
|
{
|
2019-09-27 13:03:47 -07:00
|
|
|
result *= radix;
|
2019-08-23 11:56:54 -07:00
|
|
|
result += (int32)(c - '0');
|
|
|
|
}
|
2019-09-27 13:03:47 -07:00
|
|
|
else if ((c >= 'a') && (c <= 'f'))
|
|
|
|
{
|
2020-04-10 08:58:29 -07:00
|
|
|
if (radix != 0x10)
|
|
|
|
return .Err(.InvalidChar(result));
|
2019-09-27 13:03:47 -07:00
|
|
|
result *= radix;
|
|
|
|
result += c - 'a' + 10;
|
|
|
|
}
|
|
|
|
else if ((c >= 'A') && (c <= 'F'))
|
|
|
|
{
|
2020-04-10 08:58:29 -07:00
|
|
|
if (radix != 0x10)
|
|
|
|
return .Err(.InvalidChar(result));
|
2019-09-27 13:03:47 -07:00
|
|
|
result *= radix;
|
|
|
|
result += c - 'A' + 10;
|
|
|
|
}
|
|
|
|
else if ((c == 'X') || (c == 'x'))
|
|
|
|
{
|
|
|
|
if (result != 0)
|
|
|
|
return .Err(.InvalidChar(result));
|
|
|
|
radix = 0x10;
|
|
|
|
}
|
|
|
|
else if (c == '\'')
|
|
|
|
{
|
|
|
|
// Ignore
|
|
|
|
}
|
2019-08-23 11:56:54 -07:00
|
|
|
else
|
2019-09-27 13:03:47 -07:00
|
|
|
return .Err(.InvalidChar(result));
|
2019-08-23 11:56:54 -07:00
|
|
|
}
|
2019-09-27 13:03:47 -07:00
|
|
|
|
|
|
|
return isNeg ? -result : result;
|
2020-05-25 23:18:13 +02:00
|
|
|
}
|
2019-09-27 13:03:47 -07:00
|
|
|
|
|
|
|
public static Result<int32, ParseError> Parse(StringView val)
|
|
|
|
{
|
|
|
|
return Parse(val, .Any);
|
|
|
|
}
|
2019-08-23 11:56:54 -07:00
|
|
|
}
|
|
|
|
}
|