...
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) MarshalText() (text []byte, err error) {
50 if x == nil {
51 return []byte("<nil>"), nil
52 }
53 return x.abs.itoa(x.neg, 10), nil
54 }
55
56
57 func (z *Int) UnmarshalText(text []byte) error {
58 if _, ok := z.setFromScanner(bytes.NewReader(text), 0); !ok {
59 return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Int", text)
60 }
61 return nil
62 }
63
64
65
66
67
68
69 func (x *Int) MarshalJSON() ([]byte, error) {
70 return x.MarshalText()
71 }
72
73
74 func (z *Int) UnmarshalJSON(text []byte) error {
75
76 if string(text) == "null" {
77 return nil
78 }
79 return z.UnmarshalText(text)
80 }
81
View as plain text