mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-19 21:31:37 +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
|
||||
// UnmarshalText method for fixed-size types.
|
||||
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 {
|
||||
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
|
||||
|
|
@ -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
|
||||
// implement the UnmarshalText method for fixed-size types.
|
||||
func UnmarshalFixedUnprefixedText(typname string, input, out []byte) error {
|
||||
raw, err := checkText(input, 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
|
||||
return unmarshalFixedText(typname, input, out, false)
|
||||
}
|
||||
|
||||
// Big marshals/unmarshals as a JSON string with 0x prefix.
|
||||
|
|
|
|||
Loading…
Reference in a new issue