1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 #![allow(unsigned_negation)]
16 macro_rules! uint_module (($T:ty) => (
18 // String conversion functions and impl num -> str
20 /// Convert to a string as a byte slice in a given base.
22 /// Use in place of x.to_string() when you do not need to store the string permanently
27 /// #![allow(deprecated)]
29 /// std::uint::to_str_bytes(123, 10, |v| {
30 /// assert!(v == "123".as_bytes());
34 #[deprecated = "just use .to_string(), or a BufWriter with write! if you mustn't allocate"]
35 pub fn to_str_bytes<U>(n: $T, radix: uint, f: |v: &[u8]| -> U) -> U {
36 use io::{Writer, Seek};
37 // The radix can be as low as 2, so we need at least 64 characters for a
38 // base 2 number, and then we need another for a possible '-' character.
39 let mut buf = [0u8, ..65];
41 let mut wr = ::io::BufWriter::new(&mut buf);
42 (write!(&mut wr, "{}", ::fmt::radix(n, radix as u8))).unwrap();
43 wr.tell().unwrap() as uint
51 use num::FromStrRadix;
54 pub fn test_from_str() {
55 assert_eq!(from_str::<$T>("0"), Some(0u as $T));
56 assert_eq!(from_str::<$T>("3"), Some(3u as $T));
57 assert_eq!(from_str::<$T>("10"), Some(10u as $T));
58 assert_eq!(from_str::<u32>("123456789"), Some(123456789 as u32));
59 assert_eq!(from_str::<$T>("00100"), Some(100u as $T));
61 assert_eq!(from_str::<$T>(""), None);
62 assert_eq!(from_str::<$T>(" "), None);
63 assert_eq!(from_str::<$T>("x"), None);
67 pub fn test_parse_bytes() {
68 assert_eq!(FromStrRadix::from_str_radix("123", 10), Some(123u as $T));
69 assert_eq!(FromStrRadix::from_str_radix("1001", 2), Some(9u as $T));
70 assert_eq!(FromStrRadix::from_str_radix("123", 8), Some(83u as $T));
71 assert_eq!(FromStrRadix::from_str_radix("123", 16), Some(291u as u16));
72 assert_eq!(FromStrRadix::from_str_radix("ffff", 16), Some(65535u as u16));
73 assert_eq!(FromStrRadix::from_str_radix("z", 36), Some(35u as $T));
75 assert_eq!(FromStrRadix::from_str_radix("Z", 10), None::<$T>);
76 assert_eq!(FromStrRadix::from_str_radix("_", 2), None::<$T>);
80 fn test_uint_to_str_overflow() {
81 let mut u8_val: u8 = 255_u8;
82 assert_eq!(u8_val.to_string(), "255".to_string());
85 assert_eq!(u8_val.to_string(), "0".to_string());
87 let mut u16_val: u16 = 65_535_u16;
88 assert_eq!(u16_val.to_string(), "65535".to_string());
91 assert_eq!(u16_val.to_string(), "0".to_string());
93 let mut u32_val: u32 = 4_294_967_295_u32;
94 assert_eq!(u32_val.to_string(), "4294967295".to_string());
97 assert_eq!(u32_val.to_string(), "0".to_string());
99 let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
100 assert_eq!(u64_val.to_string(), "18446744073709551615".to_string());
103 assert_eq!(u64_val.to_string(), "0".to_string());
107 fn test_uint_from_str_overflow() {
108 let mut u8_val: u8 = 255_u8;
109 assert_eq!(from_str::<u8>("255"), Some(u8_val));
110 assert_eq!(from_str::<u8>("256"), None);
113 assert_eq!(from_str::<u8>("0"), Some(u8_val));
114 assert_eq!(from_str::<u8>("-1"), None);
116 let mut u16_val: u16 = 65_535_u16;
117 assert_eq!(from_str::<u16>("65535"), Some(u16_val));
118 assert_eq!(from_str::<u16>("65536"), None);
121 assert_eq!(from_str::<u16>("0"), Some(u16_val));
122 assert_eq!(from_str::<u16>("-1"), None);
124 let mut u32_val: u32 = 4_294_967_295_u32;
125 assert_eq!(from_str::<u32>("4294967295"), Some(u32_val));
126 assert_eq!(from_str::<u32>("4294967296"), None);
129 assert_eq!(from_str::<u32>("0"), Some(u32_val));
130 assert_eq!(from_str::<u32>("-1"), None);
132 let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
133 assert_eq!(from_str::<u64>("18446744073709551615"), Some(u64_val));
134 assert_eq!(from_str::<u64>("18446744073709551616"), None);
137 assert_eq!(from_str::<u64>("0"), Some(u64_val));
138 assert_eq!(from_str::<u64>("-1"), None);