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
67 func (x *Int) Format(s fmt.State, ch rune) {
68
69 var base int
70 switch ch {
71 case 'b':
72 base = 2
73 case 'o', 'O':
74 base = 8
75 case 'd', 's', 'v':
76 base = 10
77 case 'x', 'X':
78 base = 16
79 default:
80
81 fmt.Fprintf(s, "%%!%c(big.Int=%s)", ch, x.String())
82 return
83 }
84
85 if x == nil {
86 fmt.Fprint(s, "<nil>")
87 return
88 }
89
90
91 sign := ""
92 switch {
93 case x.neg:
94 sign = "-"
95 case s.Flag('+'):
96 sign = "+"
97 case s.Flag(' '):
98 sign = " "
99 }
100
101
102 prefix := ""
103 if s.Flag('#') {
104 switch ch {
105 case 'b':
106 prefix = "0b"
107 case 'o':
108 prefix = "0"
109 case 'x':
110 prefix = "0x"
111 case 'X':
112 prefix = "0X"
113 }
114 }
115 if ch == 'O' {
116 prefix = "0o"
117 }
118
119 digits := x.abs.utoa(base)
120 if ch == 'X' {
121
122 for i, d := range digits {
123 if 'a' <= d && d <= 'z' {
124 digits[i] = 'A' + (d - 'a')
125 }
126 }
127 }
128
129
130 var left int
131 var zeros int
132 var right int
133
134
135 precision, precisionSet := s.Precision()
136 if precisionSet {
137 switch {
138 case len(digits) < precision:
139 zeros = precision - len(digits)
140 case len(digits) == 1 && digits[0] == '0' && precision == 0:
141 return
142 }
143 }
144
145
146 length := len(sign) + len(prefix) + zeros + len(digits)
147 if width, widthSet := s.Width(); widthSet && length < width {
148 switch d := width - length; {
149 case s.Flag('-'):
150
151 right = d
152 case s.Flag('0') && !precisionSet:
153
154 zeros = d
155 default:
156
157 left = d
158 }
159 }
160
161
162 writeMultiple(s, " ", left)
163 writeMultiple(s, sign, 1)
164 writeMultiple(s, prefix, 1)
165 writeMultiple(s, "0", zeros)
166 s.Write(digits)
167 writeMultiple(s, " ", right)
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182 func (z *Int) scan(r io.ByteScanner, base int) (*Int, int, error) {
183
184 neg, err := scanSign(r)
185 if err != nil {
186 return nil, 0, err
187 }
188
189
190 z.abs, base, _, err = z.abs.scan(r, base, false)
191 if err != nil {
192 return nil, base, err
193 }
194 z.neg = len(z.abs) > 0 && neg
195
196 return z, base, nil
197 }
198
199 func scanSign(r io.ByteScanner) (neg bool, err error) {
200 var ch byte
201 if ch, err = r.ReadByte(); err != nil {
202 return false, err
203 }
204 switch ch {
205 case '-':
206 neg = true
207 case '+':
208
209 default:
210 r.UnreadByte()
211 }
212 return
213 }
214
215
216
217 type byteReader struct {
218 fmt.ScanState
219 }
220
221 func (r byteReader) ReadByte() (byte, error) {
222 ch, size, err := r.ReadRune()
223 if size != 1 && err == nil {
224 err = fmt.Errorf("invalid rune %#U", ch)
225 }
226 return byte(ch), err
227 }
228
229 func (r byteReader) UnreadByte() error {
230 return r.UnreadRune()
231 }
232
233 var _ fmt.Scanner = intOne
234
235
236
237
238 func (z *Int) Scan(s fmt.ScanState, ch rune) error {
239 s.SkipSpace()
240 base := 0
241 switch ch {
242 case 'b':
243 base = 2
244 case 'o':
245 base = 8
246 case 'd':
247 base = 10
248 case 'x', 'X':
249 base = 16
250 case 's', 'v':
251
252 default:
253 return errors.New("Int.Scan: invalid verb")
254 }
255 _, _, err := z.scan(byteReader{s}, base)
256 return err
257 }
258
View as plain text