@@ -179,13 +179,13 @@ impl Constructor for PyComplex {
179
179
"complex() can't take second arg if first is a string" . to_owned ( ) ,
180
180
) ) ;
181
181
}
182
- let value = s
182
+ let ( re , im ) = s
183
183
. to_str ( )
184
- . and_then ( |s| parse_str ( s . trim ( ) ) )
184
+ . and_then ( rustpython_literal :: complex :: parse_str)
185
185
. ok_or_else ( || {
186
186
vm. new_value_error ( "complex() arg is a malformed string" . to_owned ( ) )
187
187
} ) ?;
188
- return Self :: from ( value )
188
+ return Self :: from ( Complex64 { re , im } )
189
189
. into_ref_with_type ( vm, cls)
190
190
. map ( Into :: into) ;
191
191
} else {
@@ -494,7 +494,7 @@ impl Representable for PyComplex {
494
494
// TODO: when you fix this, move it to rustpython_common::complex::repr and update
495
495
// ast/src/unparse.rs + impl Display for Constant in ast/src/constant.rs
496
496
let Complex64 { re, im } = zelf. value ;
497
- Ok ( rustpython_literal:: float :: complex_to_string ( re, im) )
497
+ Ok ( rustpython_literal:: complex :: to_string ( re, im) )
498
498
}
499
499
}
500
500
@@ -519,40 +519,3 @@ pub struct ComplexArgs {
519
519
#[ pyarg( any, optional) ]
520
520
imag : OptionalArg < PyObjectRef > ,
521
521
}
522
-
523
- fn parse_str ( s : & str ) -> Option < Complex64 > {
524
- // Handle parentheses
525
- let s = match s. strip_prefix ( '(' ) {
526
- None => s,
527
- Some ( s) => match s. strip_suffix ( ')' ) {
528
- None => return None ,
529
- Some ( s) => s. trim ( ) ,
530
- } ,
531
- } ;
532
-
533
- let value = match s. strip_suffix ( |c| c == 'j' || c == 'J' ) {
534
- None => Complex64 :: new ( crate :: literal:: float:: parse_str ( s) ?, 0.0 ) ,
535
- Some ( mut s) => {
536
- let mut real = 0.0 ;
537
- // Find the central +/- operator. If it exists, parse the real part.
538
- for ( i, w) in s. as_bytes ( ) . windows ( 2 ) . enumerate ( ) {
539
- if ( w[ 1 ] == b'+' || w[ 1 ] == b'-' ) && !( w[ 0 ] == b'e' || w[ 0 ] == b'E' ) {
540
- real = crate :: literal:: float:: parse_str ( & s[ ..=i] ) ?;
541
- s = & s[ i + 1 ..] ;
542
- break ;
543
- }
544
- }
545
-
546
- let imag = match s {
547
- // "j", "+j"
548
- "" | "+" => 1.0 ,
549
- // "-j"
550
- "-" => -1.0 ,
551
- s => crate :: literal:: float:: parse_str ( s) ?,
552
- } ;
553
-
554
- Complex64 :: new ( real, imag)
555
- }
556
- } ;
557
- Some ( value)
558
- }
0 commit comments