Commit 2a1a531b authored by Péter Szilágyi's avatar Péter Szilágyi Committed by GitHub

Merge pull request #3570 from fjl/hexutil-zero-fix

common/hexutil: fix EncodeBig, Big.MarshalJSON
parents b5a100b8 51f6b6d3
...@@ -169,12 +169,7 @@ func EncodeBig(bigint *big.Int) string { ...@@ -169,12 +169,7 @@ func EncodeBig(bigint *big.Int) string {
if nbits == 0 { if nbits == 0 {
return "0x0" return "0x0"
} }
enc := make([]byte, 2, (nbits/8)*2+2) return fmt.Sprintf("0x%x", bigint)
copy(enc, "0x")
for i := len(bigint.Bits()) - 1; i >= 0; i-- {
enc = strconv.AppendUint(enc, uint64(bigint.Bits()[i]), 16)
}
return string(enc)
} }
func has0xPrefix(input string) bool { func has0xPrefix(input string) bool {
......
...@@ -46,6 +46,7 @@ var ( ...@@ -46,6 +46,7 @@ var (
{referenceBig("1"), "0x1"}, {referenceBig("1"), "0x1"},
{referenceBig("ff"), "0xff"}, {referenceBig("ff"), "0xff"},
{referenceBig("112233445566778899aabbccddeeff"), "0x112233445566778899aabbccddeeff"}, {referenceBig("112233445566778899aabbccddeeff"), "0x112233445566778899aabbccddeeff"},
{referenceBig("80a7f2c1bcc396c00"), "0x80a7f2c1bcc396c00"},
} }
encodeUint64Tests = []marshalTest{ encodeUint64Tests = []marshalTest{
......
...@@ -109,13 +109,8 @@ func (b *Big) MarshalJSON() ([]byte, error) { ...@@ -109,13 +109,8 @@ func (b *Big) MarshalJSON() ([]byte, error) {
if nbits == 0 { if nbits == 0 {
return jsonZero, nil return jsonZero, nil
} }
enc := make([]byte, 3, (nbits/8)*2+4) enc := fmt.Sprintf(`"0x%x"`, bigint)
copy(enc, `"0x`) return []byte(enc), nil
for i := len(bigint.Bits()) - 1; i >= 0; i-- {
enc = strconv.AppendUint(enc, uint64(bigint.Bits()[i]), 16)
}
enc = append(enc, '"')
return enc, nil
} }
// UnmarshalJSON implements json.Unmarshaler. // UnmarshalJSON implements json.Unmarshaler.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment