handles case where imaginary part is just 'i' or '-i', adds tests
This commit is contained in:
parent
506fbaf4e7
commit
e5e8266009
|
@ -755,7 +755,7 @@ impl<T> FromStr for Complex<T> where
|
||||||
|
|
||||||
let mut a = match split_p.next() {
|
let mut a = match split_p.next() {
|
||||||
None => return Err(ParseComplexError { kind: ComplexErrorKind::ParseError }),
|
None => return Err(ParseComplexError { kind: ComplexErrorKind::ParseError }),
|
||||||
Some(s) => s.to_string()
|
Some(s) => s.trim_right().to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut b = match split_p.next() {
|
let mut b = match split_p.next() {
|
||||||
|
@ -765,7 +765,7 @@ impl<T> FromStr for Complex<T> where
|
||||||
|
|
||||||
a = match split_m.next() {
|
a = match split_m.next() {
|
||||||
None => return Err(ParseComplexError { kind: ComplexErrorKind::ParseError }),
|
None => return Err(ParseComplexError { kind: ComplexErrorKind::ParseError }),
|
||||||
Some(s) => s.to_string()
|
Some(s) => s.trim_right().to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
let c = match split_m.next() {
|
let c = match split_m.next() {
|
||||||
|
@ -777,12 +777,12 @@ impl<T> FromStr for Complex<T> where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
"-".to_string() + s
|
"-".to_string() + s.trim_left()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
c
|
c
|
||||||
},
|
},
|
||||||
Some(s) => s.to_string()
|
Some(s) => s.trim_left().to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
let re = match a.rfind('i') {
|
let re = match a.rfind('i') {
|
||||||
|
@ -798,12 +798,51 @@ impl<T> FromStr for Complex<T> where
|
||||||
|
|
||||||
let im = match a.rfind('i') {
|
let im = match a.rfind('i') {
|
||||||
None => {
|
None => {
|
||||||
b.pop();
|
// a is real
|
||||||
|
match b.pop() {
|
||||||
|
// b was empty
|
||||||
|
None => return Err(ParseComplexError { kind: ComplexErrorKind::ParseError }),
|
||||||
|
Some(c) => {
|
||||||
|
if c == 'i' {
|
||||||
|
// b is imaginary
|
||||||
|
if b.is_empty() {
|
||||||
|
// b was just 'i'
|
||||||
|
b = "1".to_string();
|
||||||
|
} else if b == "-" {
|
||||||
|
// b was just '-i'
|
||||||
|
b = "-1".to_string();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// given a sum or difference of two reals
|
||||||
|
return Err(ParseComplexError { kind: ComplexErrorKind::ParseError });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try!(T::from_str(&b)
|
try!(T::from_str(&b)
|
||||||
.map_err(|_| ParseComplexError { kind: ComplexErrorKind::ParseError }))
|
.map_err(|_| ParseComplexError { kind: ComplexErrorKind::ParseError }))
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
a.pop();
|
// a contains an 'i'
|
||||||
|
match a.pop() {
|
||||||
|
None => return Err(ParseComplexError { kind: ComplexErrorKind::ParseError }),
|
||||||
|
Some(c) => {
|
||||||
|
if c == 'i' {
|
||||||
|
// a is imaginary
|
||||||
|
if a.is_empty() {
|
||||||
|
// a was just 'i'
|
||||||
|
a = "1".to_string();
|
||||||
|
} else if a == "-" {
|
||||||
|
// a was just '-i'
|
||||||
|
a = "-1".to_string();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// given a sum or difference of two reals
|
||||||
|
return Err(ParseComplexError { kind: ComplexErrorKind::ParseError });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try!(T::from_str(&a)
|
try!(T::from_str(&a)
|
||||||
.map_err(|_| ParseComplexError { kind: ComplexErrorKind::ParseError }))
|
.map_err(|_| ParseComplexError { kind: ComplexErrorKind::ParseError }))
|
||||||
}
|
}
|
||||||
|
@ -881,6 +920,7 @@ mod test {
|
||||||
|
|
||||||
use super::{Complex64, Complex};
|
use super::{Complex64, Complex};
|
||||||
use std::f64;
|
use std::f64;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use traits::{Zero, One, Float};
|
use traits::{Zero, One, Float};
|
||||||
|
|
||||||
|
@ -1502,4 +1542,36 @@ mod test {
|
||||||
assert!(!b.is_normal());
|
assert!(!b.is_normal());
|
||||||
assert!(_1_1i.is_normal());
|
assert!(_1_1i.is_normal());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_from_string() {
|
||||||
|
fn test(z: Complex64, s: String) {
|
||||||
|
assert_eq!(FromStr::from_str(&s), Ok(z));
|
||||||
|
}
|
||||||
|
test(_0_0i, "0".to_string());
|
||||||
|
test(_0_0i, "0i".to_string());
|
||||||
|
test(_0_0i, "-0".to_string());
|
||||||
|
test(_0_0i, "-0i".to_string());
|
||||||
|
test(_0_0i, "0 + 0i".to_string());
|
||||||
|
test(_0_0i, "0 - 0i".to_string());
|
||||||
|
|
||||||
|
test(_1_0i, "1".to_string());
|
||||||
|
test(_1_0i, "1 + 0i".to_string());
|
||||||
|
test(_1_0i, "1 - 0i".to_string());
|
||||||
|
|
||||||
|
test(_1_1i, "1 + i".to_string());
|
||||||
|
test(_1_1i, "1 + 1i".to_string());
|
||||||
|
|
||||||
|
test(_0_1i, "i".to_string());
|
||||||
|
test(_0_1i, "1i".to_string());
|
||||||
|
test(_0_1i, "0 + i".to_string());
|
||||||
|
test(_0_1i, "-0 + i".to_string());
|
||||||
|
test(_0_1i, "0 + 1i".to_string());
|
||||||
|
test(_0_1i, "-0 + 1i".to_string());
|
||||||
|
|
||||||
|
test(_neg1_1i, "-1 + i".to_string());
|
||||||
|
test(_neg1_1i, "-1 + 1i".to_string());
|
||||||
|
|
||||||
|
test(_05_05i, "0.5 + 0.5i".to_string());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue