@@ -2034,8 +2034,6 @@ public static bool TryParse(ReadOnlySpan<char> value, out UInt256 result) => val
2034
2034
? TryParse ( value . Slice ( 2 ) , NumberStyles . HexNumber , CultureInfo . InvariantCulture , out result )
2035
2035
: TryParse ( value , NumberStyles . Integer , CultureInfo . InvariantCulture , out result ) ;
2036
2036
2037
- //public static bool TryParse(string value, NumberStyles style, IFormatProvider provider, out UInt256 result) => TryParse(value.AsSpan(), style, provider, out result);
2038
-
2039
2037
public static bool TryParse ( in ReadOnlySpan < char > value , NumberStyles style , IFormatProvider ? provider , out UInt256 result )
2040
2038
{
2041
2039
BigInteger a ;
@@ -2166,12 +2164,6 @@ static bool INumberBase<UInt256>.TryConvertFromChecked<TOther>(TOther value, out
2166
2164
result = actualValue ;
2167
2165
return true ;
2168
2166
}
2169
- else if ( typeof ( TOther ) == typeof ( decimal ) )
2170
- {
2171
- decimal actualValue = ( decimal ) ( object ) value ;
2172
- result = checked ( ( ulong ) actualValue ) ;
2173
- return true ;
2174
- }
2175
2167
else if ( typeof ( TOther ) == typeof ( ushort ) )
2176
2168
{
2177
2169
ushort actualValue = ( ushort ) ( object ) value ;
@@ -2187,7 +2179,7 @@ static bool INumberBase<UInt256>.TryConvertFromChecked<TOther>(TOther value, out
2187
2179
else if ( typeof ( TOther ) == typeof ( UInt128 ) )
2188
2180
{
2189
2181
UInt128 actualValue = ( UInt128 ) ( object ) value ;
2190
- result = checked ( ( ulong ) actualValue ) ;
2182
+ result = actualValue ;
2191
2183
return true ;
2192
2184
}
2193
2185
else if ( typeof ( TOther ) == typeof ( nuint ) )
@@ -2217,6 +2209,12 @@ static bool INumberBase<UInt256>.TryConvertFromSaturating<TOther>(TOther value,
2217
2209
result = actualValue ;
2218
2210
return true ;
2219
2211
}
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
+ }
2220
2218
else if ( typeof ( TOther ) == typeof ( ushort ) )
2221
2219
{
2222
2220
ushort actualValue = ( ushort ) ( object ) value ;
@@ -2229,10 +2227,16 @@ static bool INumberBase<UInt256>.TryConvertFromSaturating<TOther>(TOther value,
2229
2227
result = actualValue ;
2230
2228
return true ;
2231
2229
}
2230
+ else if ( typeof ( TOther ) == typeof ( ulong ) )
2231
+ {
2232
+ ulong actualValue = ( ulong ) ( object ) value ;
2233
+ result = actualValue ;
2234
+ return true ;
2235
+ }
2232
2236
else if ( typeof ( TOther ) == typeof ( UInt128 ) )
2233
2237
{
2234
2238
UInt128 actualValue = ( UInt128 ) ( object ) value ;
2235
- result = ( actualValue >= MaxValue ) ? MaxValue : ( ulong ) actualValue ;
2239
+ result = actualValue ;
2236
2240
return true ;
2237
2241
}
2238
2242
else if ( typeof ( TOther ) == typeof ( nuint ) )
@@ -2274,10 +2278,16 @@ static bool INumberBase<UInt256>.TryConvertFromTruncating<TOther>(TOther value,
2274
2278
result = actualValue ;
2275
2279
return true ;
2276
2280
}
2281
+ else if ( typeof ( TOther ) == typeof ( ulong ) )
2282
+ {
2283
+ ulong actualValue = ( ulong ) ( object ) value ;
2284
+ result = actualValue ;
2285
+ return true ;
2286
+ }
2277
2287
else if ( typeof ( TOther ) == typeof ( UInt128 ) )
2278
2288
{
2279
2289
UInt128 actualValue = ( UInt128 ) ( object ) value ;
2280
- result = ( ulong ) actualValue ;
2290
+ result = actualValue ;
2281
2291
return true ;
2282
2292
}
2283
2293
else if ( typeof ( TOther ) == typeof ( nuint ) )
@@ -2319,12 +2329,6 @@ static bool INumberBase<UInt256>.TryConvertToChecked<TOther>(UInt256 value, [May
2319
2329
result = ( TOther ) ( object ) actualResult ;
2320
2330
return true ;
2321
2331
}
2322
- else if ( typeof ( TOther ) == typeof ( UInt128 ) )
2323
- {
2324
- UInt128 actualResult = checked ( ( UInt128 ) value ) ;
2325
- result = ( TOther ) ( object ) actualResult ;
2326
- return true ;
2327
- }
2328
2332
else if ( typeof ( TOther ) == typeof ( nint ) )
2329
2333
{
2330
2334
nint actualResult = checked ( ( nint ) value ) ;
@@ -2348,43 +2352,37 @@ static bool INumberBase<UInt256>.TryConvertToSaturating<TOther>(UInt256 value, [
2348
2352
{
2349
2353
if ( typeof ( TOther ) == typeof ( short ) )
2350
2354
{
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 ;
2352
2356
result = ( TOther ) ( object ) actualResult ;
2353
2357
return true ;
2354
2358
}
2355
2359
else if ( typeof ( TOther ) == typeof ( int ) )
2356
2360
{
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 ;
2358
2362
result = ( TOther ) ( object ) actualResult ;
2359
2363
return true ;
2360
2364
}
2361
2365
else if ( typeof ( TOther ) == typeof ( long ) )
2362
2366
{
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 ;
2364
2368
result = ( TOther ) ( object ) actualResult ;
2365
2369
return true ;
2366
2370
}
2367
2371
else if ( typeof ( TOther ) == typeof ( Int128 ) )
2368
2372
{
2369
- Int128 actualResult = ( Int128 ) value ;
2373
+ Int128 actualResult = ( value >= new UInt256 ( 0UL , 0UL , 0UL , 0x0000_0000_0000_7FFF ) ) ? Int128 . MaxValue : ( Int128 ) value ;
2370
2374
result = ( TOther ) ( object ) actualResult ;
2371
2375
return true ;
2372
2376
}
2373
- else if ( typeof ( TOther ) == typeof ( UInt128 ) )
2377
+ else if ( typeof ( TOther ) == typeof ( nint ) ) // 64-bits only
2374
2378
{
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 ;
2382
2380
result = ( TOther ) ( object ) actualResult ;
2383
2381
return true ;
2384
2382
}
2385
2383
else if ( typeof ( TOther ) == typeof ( sbyte ) )
2386
2384
{
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 ;
2388
2386
result = ( TOther ) ( object ) actualResult ;
2389
2387
return true ;
2390
2388
}
@@ -2421,12 +2419,6 @@ static bool INumberBase<UInt256>.TryConvertToTruncating<TOther>(UInt256 value, [
2421
2419
result = ( TOther ) ( object ) actualResult ;
2422
2420
return true ;
2423
2421
}
2424
- else if ( typeof ( TOther ) == typeof ( UInt128 ) )
2425
- {
2426
- UInt128 actualResult = ( UInt128 ) value ;
2427
- result = ( TOther ) ( object ) actualResult ;
2428
- return true ;
2429
- }
2430
2422
else if ( typeof ( TOther ) == typeof ( nint ) )
2431
2423
{
2432
2424
nint actualResult = ( nint ) value ;
0 commit comments