Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions bigintHelpers.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down