OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package strconv | 5 package strconv |
6 | 6 |
7 import "os" | 7 import "os" |
8 | 8 |
9 type NumError struct { | 9 type NumError struct { |
10 Num string | 10 Num string |
11 Error os.Error | 11 Error os.Error |
12 } | 12 } |
13 | 13 |
14 func (e *NumError) String() string { return `parsing "` + e.Num + `": ` + e.Erro
r.String() } | 14 func (e *NumError) String() string { return `parsing "` + e.Num + `": ` + e.Erro
r.String() } |
15 | 15 |
16 | |
17 func computeIntsize() uint { | 16 func computeIntsize() uint { |
18 siz := uint(8) | 17 siz := uint(8) |
19 for 1<<siz != 0 { | 18 for 1<<siz != 0 { |
20 siz *= 2 | 19 siz *= 2 |
21 } | 20 } |
22 return siz | 21 return siz |
23 } | 22 } |
24 | 23 |
25 var IntSize = computeIntsize() | 24 var IntSize = computeIntsize() |
26 | 25 |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 if neg { | 165 if neg { |
167 n = -n | 166 n = -n |
168 } | 167 } |
169 return n, nil | 168 return n, nil |
170 } | 169 } |
171 | 170 |
172 // Atoi64 is like Atoui64 but allows signed numbers and | 171 // Atoi64 is like Atoui64 but allows signed numbers and |
173 // returns its result in an int64. | 172 // returns its result in an int64. |
174 func Atoi64(s string) (i int64, err os.Error) { return Btoi64(s, 10) } | 173 func Atoi64(s string) (i int64, err os.Error) { return Btoi64(s, 10) } |
175 | 174 |
176 | |
177 // Atoui is like Atoui64 but returns its result as a uint. | 175 // Atoui is like Atoui64 but returns its result as a uint. |
178 func Atoui(s string) (i uint, err os.Error) { | 176 func Atoui(s string) (i uint, err os.Error) { |
179 i1, e1 := Atoui64(s) | 177 i1, e1 := Atoui64(s) |
180 if e1 != nil && e1.(*NumError).Error != os.ERANGE { | 178 if e1 != nil && e1.(*NumError).Error != os.ERANGE { |
181 return 0, e1 | 179 return 0, e1 |
182 } | 180 } |
183 i = uint(i1) | 181 i = uint(i1) |
184 if uint64(i) != i1 { | 182 if uint64(i) != i1 { |
185 return ^uint(0), &NumError{s, os.ERANGE} | 183 return ^uint(0), &NumError{s, os.ERANGE} |
186 } | 184 } |
187 return i, nil | 185 return i, nil |
188 } | 186 } |
189 | 187 |
190 // Atoi is like Atoi64 but returns its result as an int. | 188 // Atoi is like Atoi64 but returns its result as an int. |
191 func Atoi(s string) (i int, err os.Error) { | 189 func Atoi(s string) (i int, err os.Error) { |
192 i1, e1 := Atoi64(s) | 190 i1, e1 := Atoi64(s) |
193 if e1 != nil && e1.(*NumError).Error != os.ERANGE { | 191 if e1 != nil && e1.(*NumError).Error != os.ERANGE { |
194 return 0, e1 | 192 return 0, e1 |
195 } | 193 } |
196 i = int(i1) | 194 i = int(i1) |
197 if int64(i) != i1 { | 195 if int64(i) != i1 { |
198 if i1 < 0 { | 196 if i1 < 0 { |
199 return -1 << (IntSize - 1), &NumError{s, os.ERANGE} | 197 return -1 << (IntSize - 1), &NumError{s, os.ERANGE} |
200 } | 198 } |
201 return 1<<(IntSize-1) - 1, &NumError{s, os.ERANGE} | 199 return 1<<(IntSize-1) - 1, &NumError{s, os.ERANGE} |
202 } | 200 } |
203 return i, nil | 201 return i, nil |
204 } | 202 } |
OLD | NEW |