From 14d323f5a37364a423fbbc361892e2d8194b8c96 Mon Sep 17 00:00:00 2001 From: Alan Liddell Date: Thu, 13 Jul 2017 14:13:04 -0400 Subject: [PATCH] Allows 'j' for imaginary unit --- complex/src/lib.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/complex/src/lib.rs b/complex/src/lib.rs index 348f838..e71cf5b 100644 --- a/complex/src/lib.rs +++ b/complex/src/lib.rs @@ -750,7 +750,10 @@ impl FromStr for Complex where /// Parses `a +/- bi`; `ai +/- b`; `a`; or `bi` where `a` and `b` are of type `T` fn from_str(s: &str) -> Result, ParseComplexError> { - let imag = 'i'; + let imag = match s.rfind('j') { + None => 'i', + _ => 'j' + }; let mut a = String::with_capacity(s.len()); let mut b = String::with_capacity(s.len()); @@ -1572,41 +1575,71 @@ mod test { } test(_0_0i, "0".to_string()); test(_0_0i, "0i".to_string()); + test(_0_0i, "0j".to_string()); test(_0_0i, "-0".to_string()); test(_0_0i, "-0i".to_string()); + test(_0_0i, "-0j".to_string()); test(_0_0i, "0 + 0i".to_string()); + test(_0_0i, "0 + 0j".to_string()); test(_0_0i, "0+0i".to_string()); + test(_0_0i, "0+0j".to_string()); test(_0_0i, "0 - 0i".to_string()); + test(_0_0i, "0 - 0j".to_string()); test(_0_0i, "0-0i".to_string()); + test(_0_0i, "0-0j".to_string()); test(_1_0i, "1".to_string()); test(_1_0i, "1 + 0i".to_string()); + test(_1_0i, "1 + 0j".to_string()); test(_1_0i, "1+0i".to_string()); + test(_1_0i, "1+0j".to_string()); test(_1_0i, "1 - 0i".to_string()); + test(_1_0i, "1 - 0j".to_string()); test(_1_0i, "1-0i".to_string()); + test(_1_0i, "1-0j".to_string()); test(_1_1i, "1 + i".to_string()); + test(_1_1i, "1 + j".to_string()); test(_1_1i, "1+i".to_string()); + test(_1_1i, "1+j".to_string()); test(_1_1i, "1 + 1i".to_string()); + test(_1_1i, "1 + 1j".to_string()); test(_1_1i, "1+1i".to_string()); + test(_1_1i, "1+1j".to_string()); test(_0_1i, "i".to_string()); + test(_0_1i, "j".to_string()); test(_0_1i, "1i".to_string()); + test(_0_1i, "1j".to_string()); test(_0_1i, "0 + i".to_string()); + test(_0_1i, "0 + j".to_string()); test(_0_1i, "0+i".to_string()); + test(_0_1i, "0+j".to_string()); test(_0_1i, "-0 + i".to_string()); + test(_0_1i, "-0 + j".to_string()); test(_0_1i, "-0+i".to_string()); + test(_0_1i, "-0+j".to_string()); test(_0_1i, "0 + 1i".to_string()); + test(_0_1i, "0 + 1j".to_string()); test(_0_1i, "0+1i".to_string()); + test(_0_1i, "0+1j".to_string()); test(_0_1i, "-0 + 1i".to_string()); + test(_0_1i, "-0 + 1j".to_string()); test(_0_1i, "-0+1i".to_string()); + test(_0_1i, "-0+1j".to_string()); test(_neg1_1i, "-1 + i".to_string()); + test(_neg1_1i, "-1 + j".to_string()); test(_neg1_1i, "-1+i".to_string()); + test(_neg1_1i, "-1+j".to_string()); test(_neg1_1i, "-1 + 1i".to_string()); + test(_neg1_1i, "-1 + 1j".to_string()); test(_neg1_1i, "-1+1i".to_string()); + test(_neg1_1i, "-1+1j".to_string()); test(_05_05i, "0.5 + 0.5i".to_string()); + test(_05_05i, "0.5 + 0.5j".to_string()); test(_05_05i, "0.5+0.5i".to_string()); + test(_05_05i, "0.5+0.5j".to_string()); } }