diff --git a/bigintHelpers.go.tmpl b/bigintHelpers.go.tmpl index d0675ea..6413aff 100644 --- a/bigintHelpers.go.tmpl +++ b/bigintHelpers.go.tmpl @@ -24,13 +24,29 @@ func (b BigInt) MarshalText() ([]byte, error) { // UnmarshalText implements encoding.TextUnmarshaler. func (b *BigInt) UnmarshalText(text []byte) error { - t := string(text) - if len(text) <= 2 || t == "null" || t == "" { + if len(text) == 0 { return nil } - i, ok := big.NewInt(0).SetString(string(text[1:len(text)-1]), 10) + if len(text) == 4 && text[0] == 'n' && string(text) == "null" { + return nil + } + for _, c := range text { + if c == ' ' || c == '\t' || c == '\n' || c == '\r' { + return fmt.Errorf("BigInt.UnmarshalText: unexpected whitespace in %q", text) + } + } + var digits []byte + if text[0] == '-' || (text[0] >= '0' && text[0] <= '9') { + digits = text + } else { + if len(text) < 2 || text[0] != '"' || text[len(text)-1] != '"' { + return fmt.Errorf("BigInt.UnmarshalText: unsupported format %q", text) + } + digits = text[1 : len(text)-1] + } + i, ok := big.NewInt(0).SetString(string(digits), 10) if !ok { - return fmt.Errorf("BigInt.UnmarshalText: failed to unmarshal %q", text) + return fmt.Errorf("BigInt.UnmarshalText: failed to parse %q", text) } *b = BigInt(*i) return nil