Skip to content

Commit a98e92b

Browse files
committed
add decoding
1 parent 1512cf4 commit a98e92b

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

common/hexutil/hexutil.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,23 @@ func MustDecodeUint64(input string) uint64 {
112112
return dec
113113
}
114114

115+
// DecodeUint16 decodes a hex string with 0x prefix as a quantity.
116+
func DecodeUint16(input string) (uint16, error) {
117+
raw, err := checkNumber(input)
118+
if err != nil {
119+
return 0, err
120+
}
121+
dec, err := strconv.ParseUint(raw, 16, 16)
122+
if err != nil {
123+
err = mapError(err)
124+
if err == ErrUint64Range {
125+
return 0, ErrUint16Range
126+
}
127+
}
128+
return uint16(dec), err
129+
}
130+
131+
// MustDecodeUint16 decodes a hex string with 0x prefix as a quantity.
115132
// EncodeUint64 encodes i as a hex string with 0x prefix.
116133
func EncodeUint64(i uint64) string {
117134
enc := make([]byte, 2, 10)

common/hexutil/hexutil_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ var (
141141
{input: `0xbbb`, want: uint64(0xbbb)},
142142
{input: `0xffffffffffffffff`, want: uint64(0xffffffffffffffff)},
143143
}
144+
145+
decodeUint16Tests = []unmarshalTest{
146+
// invalid
147+
{input: `0`, wantErr: ErrMissingPrefix},
148+
{input: `0x`, wantErr: ErrEmptyNumber},
149+
{input: `0x01`, wantErr: ErrLeadingZero},
150+
{input: `0xfffff`, wantErr: ErrUint16Range},
151+
{input: `0xz1`, wantErr: ErrSyntax},
152+
// valid
153+
{input: `0x0`, want: uint16(0)},
154+
{input: `0x2`, want: uint16(0x2)},
155+
{input: `0x2F2`, want: uint16(0x2f2)},
156+
{input: `0X2F2`, want: uint16(0x2f2)},
157+
{input: `0xff`, want: uint16(0xff)},
158+
{input: `0x12af`, want: uint16(0x12af)},
159+
{input: `0xbbb`, want: uint16(0xbbb)},
160+
{input: `0xffff`, want: uint16(0xffff)},
161+
}
144162
)
145163

146164
func TestEncode(t *testing.T) {
@@ -218,6 +236,19 @@ func TestDecodeUint64(t *testing.T) {
218236
}
219237
}
220238

239+
func TestDecodeUint16(t *testing.T) {
240+
for _, test := range decodeUint16Tests {
241+
dec, err := DecodeUint16(test.input)
242+
if !checkError(t, test.input, err, test.wantErr) {
243+
continue
244+
}
245+
if dec != test.want.(uint16) {
246+
t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
247+
continue
248+
}
249+
}
250+
}
251+
221252
func BenchmarkEncodeBig(b *testing.B) {
222253
for _, bench := range encodeBigTests {
223254
b.Run(bench.want, func(b *testing.B) {

common/hexutil/json_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ var unmarshalUint16Tests = []unmarshalTest{
408408
{input: `"0x01"`, wantErr: wrapTypeError(ErrLeadingZero, uint16T)},
409409
{input: `"0x10000"`, wantErr: wrapTypeError(ErrUint16Range, uint16T)},
410410
{input: `"0xx"`, wantErr: wrapTypeError(ErrSyntax, uint16T)},
411-
{input: `"0x1zz01"`, wantErr: wrapTypeError(ErrSyntax, uint16T)},
411+
{input: `"0xz1"`, wantErr: wrapTypeError(ErrSyntax, uint16T)},
412412

413413
// valid encoding
414414
{input: `""`, want: uint16(0)},

0 commit comments

Comments
 (0)