common/hexutil: deduplicate UnmarshalFixedText functions

This commit is contained in:
emmmm 2026-03-11 05:22:05 -03:00 committed by GitHub
parent 32f05d68a2
commit 95dd15f221
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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.