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 macro_rules! int_module (($T:ty) => (
16 // String conversion functions and impl str -> num
18 /// Parse a byte slice as a number in the given base
20 /// Yields an `Option` because `buf` may or may not actually be parseable.
25 /// let num = std::i64::parse_bytes([49,50,51,52,53,54,55,56,57], 10);
26 /// assert!(num == Some(123456789));
29 pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<$T> {
30 strconv::from_str_bytes_common(buf, radix, true, false, false,
31 strconv::ExpNone, false, false)
36 fn from_str(s: &str) -> Option<$T> {
37 strconv::from_str_common(s, 10u, true, false, false,
38 strconv::ExpNone, false, false)
42 impl FromStrRadix for $T {
44 fn from_str_radix(s: &str, radix: uint) -> Option<$T> {
45 strconv::from_str_common(s, radix, true, false, false,
46 strconv::ExpNone, false, false)
50 // String conversion functions and impl num -> str
52 /// Convert to a string as a byte slice in a given base.
54 /// Use in place of x.to_str() when you do not need to store the string permanently
59 /// std::int::to_str_bytes(123, 10, |v| {
60 /// assert!(v == "123".as_bytes());
64 pub fn to_str_bytes<U>(n: $T, radix: uint, f: |v: &[u8]| -> U) -> U {
65 use io::{Writer, Seek};
66 // The radix can be as low as 2, so we need at least 64 characters for a
67 // base 2 number, and then we need another for a possible '-' character.
68 let mut buf = [0u8, ..65];
70 let mut wr = ::io::BufWriter::new(buf);
71 (write!(&mut wr, "{}", ::fmt::radix(n, radix as u8))).unwrap();
72 wr.tell().unwrap() as uint
77 impl ToStrRadix for $T {
78 /// Convert to a string in a given base.
80 fn to_str_radix(&self, radix: uint) -> String {
81 format_strbuf!("{}", ::fmt::radix(*self, radix as u8))
96 assert_eq!(from_str::<$T>("0"), Some(0 as $T));
97 assert_eq!(from_str::<$T>("3"), Some(3 as $T));
98 assert_eq!(from_str::<$T>("10"), Some(10 as $T));
99 assert_eq!(from_str::<i32>("123456789"), Some(123456789 as i32));
100 assert_eq!(from_str::<$T>("00100"), Some(100 as $T));
102 assert_eq!(from_str::<$T>("-1"), Some(-1 as $T));
103 assert_eq!(from_str::<$T>("-3"), Some(-3 as $T));
104 assert_eq!(from_str::<$T>("-10"), Some(-10 as $T));
105 assert_eq!(from_str::<i32>("-123456789"), Some(-123456789 as i32));
106 assert_eq!(from_str::<$T>("-00100"), Some(-100 as $T));
108 assert!(from_str::<$T>(" ").is_none());
109 assert!(from_str::<$T>("x").is_none());
113 fn test_parse_bytes() {
115 assert_eq!(parse_bytes("123".as_bytes(), 10u), Some(123 as $T));
116 assert_eq!(parse_bytes("1001".as_bytes(), 2u), Some(9 as $T));
117 assert_eq!(parse_bytes("123".as_bytes(), 8u), Some(83 as $T));
118 assert_eq!(i32::parse_bytes("123".as_bytes(), 16u), Some(291 as i32));
119 assert_eq!(i32::parse_bytes("ffff".as_bytes(), 16u), Some(65535 as i32));
120 assert_eq!(i32::parse_bytes("FFFF".as_bytes(), 16u), Some(65535 as i32));
121 assert_eq!(parse_bytes("z".as_bytes(), 36u), Some(35 as $T));
122 assert_eq!(parse_bytes("Z".as_bytes(), 36u), Some(35 as $T));
124 assert_eq!(parse_bytes("-123".as_bytes(), 10u), Some(-123 as $T));
125 assert_eq!(parse_bytes("-1001".as_bytes(), 2u), Some(-9 as $T));
126 assert_eq!(parse_bytes("-123".as_bytes(), 8u), Some(-83 as $T));
127 assert_eq!(i32::parse_bytes("-123".as_bytes(), 16u), Some(-291 as i32));
128 assert_eq!(i32::parse_bytes("-ffff".as_bytes(), 16u), Some(-65535 as i32));
129 assert_eq!(i32::parse_bytes("-FFFF".as_bytes(), 16u), Some(-65535 as i32));
130 assert_eq!(parse_bytes("-z".as_bytes(), 36u), Some(-35 as $T));
131 assert_eq!(parse_bytes("-Z".as_bytes(), 36u), Some(-35 as $T));
133 assert!(parse_bytes("Z".as_bytes(), 35u).is_none());
134 assert!(parse_bytes("-9".as_bytes(), 2u).is_none());
139 assert_eq!((0 as $T).to_str_radix(10u), "0".to_string());
140 assert_eq!((1 as $T).to_str_radix(10u), "1".to_string());
141 assert_eq!((-1 as $T).to_str_radix(10u), "-1".to_string());
142 assert_eq!((127 as $T).to_str_radix(16u), "7f".to_string());
143 assert_eq!((100 as $T).to_str_radix(10u), "100".to_string());
148 fn test_int_to_str_overflow() {
149 let mut i8_val: i8 = 127_i8;
150 assert_eq!(i8_val.to_str(), "127".to_string());
153 assert_eq!(i8_val.to_str(), "-128".to_string());
155 let mut i16_val: i16 = 32_767_i16;
156 assert_eq!(i16_val.to_str(), "32767".to_string());
159 assert_eq!(i16_val.to_str(), "-32768".to_string());
161 let mut i32_val: i32 = 2_147_483_647_i32;
162 assert_eq!(i32_val.to_str(), "2147483647".to_string());
165 assert_eq!(i32_val.to_str(), "-2147483648".to_string());
167 let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
168 assert_eq!(i64_val.to_str(), "9223372036854775807".to_string());
171 assert_eq!(i64_val.to_str(), "-9223372036854775808".to_string());
175 fn test_int_from_str_overflow() {
176 let mut i8_val: i8 = 127_i8;
177 assert_eq!(from_str::<i8>("127"), Some(i8_val));
178 assert!(from_str::<i8>("128").is_none());
181 assert_eq!(from_str::<i8>("-128"), Some(i8_val));
182 assert!(from_str::<i8>("-129").is_none());
184 let mut i16_val: i16 = 32_767_i16;
185 assert_eq!(from_str::<i16>("32767"), Some(i16_val));
186 assert!(from_str::<i16>("32768").is_none());
189 assert_eq!(from_str::<i16>("-32768"), Some(i16_val));
190 assert!(from_str::<i16>("-32769").is_none());
192 let mut i32_val: i32 = 2_147_483_647_i32;
193 assert_eq!(from_str::<i32>("2147483647"), Some(i32_val));
194 assert!(from_str::<i32>("2147483648").is_none());
197 assert_eq!(from_str::<i32>("-2147483648"), Some(i32_val));
198 assert!(from_str::<i32>("-2147483649").is_none());
200 let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
201 assert_eq!(from_str::<i64>("9223372036854775807"), Some(i64_val));
202 assert!(from_str::<i64>("9223372036854775808").is_none());
205 assert_eq!(from_str::<i64>("-9223372036854775808"), Some(i64_val));
206 assert!(from_str::<i64>("-9223372036854775809").is_none());