Skip to content

Commit

Permalink
consolidated String.ToUint64 into UnmarshalUint64Text
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Oct 5, 2024
1 parent 653d328 commit c40ecb7
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 82 deletions.
57 changes: 0 additions & 57 deletions mod/primitives/pkg/encoding/hex/hex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,63 +291,6 @@ func TestString_MustToBytes(t *testing.T) {
}
}

func TestString_ToUint64(t *testing.T) {
tests := []struct {
name string
input hex.String
expected uint64
expectErr bool
}{
{"Single digit", "0x1", 1, false},
{"Two digits", "0x10", 16, false},
{"Mixed digits and letters", "0x1a", 26, false},
{"Invalid hex string", "0xinvalid", 0, true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := tt.input.ToUint64()
if tt.expectErr {
require.Error(t, err, "Test case: %s", tt.name)
} else {
require.NoError(t, err, "Test case: %s", tt.name)
require.Equal(t, tt.expected, result, "Test case: %s", tt.name)
}
})
}
}

func TestString_MustToUInt64(t *testing.T) {
tests := []struct {
name string
input hex.String
expected uint64
panics bool
}{
{"Single digit", "0x1", 1, false},
{"Two digits", "0x10", 16, false},
{"Mixed digits and letters", "0x1a", 26, false},
{"Invalid hex string", "0xinvalid", 0, true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var (
res uint64
f = func() {
res = tt.input.MustToUInt64()
}
)
if tt.panics {
require.Panics(t, f)
} else {
require.NotPanics(t, f)
require.Equal(t, tt.expected, res)
}
})
}
}

func TestString_MustToBigInt(t *testing.T) {
tests := []struct {
name string
Expand Down
24 changes: 0 additions & 24 deletions mod/primitives/pkg/encoding/hex/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package hex
import (
"bytes"
"math/big"
"strconv"
"strings"

"github.com/berachain/beacon-kit/mod/errors"
Expand Down Expand Up @@ -92,25 +91,6 @@ func FromJSONString[B ~[]byte](b B) String {
return NewString(bytes.Trim(b, "\""))
}

// ToUint64 decodes a hex string with 0x prefix.
func (s String) ToUint64() (uint64, error) {
raw, err := formatAndValidateNumber(s.Unwrap())
if err != nil {
return 0, err
}
return strconv.ParseUint(raw, 16, 64)
}

// MustToUInt64 decodes a hex string with 0x prefix.
// It panics for invalid input.
func (s String) MustToUInt64() uint64 {
i, err := s.ToUint64()
if err != nil {
panic(err)
}
return i
}

// ToBigInt decodes a hex string with 0x prefix.
func (s String) ToBigInt() (*big.Int, error) {
raw, err := formatAndValidateNumber(s.Unwrap())
Expand Down Expand Up @@ -155,10 +135,6 @@ func (s String) MustToBigInt() *big.Int {
return bi
}

func (s String) AddQuotes() String {
return "\"" + s + "\""
}

// Unwrap returns the string value.
func (s String) Unwrap() string {
return string(s)
Expand Down
4 changes: 4 additions & 0 deletions mod/primitives/pkg/encoding/hex/u64.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func UnmarshalUint64Text(input []byte) (uint64, error) {
if len(raw) > nibblesPer64Bits {
return 0, ErrUint64Range
}

// Question: could the code below be
// return strconv.ParseUint(string(raw), 16, 64)
// as it was in String.ToUint64() ?
var dec uint64
for _, byte := range raw {
nib := decodeNibble(byte)
Expand Down
5 changes: 4 additions & 1 deletion mod/primitives/pkg/encoding/hex/u64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,17 @@ func TestUnmarshalUint64Text(t *testing.T) {
err error
}{
{"Zero", []byte("0x0"), 0, nil},
{"Single digit", []byte("0x1"), 1, nil},
{"Two digits", []byte("0x10"), 16, nil},
{"Mixed digits and letters", []byte("0x1a"), 26, nil},
{"MaxByte", []byte("0xff"), 255, nil},
{"MaxWord", []byte("0xffff"), 65535, nil},
{"MaxDWord", []byte("0xffffffff"), 4294967295, nil},
{"MaxQWord", []byte("0xffffffffffffffff"), 18446744073709551615, nil},
{"OutOfRange", []byte("0x10000000000000000"), 0, hex.ErrUint64Range},
{"InvalidString", []byte("0xzz"), 0, hex.ErrInvalidString},
{"Invalid hex string", []byte("0xinvalid"), 0, hex.ErrInvalidString},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := hex.UnmarshalUint64Text(test.input)
Expand Down

0 comments on commit c40ecb7

Please sign in to comment.