Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Remove existing panics in the code #270

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions sort/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ import (

// StringSlice is a function type used to sort a slice of strings in increasing
// order. Any errors which arise from sorting the slice will raise a panic.
type StringSlice func(x []string)
type StringSlice func(x []string) error

// Strings sorts a slice of strings in increasing order.
var Strings = sort.Strings
func Strings(x []string) error {
sort.Strings(x)
return nil
}

// StringsByInt sorts a slice of strings according to their integer value.
// This function panics in the event that an element in the slice cannot be
// converted to an integer
func StringsByInt(x []string) {
func StringsByInt(x []string) error {
sort.Slice(x, func(i, j int) bool {
valI, err := strconv.Atoi(x[i])
if err != nil {
Expand All @@ -31,22 +34,27 @@ func StringsByInt(x []string) {
}
return valI < valJ
})
return nil
}

// StringsByHex sorts a slice of strings according to their big-endian Hex value.
// This function panics in the event that an element in the slice cannot be
// converted to a Hex slice. Each string representation of a hex value must be
// of even length.
func StringsByHex(x []string) {
func StringsByHex(x []string) error {
var outerErr error
sort.Slice(x, func(i, j int) bool {
valI, err := encoding.ASCIIHexToBytes.Encode([]byte(x[i]))
if err != nil {
panic(fmt.Sprintf("failed to encode ascii hex %s to bytes : %v", x[i], err))
outerErr = fmt.Errorf("failed to encode ascii hex %s to bytes : %v", x[i], err)
return false
}
valJ, err := encoding.ASCIIHexToBytes.Encode([]byte(x[j]))
if err != nil {
panic(fmt.Sprintf("failed to sort strings by hex: %v", err))
outerErr = fmt.Errorf("failed to sort strings by hex: %v", err)
return false
}
return new(big.Int).SetBytes(valI).Int64() < new(big.Int).SetBytes(valJ).Int64()
})
return outerErr
}
7 changes: 7 additions & 0 deletions sort/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ func TestSortStringsByHex(t *testing.T) {
StringsByHex(x)
require.Equal(t, []string{"10", "B0", "ABCD"}, x)
}

func TestStringsByHexError(t *testing.T) {
x := []string{"B0", "Z", "ABCD"}
err := StringsByHex(x)
require.NotNil(t, err)
require.Equal(t, "failed to sort strings by hex: failed to perform hex decoding", err.Error())
}
Loading