Skip to content

Commit 949e6ff

Browse files
committed
Revise INumber conversion methods
1 parent 6ffb894 commit 949e6ff

File tree

1 file changed

+28
-36
lines changed

1 file changed

+28
-36
lines changed

src/Nethermind.Int256/UInt256.cs

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,8 +2034,6 @@ public static bool TryParse(ReadOnlySpan<char> value, out UInt256 result) => val
20342034
? TryParse(value.Slice(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out result)
20352035
: TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
20362036

2037-
//public static bool TryParse(string value, NumberStyles style, IFormatProvider provider, out UInt256 result) => TryParse(value.AsSpan(), style, provider, out result);
2038-
20392037
public static bool TryParse(in ReadOnlySpan<char> value, NumberStyles style, IFormatProvider? provider, out UInt256 result)
20402038
{
20412039
BigInteger a;
@@ -2166,12 +2164,6 @@ static bool INumberBase<UInt256>.TryConvertFromChecked<TOther>(TOther value, out
21662164
result = actualValue;
21672165
return true;
21682166
}
2169-
else if (typeof(TOther) == typeof(decimal))
2170-
{
2171-
decimal actualValue = (decimal)(object)value;
2172-
result = checked((ulong)actualValue);
2173-
return true;
2174-
}
21752167
else if (typeof(TOther) == typeof(ushort))
21762168
{
21772169
ushort actualValue = (ushort)(object)value;
@@ -2187,7 +2179,7 @@ static bool INumberBase<UInt256>.TryConvertFromChecked<TOther>(TOther value, out
21872179
else if (typeof(TOther) == typeof(UInt128))
21882180
{
21892181
UInt128 actualValue = (UInt128)(object)value;
2190-
result = checked((ulong)actualValue);
2182+
result = actualValue;
21912183
return true;
21922184
}
21932185
else if (typeof(TOther) == typeof(nuint))
@@ -2217,6 +2209,12 @@ static bool INumberBase<UInt256>.TryConvertFromSaturating<TOther>(TOther value,
22172209
result = actualValue;
22182210
return true;
22192211
}
2212+
else if (typeof(TOther) == typeof(decimal))
2213+
{
2214+
decimal actualValue = (decimal)(object)value;
2215+
result = (actualValue < 0) ? MinValue : (UInt128)actualValue;
2216+
return true;
2217+
}
22202218
else if (typeof(TOther) == typeof(ushort))
22212219
{
22222220
ushort actualValue = (ushort)(object)value;
@@ -2229,10 +2227,16 @@ static bool INumberBase<UInt256>.TryConvertFromSaturating<TOther>(TOther value,
22292227
result = actualValue;
22302228
return true;
22312229
}
2230+
else if (typeof(TOther) == typeof(ulong))
2231+
{
2232+
ulong actualValue = (ulong)(object)value;
2233+
result = actualValue;
2234+
return true;
2235+
}
22322236
else if (typeof(TOther) == typeof(UInt128))
22332237
{
22342238
UInt128 actualValue = (UInt128)(object)value;
2235-
result = (actualValue >= MaxValue) ? MaxValue : (ulong)actualValue;
2239+
result = actualValue;
22362240
return true;
22372241
}
22382242
else if (typeof(TOther) == typeof(nuint))
@@ -2274,10 +2278,16 @@ static bool INumberBase<UInt256>.TryConvertFromTruncating<TOther>(TOther value,
22742278
result = actualValue;
22752279
return true;
22762280
}
2281+
else if (typeof(TOther) == typeof(ulong))
2282+
{
2283+
ulong actualValue = (ulong)(object)value;
2284+
result = actualValue;
2285+
return true;
2286+
}
22772287
else if (typeof(TOther) == typeof(UInt128))
22782288
{
22792289
UInt128 actualValue = (UInt128)(object)value;
2280-
result = (ulong)actualValue;
2290+
result = actualValue;
22812291
return true;
22822292
}
22832293
else if (typeof(TOther) == typeof(nuint))
@@ -2319,12 +2329,6 @@ static bool INumberBase<UInt256>.TryConvertToChecked<TOther>(UInt256 value, [May
23192329
result = (TOther)(object)actualResult;
23202330
return true;
23212331
}
2322-
else if (typeof(TOther) == typeof(UInt128))
2323-
{
2324-
UInt128 actualResult = checked((UInt128)value);
2325-
result = (TOther)(object)actualResult;
2326-
return true;
2327-
}
23282332
else if (typeof(TOther) == typeof(nint))
23292333
{
23302334
nint actualResult = checked((nint)value);
@@ -2348,43 +2352,37 @@ static bool INumberBase<UInt256>.TryConvertToSaturating<TOther>(UInt256 value, [
23482352
{
23492353
if (typeof(TOther) == typeof(short))
23502354
{
2351-
short actualResult = (value >= (ulong)short.MaxValue) ? short.MaxValue : (short)value;
2355+
short actualResult = (value >= new UInt256(0UL, 0UL, 0UL, 0x0000_0000_0000_7FFF)) ? short.MaxValue : (short)value;
23522356
result = (TOther)(object)actualResult;
23532357
return true;
23542358
}
23552359
else if (typeof(TOther) == typeof(int))
23562360
{
2357-
int actualResult = (value >= int.MaxValue) ? int.MaxValue : (int)value;
2361+
int actualResult = (value >= new UInt256(0UL, 0UL, 0UL, 0x0000_0000_0000_7FFF)) ? int.MaxValue : (int)value;
23582362
result = (TOther)(object)actualResult;
23592363
return true;
23602364
}
23612365
else if (typeof(TOther) == typeof(long))
23622366
{
2363-
long actualResult = (value >= long.MaxValue) ? long.MaxValue : (long)value;
2367+
long actualResult = (value >= new UInt256(0UL, 0UL, 0UL, 0x0000_0000_0000_7FFF)) ? long.MaxValue : (long)value;
23642368
result = (TOther)(object)actualResult;
23652369
return true;
23662370
}
23672371
else if (typeof(TOther) == typeof(Int128))
23682372
{
2369-
Int128 actualResult = (Int128)value;
2373+
Int128 actualResult = (value >= new UInt256(0UL, 0UL, 0UL, 0x0000_0000_0000_7FFF)) ? Int128.MaxValue : (Int128)value;
23702374
result = (TOther)(object)actualResult;
23712375
return true;
23722376
}
2373-
else if (typeof(TOther) == typeof(UInt128))
2377+
else if (typeof(TOther) == typeof(nint)) // 64-bits only
23742378
{
2375-
UInt128 actualResult = (UInt128)value;
2376-
result = (TOther)(object)actualResult;
2377-
return true;
2378-
}
2379-
else if (typeof(TOther) == typeof(nint))
2380-
{
2381-
nint actualResult = (value >= (ulong)nint.MaxValue) ? nint.MaxValue : (nint)value;
2379+
nint actualResult = (value >= new UInt256(0UL, 0UL, 0UL, 0x0000_0000_0000_7FFF)) ? nint.MaxValue : (nint)value;
23822380
result = (TOther)(object)actualResult;
23832381
return true;
23842382
}
23852383
else if (typeof(TOther) == typeof(sbyte))
23862384
{
2387-
sbyte actualResult = (value >= (ulong)sbyte.MaxValue) ? sbyte.MaxValue : (sbyte)value;
2385+
sbyte actualResult = (value >= new UInt256(0UL, 0UL, 0UL, 0x0000_0000_0000_7FFF)) ? sbyte.MaxValue : (sbyte)value;
23882386
result = (TOther)(object)actualResult;
23892387
return true;
23902388
}
@@ -2421,12 +2419,6 @@ static bool INumberBase<UInt256>.TryConvertToTruncating<TOther>(UInt256 value, [
24212419
result = (TOther)(object)actualResult;
24222420
return true;
24232421
}
2424-
else if (typeof(TOther) == typeof(UInt128))
2425-
{
2426-
UInt128 actualResult = (UInt128)value;
2427-
result = (TOther)(object)actualResult;
2428-
return true;
2429-
}
24302422
else if (typeof(TOther) == typeof(nint))
24312423
{
24322424
nint actualResult = (nint)value;

0 commit comments

Comments
 (0)