| 155 | |
| 156 | |
| 157 | static bool |
| 158 | to_complex_int( |
| 159 | const Py_UCS4 *item, const Py_UCS4 *token_end, |
| 160 | double *p_real, double *p_imag, |
| 161 | Py_UCS4 imaginary_unit, bool allow_parens) |
| 162 | { |
| 163 | const Py_UCS4 *p_end; |
| 164 | bool unmatched_opening_paren = false; |
| 165 | |
| 166 | /* Remove whitespace before the possibly leading '(' */ |
| 167 | while (Py_UNICODE_ISSPACE(*item)) { |
| 168 | ++item; |
| 169 | } |
| 170 | if (allow_parens && (*item == '(')) { |
| 171 | unmatched_opening_paren = true; |
| 172 | ++item; |
| 173 | /* Allow whitespace within the parentheses: "( 1j)" */ |
| 174 | while (Py_UNICODE_ISSPACE(*item)) { |
| 175 | ++item; |
| 176 | } |
| 177 | } |
| 178 | if (double_from_ucs4(item, token_end, false, p_real, &p_end) < 0) { |
| 179 | return false; |
| 180 | } |
| 181 | if (p_end == token_end) { |
| 182 | // No imaginary part in the string (e.g. "3.5") |
| 183 | *p_imag = 0.0; |
| 184 | return !unmatched_opening_paren; |
| 185 | } |
| 186 | if (*p_end == imaginary_unit) { |
| 187 | /* Only an imaginary part (e.g "1.5j") */ |
| 188 | *p_imag = *p_real; |
| 189 | *p_real = 0.0; |
| 190 | ++p_end; |
| 191 | } |
| 192 | else if (*p_end == '+' || *p_end == '-') { |
| 193 | /* Imaginary part still to parse */ |
| 194 | if (*p_end == '+') { |
| 195 | ++p_end; /* Advance to support +- (and ++) */ |
| 196 | } |
| 197 | if (double_from_ucs4(p_end, token_end, false, p_imag, &p_end) < 0) { |
| 198 | return false; |
| 199 | } |
| 200 | if (*p_end != imaginary_unit) { |
| 201 | return false; |
| 202 | } |
| 203 | ++p_end; |
| 204 | } |
| 205 | else { |
| 206 | *p_imag = 0; |
| 207 | } |
| 208 | |
| 209 | if (unmatched_opening_paren) { |
| 210 | /* Allow whitespace inside brackets as in "(1+2j )" or "( 1j )" */ |
| 211 | while (Py_UNICODE_ISSPACE(*p_end)) { |
| 212 | ++p_end; |
| 213 | } |
| 214 | if (*p_end == ')') { |
no test coverage detected