Source file
src/math/big/intmarsh.go
1
2
3
4
5
6
7 package big
8
9 import (
10 "bytes"
11 "fmt"
12 )
13
14
15 const intGobVersion byte = 1
16
17
18 func (x *Int) GobEncode() ([]byte, error) {
19 if x == nil {
20 return nil, nil
21 }
22 buf := make([]byte, 1+len(x.abs)*_S)
23 i := x.abs.bytes(buf) - 1
24 b := intGobVersion << 1
25 if x.neg {
26 b |= 1
27 }
28 buf[i] = b
29 return buf[i:], nil
30 }
31
32
33 func (z *Int) GobDecode(buf []byte) error {
34 if len(buf) == 0 {
35
36 *z = Int{}
37 return nil
38 }
39 b := buf[0]
40 if b>>1 != intGobVersion {
41 return fmt.Errorf("Int.GobDecode: encoding version %d not supported", b>>1)
42 }
43 z.neg = b&1 != 0
44 z.abs = z.abs.setBytes(buf[1:])
45 return nil
46 }
47
48
49 func (x *Int) AppendText(b []byte) (text []byte, err error) {
50 return x.Append(b, 10), nil
51 }
52
53
54 func (x *Int) MarshalText() (text []byte, err error) {
55 return x.AppendText(nil)
56 }
57
58
59 func (z *Int) UnmarshalText(text []byte) error {
60 if _, ok := z.setFromScanner(bytes.NewReader(text), 0); !ok {
61 return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Int", text)
62 }
63 return nil
64 }
65
66
67
68
69
70
71 func (x *Int) MarshalJSON() ([]byte, error) {
72 if x == nil {
73 return []byte("null"), nil
74 }
75 return x.abs.itoa(x.neg, 10), nil
76 }
77
78
79 func (z *Int) UnmarshalJSON(text []byte) error {
80
81 if string(text) == "null" {
82 return nil
83 }
84 return z.UnmarshalText(text)
85 }
86
View as plain text