@@ -17,7 +17,9 @@ use T = self::inst::T;
17
17
use char;
18
18
use cmp:: { Eq , Ord } ;
19
19
use cmp;
20
+ use to_str:: ToStr ;
20
21
use from_str:: FromStr ;
22
+ use num:: { ToStrRadix , FromStrRadix } ;
21
23
use num;
22
24
use num:: Num :: from_int;
23
25
use prelude:: * ;
@@ -212,70 +214,87 @@ impl T: num::Round {
212
214
pure fn fract(&self) -> T { 0 }
213
215
}
214
216
215
- /**
216
- * Parse a buffer of bytes
217
- *
218
- * # Arguments
219
- *
220
- * * buf - A byte buffer
221
- * * radix - The base of the number
222
- */
223
- pub pure fn parse_bytes(buf: &[u8], radix: uint) -> Option<T> {
224
- if vec::len(buf) == 0u { return None; }
225
- let mut i = vec::len(buf) - 1u;
226
- let mut start = 0u;
227
- let mut power = 1 as T;
228
-
229
- if buf[0] == ('-' as u8) {
230
- power = -1 as T;
231
- start = 1u;
232
- }
233
- let mut n = 0 as T;
234
- loop {
235
- match char::to_digit(buf[i] as char, radix) {
236
- Some(d) => n += (d as T) * power,
237
- None => return None
238
- }
239
- power *= radix as T;
240
- if i <= start { return Some(n); }
241
- i -= 1u;
242
- };
217
+ // String conversion functions and impl str -> num
218
+
219
+ /// Parse a string as a number in base 10.
220
+ #[inline(always)]
221
+ pub pure fn from_str(s: &str) -> Option<T> {
222
+ num::from_str_common(s, 10u, true, false, false,
223
+ num::ExpNone, false)
243
224
}
244
225
245
- /// Parse a string to an int
226
+ /// Parse a string as a number in the given base.
246
227
#[inline(always)]
247
- pub pure fn from_str(s: &str) -> Option<T>
248
- {
249
- parse_bytes(str::to_bytes(s), 10u)
228
+ pub pure fn from_str_radix(s: &str, radix: uint) -> Option<T> {
229
+ num::from_str_common(s, radix, true, false, false,
230
+ num::ExpNone, false)
231
+ }
232
+
233
+ /// Parse a byte slice as a number in the given base.
234
+ #[inline(always)]
235
+ pub pure fn parse_bytes(buf: &[u8], radix: uint) -> Option<T> {
236
+ num::from_str_bytes_common(buf, radix, true, false, false,
237
+ num::ExpNone, false)
250
238
}
251
239
252
240
impl T : FromStr {
253
241
#[inline(always)]
254
- static pure fn from_str(s: &str) -> Option<T> { from_str(s) }
242
+ static pure fn from_str(s: &str) -> Option<T> {
243
+ from_str(s)
244
+ }
255
245
}
256
246
257
- /// Convert to a string in a given base
258
- #[inline(always)]
259
- pub pure fn to_str(n: T, radix: uint) -> ~str {
260
- do to_str_bytes(n, radix) |slice| {
261
- do vec::as_imm_buf(slice) |p, len| {
262
- unsafe { str::raw::from_buf_len(p, len) }
263
- }
247
+ impl T : FromStrRadix {
248
+ #[inline(always)]
249
+ static pure fn from_str_radix(&self, s: &str, radix: uint) -> Option<T> {
250
+ from_str_radix(s, radix)
264
251
}
265
252
}
266
253
254
+ // String conversion functions and impl num -> str
255
+
256
+ /// Convert to a string as a byte slice in a given base.
267
257
#[inline(always)]
268
258
pub pure fn to_str_bytes<U>(n: T, radix: uint, f: fn(v: &[u8]) -> U) -> U {
269
- if n < 0 as T {
270
- uint::to_str_bytes(true, -n as uint, radix, f)
271
- } else {
272
- uint::to_str_bytes(false, n as uint, radix, f)
273
- }
259
+ let (buf, _) = num::to_str_bytes_common(&n, radix, false, false,
260
+ num::SignNeg, num::DigAll);
261
+ f(buf)
274
262
}
275
263
276
- /// Convert to a string
264
+ /// Convert to a string in base 10.
277
265
#[inline(always)]
278
- pub pure fn str(i: T) -> ~str { return to_str(i, 10u); }
266
+ pub pure fn to_str(num: T) -> ~str {
267
+ let (buf, _) = num::to_str_common(&num, 10u, false, false,
268
+ num::SignNeg, num::DigAll);
269
+ buf
270
+ }
271
+
272
+ /// Convert to a string in a given base.
273
+ #[inline(always)]
274
+ pub pure fn to_str_radix(num: T, radix: uint) -> ~str {
275
+ let (buf, _) = num::to_str_common(&num, radix, false, false,
276
+ num::SignNeg, num::DigAll);
277
+ buf
278
+ }
279
+
280
+ /// Convert to a string.
281
+ /// *Deprecated*, use to_str() instead.
282
+ #[inline(always)]
283
+ pub pure fn str(i: T) -> ~str { to_str(i) }
284
+
285
+ impl T : ToStr {
286
+ #[inline(always)]
287
+ pure fn to_str() -> ~str {
288
+ to_str(self)
289
+ }
290
+ }
291
+
292
+ impl T : ToStrRadix {
293
+ #[inline(always)]
294
+ pure fn to_str_radix(&self, radix: uint) -> ~str {
295
+ to_str_radix(*self, radix)
296
+ }
297
+ }
279
298
280
299
#[test]
281
300
fn test_from_str() {
@@ -322,11 +341,66 @@ fn test_parse_bytes() {
322
341
323
342
#[test]
324
343
fn test_to_str() {
325
- assert (to_str(0 as T, 10u) == ~" 0 ");
326
- assert (to_str(1 as T, 10u) == ~" 1 ");
327
- assert (to_str(-1 as T, 10u) == ~" -1 ");
328
- assert (to_str(127 as T, 16u) == ~" 7 f");
329
- assert (to_str(100 as T, 10u) == ~" 100 ");
344
+ assert (to_str_radix(0 as T, 10u) == ~" 0 ");
345
+ assert (to_str_radix(1 as T, 10u) == ~" 1 ");
346
+ assert (to_str_radix(-1 as T, 10u) == ~" -1 ");
347
+ assert (to_str_radix(127 as T, 16u) == ~" 7 f");
348
+ assert (to_str_radix(100 as T, 10u) == ~" 100 ");
349
+
350
+ }
351
+
352
+ #[test]
353
+ fn test_int_to_str_overflow() {
354
+ let mut i8_val: i8 = 127_i8;
355
+ assert (i8::to_str(i8_val) == ~" 127 ");
356
+
357
+ i8_val += 1 as i8;
358
+ assert (i8::to_str(i8_val) == ~" -128 ");
359
+
360
+ let mut i16_val: i16 = 32_767_i16;
361
+ assert (i16::to_str(i16_val) == ~" 32767 ");
362
+
363
+ i16_val += 1 as i16;
364
+ assert (i16::to_str(i16_val) == ~" -32768 ");
365
+
366
+ let mut i32_val: i32 = 2_147_483_647_i32;
367
+ assert (i32::to_str(i32_val) == ~" 2147483647 ");
368
+
369
+ i32_val += 1 as i32;
370
+ assert (i32::to_str(i32_val) == ~" -2147483648 ");
371
+
372
+ let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
373
+ assert (i64::to_str(i64_val) == ~" 9223372036854775807 ");
374
+
375
+ i64_val += 1 as i64;
376
+ assert (i64::to_str(i64_val) == ~" -9223372036854775808 ");
377
+ }
378
+
379
+ #[test]
380
+ fn test_int_from_str_overflow() {
381
+ let mut i8_val: i8 = 127_i8;
382
+ assert (i8::from_str(~" 127 ") == Some(i8_val));
383
+
384
+ i8_val += 1 as i8;
385
+ assert (i8::from_str(~" -128 ") == Some(i8_val));
386
+
387
+ let mut i16_val: i16 = 32_767_i16;
388
+ assert (i16::from_str(~" 32767 ") == Some(i16_val));
389
+
390
+ i16_val += 1 as i16;
391
+ assert (i16::from_str(~" -32768 ") == Some(i16_val));
392
+
393
+ let mut i32_val: i32 = 2_147_483_647_i32;
394
+ assert (i32::from_str(~" 2147483647 ") == Some(i32_val));
395
+
396
+ i32_val += 1 as i32;
397
+ assert (i32::from_str(~" -2147483648 ") == Some(i32_val));
398
+
399
+ let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
400
+ assert (i64::from_str(~" 9223372036854775807 ") == Some(i64_val));
401
+
402
+ i64_val += 1 as i64;
403
+ assert (i64::from_str(~" -9223372036854775808 ") == Some(i64_val));
330
404
}
331
405
332
406
#[test]
0 commit comments