Skip to content

Commit

Permalink
dropped NewStringStrict
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Oct 5, 2024
1 parent 2ce82fc commit b09b2f1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 40 deletions.
8 changes: 2 additions & 6 deletions mod/primitives/pkg/encoding/hex/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,9 @@ func MustToBytes(input string) []byte {
// ToBytes returns the bytes represented by the given hex string.
// An error is returned if the input is not a valid hex string.
func ToBytes(input string) ([]byte, error) {
s, err := NewStringStrict(input)
strippedInput, err := IsValidHex(input)
if err != nil {
return nil, err
}
h, err := s.ToBytes()
if err != nil {
return nil, err
}
return h, nil
return hex.DecodeString(strippedInput)
}
48 changes: 23 additions & 25 deletions mod/primitives/pkg/encoding/hex/hex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,43 @@ import (
func TestNewStringStrictInvariants(t *testing.T) {
// NewStringStrict constructor should error if the input is invalid
tests := []struct {
name string
input string
expectErr bool
name string
input string
expectedErr error
}{
{
name: "Valid hex string",
input: "0x48656c6c6f",
expectErr: false,
name: "Valid hex string",
input: "0x48656c6c6f",
expectedErr: nil,
},
{
name: "Empty string",
input: "",
expectErr: true,
name: "Empty string",
input: "",
expectedErr: hex.ErrEmptyString,
},
{
name: "No 0x prefix",
input: "48656c6c6f",
expectErr: true,
name: "No 0x prefix",
input: "48656c6c6f",
expectedErr: hex.ErrMissingPrefix,
},
{
name: "Valid single hex character",
input: "0x0",
expectErr: false,
name: "Valid single hex character",
input: "0x0",
expectedErr: nil,
},
{
name: "Empty hex string",
input: "0x",
expectErr: false,
name: "Empty hex string",
input: "0x",
expectedErr: nil,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
str, err := hex.NewStringStrict(test.input)
if test.expectErr {
require.Error(t, err, "Test case: %s", test.name)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := hex.IsValidHex(tt.input)
if tt.expectedErr != nil {
require.Error(t, err)
} else {
require.NoError(t, err, "Test case: %s", test.name)
_, err = hex.IsValidHex(str)
require.NoError(t, err)
}
})
Expand Down
9 changes: 0 additions & 9 deletions mod/primitives/pkg/encoding/hex/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,6 @@ func IsValidHex[T ~[]byte | ~string](s T) (T, error) {
return s[prefixLen:], nil
}

// NewStringStrict creates a hex string with 0x prefix. It errors if any of the
// string invariants are violated.
func NewStringStrict[T []byte | string](s T) (String, error) {
if _, err := IsValidHex(s); err != nil {
return "", err
}
return String(s), nil
}

// NewString creates a hex string with 0x prefix. It modifies the input to
// ensure that the string invariants are satisfied.
func NewString[T []byte | string](s T) String {
Expand Down

0 comments on commit b09b2f1

Please sign in to comment.