mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 10:50:44 +00:00
common/hexutil: deduplicate UnmarshalFixedText functions
This commit is contained in:
parent
32f05d68a2
commit
95dd15f221
1 changed files with 6 additions and 17 deletions
|
|
@ -108,14 +108,17 @@ func UnmarshalFixedJSON(typ reflect.Type, input, out []byte) error {
|
||||||
// determines the required input length. This function is commonly used to implement the
|
// determines the required input length. This function is commonly used to implement the
|
||||||
// UnmarshalText method for fixed-size types.
|
// UnmarshalText method for fixed-size types.
|
||||||
func UnmarshalFixedText(typname string, input, out []byte) error {
|
func UnmarshalFixedText(typname string, input, out []byte) error {
|
||||||
raw, err := checkText(input, true)
|
return unmarshalFixedText(typname, input, out, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmarshalFixedText(typname string, input, out []byte, wantPrefix bool) error {
|
||||||
|
raw, err := checkText(input, wantPrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(raw)/2 != len(out) {
|
if len(raw)/2 != len(out) {
|
||||||
return fmt.Errorf("hex string has length %d, want %d for %s", len(raw), len(out)*2, typname)
|
return fmt.Errorf("hex string has length %d, want %d for %s", len(raw), len(out)*2, typname)
|
||||||
}
|
}
|
||||||
// Pre-verify syntax before modifying out.
|
|
||||||
for _, b := range raw {
|
for _, b := range raw {
|
||||||
if decodeNibble(b) == badNibble {
|
if decodeNibble(b) == badNibble {
|
||||||
return ErrSyntax
|
return ErrSyntax
|
||||||
|
|
@ -129,21 +132,7 @@ func UnmarshalFixedText(typname string, input, out []byte) error {
|
||||||
// length of out determines the required input length. This function is commonly used to
|
// length of out determines the required input length. This function is commonly used to
|
||||||
// implement the UnmarshalText method for fixed-size types.
|
// implement the UnmarshalText method for fixed-size types.
|
||||||
func UnmarshalFixedUnprefixedText(typname string, input, out []byte) error {
|
func UnmarshalFixedUnprefixedText(typname string, input, out []byte) error {
|
||||||
raw, err := checkText(input, false)
|
return unmarshalFixedText(typname, input, out, false)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(raw)/2 != len(out) {
|
|
||||||
return fmt.Errorf("hex string has length %d, want %d for %s", len(raw), len(out)*2, typname)
|
|
||||||
}
|
|
||||||
// Pre-verify syntax before modifying out.
|
|
||||||
for _, b := range raw {
|
|
||||||
if decodeNibble(b) == badNibble {
|
|
||||||
return ErrSyntax
|
|
||||||
}
|
|
||||||
}
|
|
||||||
hex.Decode(out, raw)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Big marshals/unmarshals as a JSON string with 0x prefix.
|
// Big marshals/unmarshals as a JSON string with 0x prefix.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue