Skip to content

Commit 26e0aaf

Browse files
committed
Added NumStrConv trait and impls for the string conversion functions
1 parent adac6cb commit 26e0aaf

File tree

1 file changed

+108
-1
lines changed

1 file changed

+108
-1
lines changed

src/libcore/num/strconv.rs

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ use char;
1515
use str;
1616
use kinds::Copy;
1717
use vec;
18-
use num::*;
18+
use num::{NumCast, Zero, One, cast, pow_with_uint};
19+
use num::{Round, RoundToZero,
20+
is_NaN, is_infinity, is_neg_infinity, is_neg_zero,
21+
infinity, neg_infinity, NaN};
22+
use float;
23+
use f32;
24+
use f64;
1925

2026
pub enum ExponentFormat {
2127
ExpNone,
@@ -35,6 +41,107 @@ pub enum SignFormat {
3541
SignAll
3642
}
3743

44+
pub trait NumStrConv {
45+
static fn has_NaN() -> bool;
46+
static fn has_inf() -> bool;
47+
static fn has_neg_inf() -> bool;
48+
static fn has_neg_zero() -> bool;
49+
50+
static fn NaN() -> Option<Self>;
51+
static fn inf() -> Option<Self>;
52+
static fn neg_inf() -> Option<Self>;
53+
static fn neg_zero() -> Option<Self>;
54+
55+
fn is_NaN(&self) -> bool;
56+
fn is_inf(&self) -> bool;
57+
fn is_neg_inf(&self) -> bool;
58+
fn is_neg_zero(&self) -> bool;
59+
60+
fn round_to_zero(&self) -> Self;
61+
fn split_at_dot(&self) -> (Self, Self);
62+
63+
}
64+
65+
macro_rules! impl_NumStrConv_Floating (
66+
($t:ty) => (
67+
impl NumStrConv for $t {
68+
static fn has_NaN() -> bool { true }
69+
static fn has_inf() -> bool { true }
70+
static fn has_neg_inf() -> bool { true }
71+
static fn has_neg_zero() -> bool { true }
72+
73+
static fn NaN() -> Option<$t> { Some( 0.0 / 0.0) }
74+
static fn inf() -> Option<$t> { Some( 1.0 / 0.0) }
75+
static fn neg_inf() -> Option<$t> { Some(-1.0 / 0.0) }
76+
static fn neg_zero() -> Option<$t> { Some(-0.0 ) }
77+
78+
fn is_NaN(&self) -> bool { *self != *self }
79+
fn is_inf(&self) -> bool {
80+
*self == NumStrConv::inf().unwrap()
81+
}
82+
fn is_neg_inf(&self) -> bool {
83+
*self == NumStrConv::neg_inf().unwrap()
84+
}
85+
fn is_neg_zero(&self) -> bool {
86+
*self == 0.0 && (1.0 / *self).is_neg_inf()
87+
}
88+
89+
fn round_to_zero(&self) -> $t {
90+
( if *self < 0.0 { f64::ceil(*self as f64) }
91+
else { f64::floor(*self as f64) }
92+
) as $t
93+
}
94+
95+
fn split_at_dot(&self) -> ($t, $t) {
96+
let r = self.round_to_zero();
97+
(r, *self - r)
98+
}
99+
}
100+
)
101+
)
102+
103+
macro_rules! impl_NumStrConv_Integer (
104+
($t:ty) => (
105+
impl NumStrConv for $t {
106+
static fn has_NaN() -> bool { false }
107+
static fn has_inf() -> bool { false }
108+
static fn has_neg_inf() -> bool { false }
109+
static fn has_neg_zero() -> bool { false }
110+
111+
static fn NaN() -> Option<$t> { None }
112+
static fn inf() -> Option<$t> { None }
113+
static fn neg_inf() -> Option<$t> { None }
114+
static fn neg_zero() -> Option<$t> { None }
115+
116+
fn is_NaN(&self) -> bool { false }
117+
fn is_inf(&self) -> bool { false }
118+
fn is_neg_inf(&self) -> bool { false }
119+
fn is_neg_zero(&self) -> bool { false }
120+
121+
fn round_to_zero(&self) -> $t { *self }
122+
123+
fn split_at_dot(&self) -> ($t, $t) { (*self, 0) }
124+
}
125+
)
126+
)
127+
128+
// XXX: Replace by two generic impls for traits 'Integral' and 'Floating'
129+
impl_NumStrConv_Floating!(float)
130+
impl_NumStrConv_Floating!(f32)
131+
impl_NumStrConv_Floating!(f64)
132+
133+
impl_NumStrConv_Integer!(int)
134+
impl_NumStrConv_Integer!(i8)
135+
impl_NumStrConv_Integer!(i16)
136+
impl_NumStrConv_Integer!(i32)
137+
impl_NumStrConv_Integer!(i64)
138+
139+
impl_NumStrConv_Integer!(uint)
140+
impl_NumStrConv_Integer!(u8)
141+
impl_NumStrConv_Integer!(u16)
142+
impl_NumStrConv_Integer!(u32)
143+
impl_NumStrConv_Integer!(u64)
144+
38145
/**
39146
* Converts a number to its string representation as a byte vector.
40147
* This is meant to be a common base implementation for all numeric string

0 commit comments

Comments
 (0)