| |
| |
| |
|
|
| |
|
|
| package big |
|
|
| import ( |
| "bytes" |
| "fmt" |
| ) |
|
|
| |
| const intGobVersion byte = 1 |
|
|
| |
| func (x *Int) GobEncode() ([]byte, error) { |
| if x == nil { |
| return nil, nil |
| } |
| buf := make([]byte, 1+len(x.abs)*_S) |
| i := x.abs.bytes(buf) - 1 |
| b := intGobVersion << 1 |
| if x.neg { |
| b |= 1 |
| } |
| buf[i] = b |
| return buf[i:], nil |
| } |
|
|
| |
| func (z *Int) GobDecode(buf []byte) error { |
| if len(buf) == 0 { |
| |
| *z = Int{} |
| return nil |
| } |
| b := buf[0] |
| if b>>1 != intGobVersion { |
| return fmt.Errorf("Int.GobDecode: encoding version %d not supported", b>>1) |
| } |
| z.neg = b&1 != 0 |
| z.abs = z.abs.setBytes(buf[1:]) |
| return nil |
| } |
|
|
| |
| func (x *Int) AppendText(b []byte) (text []byte, err error) { |
| return x.Append(b, 10), nil |
| } |
|
|
| |
| func (x *Int) MarshalText() (text []byte, err error) { |
| return x.AppendText(nil) |
| } |
|
|
| |
| func (z *Int) UnmarshalText(text []byte) error { |
| if _, ok := z.setFromScanner(bytes.NewReader(text), 0); !ok { |
| return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Int", text) |
| } |
| return nil |
| } |
|
|
| |
| |
| |
|
|
| |
| func (x *Int) MarshalJSON() ([]byte, error) { |
| if x == nil { |
| return []byte("null"), nil |
| } |
| return x.abs.itoa(x.neg, 10), nil |
| } |
|
|
| |
| func (z *Int) UnmarshalJSON(text []byte) error { |
| |
| if string(text) == "null" { |
| return nil |
| } |
| return z.UnmarshalText(text) |
| } |
|
|