2019-09-27 13:03:47 -07:00
|
|
|
using System.Globalization;
|
|
|
|
|
2019-08-23 11:56:54 -07:00
|
|
|
namespace System
|
|
|
|
{
|
2020-08-10 13:27:48 -07:00
|
|
|
#unwarn
|
2024-02-08 20:16:29 -06:00
|
|
|
struct Int32 : int32, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable<int32, ParseError>, IParseable<int32>, IMinMaxValue<int32>
|
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(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
|
|
|
|
2024-02-08 20:16:29 -06:00
|
|
|
public static int32 IMinMaxValue<int32>.MinValue => MinValue;
|
|
|
|
public static int32 IMinMaxValue<int32>.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;
|
|
|
|
}
|
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:18:13 +02:00
|
|
|
public override void ToString(String strBuffer)
|
|
|
|
{
|
|
|
|
// Dumb, make better.
|
2025-01-13 20:30:03 +01:00
|
|
|
char8[16] strChars = ?;
|
2020-05-25 23:18:13 +02:00
|
|
|
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)
|
|
|
|
{
|
2022-01-11 10:36:22 -05:00
|
|
|
strChars[char8Idx] = (char8)('0' &- (valLeft % 10));
|
2020-05-25 23:18:13 +02:00
|
|
|
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];
|
2025-01-13 20:17:04 +01:00
|
|
|
strBuffer.Append(char8Ptr, 14 - char8Idx);
|
2020-05-25 23:18:13 +02:00
|
|
|
}
|
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)
|
|
|
|
{
|
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, (int32)this, formatProvider, outString);
|
2019-08-23 11:56:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-14 16:54:26 -03:00
|
|
|
public static Result<int32, ParseError> Parse(StringView val, NumberStyles style = .Number, CultureInfo cultureInfo = null)
|
2020-05-25 23:18:13 +02:00
|
|
|
{
|
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;
|
2024-01-14 17:06:43 -06:00
|
|
|
bool digitsFound = false;
|
2019-09-27 13:03:47 -07:00
|
|
|
int32 result = 0;
|
|
|
|
|
2023-06-22 07:52:43 -04:00
|
|
|
int32 radix = style.HasFlag(.Hex) ? 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'))
|
|
|
|
{
|
2023-05-14 16:54:26 -03:00
|
|
|
result &*= radix;
|
|
|
|
result &+= (int32)(c - '0');
|
2024-01-14 17:06:43 -06:00
|
|
|
digitsFound = true;
|
2019-08-23 11:56:54 -07:00
|
|
|
}
|
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));
|
2023-05-14 16:54:26 -03:00
|
|
|
result &*= radix;
|
|
|
|
result &+= c - 'a' + 10;
|
2024-01-14 17:06:43 -06:00
|
|
|
digitsFound = true;
|
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));
|
2023-05-14 16:54:26 -03:00
|
|
|
result &*= radix;
|
|
|
|
result &+= c - 'A' + 10;
|
2024-01-14 17:06:43 -06:00
|
|
|
digitsFound = true;
|
2019-09-27 13:03:47 -07:00
|
|
|
}
|
|
|
|
else if ((c == 'X') || (c == 'x'))
|
|
|
|
{
|
2023-05-14 16:54:26 -03:00
|
|
|
if ((!style.HasFlag(.AllowHexSpecifier)) || (i == 0) || (result != 0))
|
2019-09-27 13:03:47 -07:00
|
|
|
return .Err(.InvalidChar(result));
|
|
|
|
radix = 0x10;
|
2024-01-14 17:06:43 -06:00
|
|
|
digitsFound = false;
|
2019-09-27 13:03:47 -07:00
|
|
|
}
|
|
|
|
else if (c == '\'')
|
|
|
|
{
|
|
|
|
// Ignore
|
|
|
|
}
|
2022-02-15 17:28:12 -05:00
|
|
|
else if ((c == '+') && (i == 0))
|
|
|
|
{
|
|
|
|
// Ignore
|
|
|
|
}
|
2019-08-23 11:56:54 -07:00
|
|
|
else
|
2019-09-27 13:03:47 -07:00
|
|
|
return .Err(.InvalidChar(result));
|
2023-05-14 16:54:26 -03:00
|
|
|
|
2024-02-27 11:01:34 -05:00
|
|
|
if ((radix == 10) && (isNeg ? (uint32)result > (uint32)MinValue : (uint32)result > (uint32)MaxValue))
|
2023-05-14 16:54:26 -03:00
|
|
|
return .Err(.Overflow);
|
2019-08-23 11:56:54 -07:00
|
|
|
}
|
2019-09-27 13:03:47 -07:00
|
|
|
|
2024-01-14 17:06:43 -06:00
|
|
|
if (!digitsFound)
|
|
|
|
return .Err(.NoValue);
|
|
|
|
|
2019-09-27 13:03:47 -07:00
|
|
|
return isNeg ? -result : result;
|
2020-05-25 23:18:13 +02:00
|
|
|
}
|
2023-12-13 16:54:23 +01:00
|
|
|
|
|
|
|
public static Result<int32, ParseError> IParseable<int32, ParseError>.Parse(StringView val)
|
|
|
|
{
|
|
|
|
return Parse(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Result<int32> IParseable<int32>.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
|
|
|
}
|
|
|
|
}
|