]> git.lizzy.rs Git - rust.git/blob - src/libstd/num/int_macros.rs
std: Remove format_strbuf!()
[rust.git] / src / libstd / num / int_macros.rs
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.
4 //
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.
10
11 #![macro_escape]
12 #![doc(hidden)]
13
14 macro_rules! int_module (($T:ty) => (
15
16 // String conversion functions and impl str -> num
17
18 /// Parse a byte slice as a number in the given base
19 ///
20 /// Yields an `Option` because `buf` may or may not actually be parseable.
21 ///
22 /// # Examples
23 ///
24 /// ```
25 /// let num = std::i64::parse_bytes([49,50,51,52,53,54,55,56,57], 10);
26 /// assert!(num == Some(123456789));
27 /// ```
28 #[inline]
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)
32 }
33
34 impl FromStr for $T {
35     #[inline]
36     fn from_str(s: &str) -> Option<$T> {
37         strconv::from_str_common(s, 10u, true, false, false,
38                              strconv::ExpNone, false, false)
39     }
40 }
41
42 impl FromStrRadix for $T {
43     #[inline]
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)
47     }
48 }
49
50 // String conversion functions and impl num -> str
51
52 /// Convert to a string as a byte slice in a given base.
53 ///
54 /// Use in place of x.to_str() when you do not need to store the string permanently
55 ///
56 /// # Examples
57 ///
58 /// ```
59 /// std::int::to_str_bytes(123, 10, |v| {
60 ///     assert!(v == "123".as_bytes());
61 /// });
62 /// ```
63 #[inline]
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];
69     let amt = {
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
73     };
74     f(buf.slice(0, amt))
75 }
76
77 impl ToStrRadix for $T {
78     /// Convert to a string in a given base.
79     #[inline]
80     fn to_str_radix(&self, radix: uint) -> String {
81         format!("{}", ::fmt::radix(*self, radix as u8))
82     }
83 }
84
85 #[cfg(test)]
86 mod tests {
87     use prelude::*;
88     use super::*;
89
90     use i32;
91     use num::ToStrRadix;
92     use str::StrSlice;
93
94     #[test]
95     fn test_from_str() {
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));
101
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));
107
108         assert!(from_str::<$T>(" ").is_none());
109         assert!(from_str::<$T>("x").is_none());
110     }
111
112     #[test]
113     fn test_parse_bytes() {
114         use str::StrSlice;
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));
123
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));
132
133         assert!(parse_bytes("Z".as_bytes(), 35u).is_none());
134         assert!(parse_bytes("-9".as_bytes(), 2u).is_none());
135     }
136
137     #[test]
138     fn test_to_str() {
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());
144
145     }
146
147     #[test]
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());
151
152         i8_val += 1 as i8;
153         assert_eq!(i8_val.to_str(), "-128".to_string());
154
155         let mut i16_val: i16 = 32_767_i16;
156         assert_eq!(i16_val.to_str(), "32767".to_string());
157
158         i16_val += 1 as i16;
159         assert_eq!(i16_val.to_str(), "-32768".to_string());
160
161         let mut i32_val: i32 = 2_147_483_647_i32;
162         assert_eq!(i32_val.to_str(), "2147483647".to_string());
163
164         i32_val += 1 as i32;
165         assert_eq!(i32_val.to_str(), "-2147483648".to_string());
166
167         let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
168         assert_eq!(i64_val.to_str(), "9223372036854775807".to_string());
169
170         i64_val += 1 as i64;
171         assert_eq!(i64_val.to_str(), "-9223372036854775808".to_string());
172     }
173
174     #[test]
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());
179
180         i8_val += 1 as i8;
181         assert_eq!(from_str::<i8>("-128"), Some(i8_val));
182         assert!(from_str::<i8>("-129").is_none());
183
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());
187
188         i16_val += 1 as i16;
189         assert_eq!(from_str::<i16>("-32768"), Some(i16_val));
190         assert!(from_str::<i16>("-32769").is_none());
191
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());
195
196         i32_val += 1 as i32;
197         assert_eq!(from_str::<i32>("-2147483648"), Some(i32_val));
198         assert!(from_str::<i32>("-2147483649").is_none());
199
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());
203
204         i64_val += 1 as i64;
205         assert_eq!(from_str::<i64>("-9223372036854775808"), Some(i64_val));
206         assert!(from_str::<i64>("-9223372036854775809").is_none());
207     }
208 }
209
210 ))