Skip to content

Commit 49edd70

Browse files
committed
literal: reject internal whitespace in complex::parse_str
parse_str split the token then parsed each part with float::parse_str, which tolerates surrounding whitespace, so "1 +2j" parsed as (1+2j). Reject whitespace inside the token after stripping optional parentheses. Add unit tests and complex() snippet tests. Assisted-by: Claude
1 parent 5c36d5c commit 49edd70

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

crates/literal/src/complex.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ pub fn parse_str(s: &str) -> Option<(f64, f64)> {
7070
Some(s) => s.strip_suffix(')')?.trim(),
7171
};
7272

73+
// Whitespace is only allowed around the whole string and the optional
74+
// parentheses, never inside the numeric token. Reject it here so that
75+
// `float::parse_str` (which tolerates surrounding whitespace on a part)
76+
// does not let e.g. "1 +2j" through.
77+
if s.contains(char::is_whitespace) {
78+
return None;
79+
}
80+
7381
let value = match s.strip_suffix(|c| c == 'j' || c == 'J') {
7482
None => (float::parse_str(s)?, 0.0),
7583
Some(mut s) => {
@@ -96,3 +104,39 @@ pub fn parse_str(s: &str) -> Option<(f64, f64)> {
96104
};
97105
Some(value)
98106
}
107+
108+
#[cfg(test)]
109+
mod tests {
110+
use super::*;
111+
112+
#[test]
113+
fn parse_rejects_internal_whitespace() {
114+
// Whitespace inside the numeric token is invalid, even where a bare
115+
// `float::parse_str` on a fragment would tolerate it.
116+
for s in [
117+
"1 +2j", "1 2j", "1 +2 j", "+ 1j", "1.5 j", "(1 +2j)", "2 -3j",
118+
] {
119+
assert_eq!(parse_str(s), None, "{s:?} must not parse");
120+
}
121+
}
122+
123+
#[test]
124+
fn parse_allows_surrounding_and_paren_whitespace() {
125+
for s in [" 1+2j ", " (1+2j) ", "( 1+2j )"] {
126+
assert_eq!(parse_str(s), Some((1.0, 2.0)), "{s:?}");
127+
}
128+
}
129+
130+
#[test]
131+
fn parse_basic() {
132+
assert_eq!(parse_str("1"), Some((1.0, 0.0)));
133+
assert_eq!(parse_str("1j"), Some((0.0, 1.0)));
134+
assert_eq!(parse_str("j"), Some((0.0, 1.0)));
135+
assert_eq!(parse_str("-j"), Some((0.0, -1.0)));
136+
assert_eq!(parse_str("1+2j"), Some((1.0, 2.0)));
137+
assert_eq!(parse_str("1e5j"), Some((0.0, 1e5)));
138+
assert_eq!(parse_str("1_000"), Some((1000.0, 0.0)));
139+
assert_eq!(parse_str(""), None);
140+
assert_eq!(parse_str("abc"), None);
141+
}
142+
}

extra_tests/snippets/builtin_complex.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ def __eq__(self, other):
168168
assert_raises(TypeError, lambda: complex("5+2j", 1))
169169
assert_raises(ValueError, lambda: complex("abc"))
170170

171+
# whitespace is allowed around the string and the optional parentheses,
172+
# but not inside the numeric token
173+
assert complex(" 1+2j ") == 1 + 2j
174+
assert complex("(1+2j)") == 1 + 2j
175+
assert complex(" ( 1+2j ) ") == 1 + 2j
176+
assert_raises(ValueError, lambda: complex("1 +2j"))
177+
assert_raises(ValueError, lambda: complex("1+ 2j"))
178+
assert_raises(ValueError, lambda: complex("1 + 2j"))
179+
171180
assert complex("1+10j") == 1 + 10j
172181
assert complex(10) == 10 + 0j
173182
assert complex(10.0) == 10 + 0j

0 commit comments

Comments
 (0)