mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
parent
0c05e37996
commit
d9258162ca
5 changed files with 16 additions and 18 deletions
|
|
@ -17,11 +17,7 @@
|
|||
// Package common contains various helper functions.
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
)
|
||||
import "encoding/hex"
|
||||
|
||||
// ToHex returns the hex representation of b, prefixed with '0x'.
|
||||
// For empty slices, the return value is "0x0".
|
||||
|
|
@ -47,8 +43,10 @@ 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 hexutil.Has0xPrefix(s) {
|
||||
s = s[2:]
|
||||
if len(s) > 1 {
|
||||
if s[0:2] == "0x" || s[0:2] == "0X" {
|
||||
s = s[2:]
|
||||
}
|
||||
}
|
||||
if len(s)%2 == 1 {
|
||||
s = "0" + s
|
||||
|
|
@ -67,6 +65,11 @@ 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')
|
||||
|
|
|
|||
|
|
@ -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:])
|
||||
|
|
@ -185,8 +185,7 @@ func EncodeBig(bigint *big.Int) string {
|
|||
return fmt.Sprintf("%#x", bigint)
|
||||
}
|
||||
|
||||
// Has0xPrefix validates input begins with '0x' or '0X'.
|
||||
func Has0xPrefix(input string) bool {
|
||||
func has0xPrefix(input string) bool {
|
||||
return len(input) >= 2 && input[0] == '0' && (input[1] == 'x' || input[1] == 'X')
|
||||
}
|
||||
|
||||
|
|
@ -194,7 +193,7 @@ 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:]
|
||||
|
|
|
|||
|
|
@ -20,8 +20,6 @@ package math
|
|||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
)
|
||||
|
||||
// Various big integer limit values.
|
||||
|
|
@ -77,7 +75,7 @@ func ParseBig256(s string) (*big.Int, bool) {
|
|||
}
|
||||
var bigint *big.Int
|
||||
var ok bool
|
||||
if hexutil.Has0xPrefix(s) {
|
||||
if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") {
|
||||
bigint, ok = new(big.Int).SetString(s[2:], 16)
|
||||
} else {
|
||||
bigint, ok = new(big.Int).SetString(s, 10)
|
||||
|
|
|
|||
|
|
@ -19,8 +19,6 @@ package math
|
|||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
)
|
||||
|
||||
// Integer limit values.
|
||||
|
|
@ -63,7 +61,7 @@ func ParseUint64(s string) (uint64, bool) {
|
|||
if s == "" {
|
||||
return 0, true
|
||||
}
|
||||
if hexutil.Has0xPrefix(s) {
|
||||
if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") {
|
||||
v, err := strconv.ParseUint(s[2:], 16, 64)
|
||||
return v, err == nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 hexutil.Has0xPrefix(s) {
|
||||
if hasHexPrefix(s) {
|
||||
s = s[2:]
|
||||
}
|
||||
return len(s) == 2*AddressLength && isHex(s)
|
||||
|
|
|
|||
Loading…
Reference in a new issue