Skip to content

Commit de00546

Browse files
Automated parsabilety of hex numbers ("0x...") (#35)
* Automated parsabilety of hex numbers starting with "0x" When the NM client parses something like a Dictionary from JSON where input keys look like this: "0x0000000000000000000000000000000000000000000000000000000000000000" we currently fail. To fix this, we need to re-implement the dictionary parser as they do here (https://stackoverflow.com/a/7010231/1973207 ) with explicit `(UInt256)BigInteger.Parse(skey.Substring(2), NumberStyles.HexNumber);` in it. This would help automate this and allow the elimination of specialised dictionary parsers like that.
1 parent 4cf50ea commit de00546

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/Nethermind.Int256.Test/UInt256Tests.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using System;
2+
using System.Globalization;
23
using System.Numerics;
34
using FluentAssertions;
45
using NUnit.Framework;
@@ -469,5 +470,13 @@ string Expected(string valueString)
469470
catch (Exception e) when (e.GetType() == expectedException) { }
470471
}
471472
}
473+
[Test]
474+
public virtual void ParseLongNumber()
475+
{
476+
string hexValueWith66Zeroes = "0x" + new string('0', 66);
477+
BigInteger bigIntWith66Zeroes = BigInteger.Parse(hexValueWith66Zeroes.Substring(2), NumberStyles.HexNumber);
478+
var UintParsedValue = UInt256.Parse(hexValueWith66Zeroes);
479+
Assert.AreEqual((UInt256)bigIntWith66Zeroes, UintParsedValue);
480+
}
472481
}
473482
}

src/Nethermind.Int256/UInt256.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Buffers.Binary;
33
using System.Globalization;
44
using System.Numerics;
@@ -1882,9 +1882,11 @@ public void Convert(out BigInteger big)
18821882

18831883
public static UInt256 Parse(in ReadOnlySpan<char> value, NumberStyles numberStyles) => !TryParse(value, numberStyles, CultureInfo.InvariantCulture, out UInt256 c) ? throw new FormatException() : c;
18841884

1885-
public static bool TryParse(string value, out UInt256 result) => TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
1885+
public static bool TryParse(string value, out UInt256 result) => TryParse(value.AsSpan(), out result);
18861886

1887-
public static bool TryParse(ReadOnlySpan<char> value, out UInt256 result) => TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
1887+
public static bool TryParse(ReadOnlySpan<char> value, out UInt256 result) => value.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
1888+
? TryParse(value.Slice(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out result)
1889+
: TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
18881890

18891891
public static bool TryParse(string value, NumberStyles style, IFormatProvider provider, out UInt256 result) => TryParse(value.AsSpan(), style, provider, out result);
18901892

0 commit comments

Comments
 (0)