Source file
src/math/big/intconv.go
1
2
3
4
5
6
7 package big
8
9 import (
10 "errors"
11 "fmt"
12 "io"
13 )
14
15
16
17
18
19
20
21 func (x *Int) Text(base int) string {
22 if x == nil {
23 return "<nil>"
24 }
25 return string(x.abs.itoa(x.neg, base))
26 }
27
28
29
30 func (x *Int) Append(buf []byte, base int) []byte {
31 if x == nil {
32 return append(buf, "<nil>"...)
33 }
34 return append(buf, x.abs.itoa(x.neg, base)...)
35 }
36
37
38
39 func (x *Int) String() string {
40 return x.Text(10)
41 }
42
43
44 func writeMultiple(s fmt.State, text string, count int) {
45 if len(text) > 0 {
46 b := []byte(text)
47 for ; count > 0; count-- {
48 s.Write(b)
49 }
50 }
51 }
52
53 var _ fmt.Formatter = intOne
54
55
56
57
58
59
60
61
62
63
64
65
66 func (x *Int) Format(s fmt.State, ch rune) {
67
68 var base int
69 switch ch {
70 case 'b':
71 base = 2
72 case 'o', 'O':
73 base = 8
74 case 'd', 's', 'v':
75 base = 10
76 case 'x', 'X':
77 base = 16
78 default:
79
80 fmt.Fprintf(s, "%%!%c(big.Int=%s)", ch, x.String())
81 return
82 }
83
84 if x == nil {
85 fmt.Fprint(s, "<nil>")
86 return
87 }
88
89
90 sign := ""
91 switch {
92 case x.neg:
93 sign = "-"
94 case s.Flag('+'):
95 sign = "+"
96 case s.Flag(' '):
97 sign = " "
98 }
99
100
101 prefix := ""
102 if s.Flag('#') {
103 switch ch {
104 case 'b':
105 prefix = "0b"
106 case 'o':
107 prefix = "0"
108 case 'x':
109 prefix = "0x"
110 case 'X':
111 prefix = "0X"
112 }
113 }
114 if ch == 'O' {
115 prefix = "0o"
116 }
117
118 digits := x.abs.utoa(base)
119 if ch == 'X' {
120
121 for i, d := range digits {
122 if 'a' <= d && d <= 'z' {
123 digits[i] = 'A' + (d - 'a')
124 }
125 }
126 }
127
128
129 var left int
130 var zeros int
131 var right int
132
133
134 precision, precisionSet := s.Precision()
135 if precisionSet {
136 switch {
137 case len(digits) < precision:
138 zeros = precision - len(digits)
139 case len(digits) == 1 && digits[0] == '0' && precision == 0:
140 return
141 }
142 }
143
144
145 length := len(sign) + len(prefix) + zeros + len(digits)
146 if width, widthSet := s.Width(); widthSet && length < width {
147 switch d := width - length; {
148 case s.Flag('-'):
149
150 right = d
151 case s.Flag('0') && !precisionSet:
152
153 zeros = d
154 default:
155
156 left = d
157 }
158 }
159
160
161 writeMultiple(s, " ", left)
162 writeMultiple(s, sign, 1)
163 writeMultiple(s, prefix, 1)
164 writeMultiple(s, "0", zeros)
165 s.Write(digits)
166 writeMultiple(s, " ", right)
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180 func (z *Int) scan(r io.ByteScanner, base int) (*Int, int, error) {
181
182 neg, err := scanSign(r)
183 if err != nil {
184 return nil, 0, err
185 }
186
187
188 z.abs, base, _, err = z.abs.scan(r, base, false)
189 if err != nil {
190 return nil, base, err
191 }
192 z.neg = len(z.abs) > 0 && neg
193
194 return z, base, nil
195 }
196
197 func scanSign(r io.ByteScanner) (neg bool, err error) {
198 var ch byte
199 if ch, err = r.ReadByte(); err != nil {
200 return false, err
201 }
202 switch ch {
203 case '-':
204 neg = true
205 case '+':
206
207 default:
208 r.UnreadByte()
209 }
210 return
211 }
212
213
214
215 type byteReader struct {
216 fmt.ScanState
217 }
218
219 func (r byteReader) ReadByte() (byte, error) {
220 ch, size, err := r.ReadRune()
221 if size != 1 && err == nil {
222 err = fmt.Errorf("invalid rune %#U", ch)
223 }
224 return byte(ch), err
225 }
226
227 func (r byteReader) UnreadByte() error {
228 return r.UnreadRune()
229 }
230
231 var _ fmt.Scanner = intOne
232
233
234
235
236 func (z *Int) Scan(s fmt.ScanState, ch rune) error {
237 s.SkipSpace()
238 base := 0
239 switch ch {
240 case 'b':
241 base = 2
242 case 'o':
243 base = 8
244 case 'd':
245 base = 10
246 case 'x', 'X':
247 base = 16
248 case 's', 'v':
249
250 default:
251 return errors.New("Int.Scan: invalid verb")
252 }
253 _, _, err := z.scan(byteReader{s}, base)
254 return err
255 }
256
View as plain text