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

common: unify hex prefix check code #19937

Merged
merged 4 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 7 additions & 10 deletions common/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
// Package common contains various helper functions.
package common

import "encoding/hex"
import (
"encoding/hex"

"github.com/ethereum/go-ethereum/common/hexutil"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like introducing this dependency for so little gain

)

// ToHex returns the hex representation of b, prefixed with '0x'.
// For empty slices, the return value is "0x0".
Expand All @@ -43,10 +47,8 @@ func ToHexArray(b [][]byte) []string {
// FromHex returns the bytes represented by the hexadecimal string s.
// s may be prefixed with "0x".
func FromHex(s string) []byte {
if len(s) > 1 {
if s[0:2] == "0x" || s[0:2] == "0X" {
s = s[2:]
}
if hexutil.Has0xPrefix(s) {
s = s[2:]
}
if len(s)%2 == 1 {
s = "0" + s
Expand All @@ -65,11 +67,6 @@ func CopyBytes(b []byte) (copiedBytes []byte) {
return
}

// hasHexPrefix validates str begins with '0x' or '0X'.
func hasHexPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
}

// isHexCharacter returns bool of c being a valid hexadecimal.
func isHexCharacter(c byte) bool {
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
Expand Down
7 changes: 4 additions & 3 deletions common/hexutil/hexutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func Decode(input string) ([]byte, error) {
if len(input) == 0 {
return nil, ErrEmptyString
}
if !has0xPrefix(input) {
if !Has0xPrefix(input) {
return nil, ErrMissingPrefix
}
b, err := hex.DecodeString(input[2:])
Expand Down Expand Up @@ -185,15 +185,16 @@ func EncodeBig(bigint *big.Int) string {
return fmt.Sprintf("%#x", bigint)
}

func has0xPrefix(input string) bool {
// Has0xPrefix validates input begins with '0x' or '0X'.
func Has0xPrefix(input string) bool {
return len(input) >= 2 && input[0] == '0' && (input[1] == 'x' || input[1] == 'X')
}

func checkNumber(input string) (raw string, err error) {
if len(input) == 0 {
return "", ErrEmptyString
}
if !has0xPrefix(input) {
if !Has0xPrefix(input) {
return "", ErrMissingPrefix
}
input = input[2:]
Expand Down
4 changes: 3 additions & 1 deletion common/math/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package math
import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common/hexutil"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like introducing this dependency for so little gain

)

// Various big integer limit values.
Expand Down Expand Up @@ -75,7 +77,7 @@ func ParseBig256(s string) (*big.Int, bool) {
}
var bigint *big.Int
var ok bool
if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") {
if hexutil.Has0xPrefix(s) {
bigint, ok = new(big.Int).SetString(s[2:], 16)
} else {
bigint, ok = new(big.Int).SetString(s, 10)
Expand Down
4 changes: 3 additions & 1 deletion common/math/integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package math
import (
"fmt"
"strconv"

"github.com/ethereum/go-ethereum/common/hexutil"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like introducing this dependency for so little gain

)

// Integer limit values.
Expand Down Expand Up @@ -61,7 +63,7 @@ func ParseUint64(s string) (uint64, bool) {
if s == "" {
return 0, true
}
if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") {
if hexutil.Has0xPrefix(s) {
v, err := strconv.ParseUint(s[2:], 16, 64)
return v, err == nil
}
Expand Down
2 changes: 1 addition & 1 deletion common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
// IsHexAddress verifies whether a string can represent a valid hex-encoded
// Ethereum address or not.
func IsHexAddress(s string) bool {
if hasHexPrefix(s) {
if hexutil.Has0xPrefix(s) {
s = s[2:]
}
return len(s) == 2*AddressLength && isHex(s)
Expand Down