Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proper underscore handling to float and complex types. #5356

Merged
merged 10 commits into from
Jul 28, 2024
Prev Previous commit
Next Next commit
update complex.rs and float.rs
  • Loading branch information
dannasman committed Jul 23, 2024
commit cffaa569daf379ee6b7ddf110a14f29d40536b63
9 changes: 3 additions & 6 deletions vm/src/builtins/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,16 +546,14 @@ fn parse_str(s: &str) -> Option<Complex64> {

let value = match s.strip_suffix(|c| c == 'j' || c == 'J') {
None => {
let stripped = float::float_strip_underscores(s.as_bytes())?;
Complex64::new(crate::literal::float::parse_bytes(&stripped)?, 0.0)
Complex64::new(crate::literal::float::parse_str(s)?, 0.0)
}
Some(mut s) => {
let mut real = 0.0;
// Find the central +/- operator. If it exists, parse the real part.
for (i, w) in s.as_bytes().windows(2).enumerate() {
if (w[1] == b'+' || w[1] == b'-') && !(w[0] == b'e' || w[0] == b'E') {
let stripped = float::float_strip_underscores(s[..=i].as_bytes())?;
real = crate::literal::float::parse_bytes(&stripped)?;
real = crate::literal::float::parse_str(&s[..=i])?;
s = &s[i + 1..];
break;
}
Expand All @@ -567,8 +565,7 @@ fn parse_str(s: &str) -> Option<Complex64> {
// "-j"
"-" => -1.0,
s => {
let stripped = float::float_strip_underscores(s.as_bytes())?;
crate::literal::float::parse_bytes(&stripped)?
crate::literal::float::parse_str(s)?
}
};
Complex64::new(real, imag)
Expand Down
42 changes: 3 additions & 39 deletions vm/src/builtins/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,33 +159,6 @@ impl Constructor for PyFloat {
}
}

pub fn float_strip_underscores(b: &[u8]) -> Option<Vec<u8>> {
let mut prev = b'\0';
let mut dup = Vec::<u8>::new();
for p in b {
if *p == b'_' {
// Underscores are only allowed after digits.
if !prev.is_ascii_digit() {
return None;
}
} else {
dup.push(*p);
// Underscores are only allowed before digits.
if prev == b'_' && !p.is_ascii_digit() {
return None;
}
}
prev = *p;
}

// Underscores are not allowed at the end.
if prev == b'_' {
return None;
}

Some(dup)
}

fn float_from_string(val: PyObjectRef, vm: &VirtualMachine) -> PyResult<f64> {
let (bytearray, buffer, buffer_lock);
let b = if let Some(s) = val.payload_if_subclass::<PyStr>(vm) {
Expand All @@ -206,18 +179,9 @@ fn float_from_string(val: PyObjectRef, vm: &VirtualMachine) -> PyResult<f64> {
)));
};

let err = val
.repr(vm)
.map(|repr| vm.new_value_error(format!("could not convert string to float: {repr}")))
.unwrap_or_else(|e| e);

if !b.contains(&b'_') {
crate::literal::float::parse_bytes(b).ok_or(err)
} else if let Some(dup) = float_strip_underscores(b) {
crate::literal::float::parse_bytes(&dup).ok_or(err)
} else {
Err(err)
}
crate::literal::float::parse_bytes(b).ok_or(val.repr(vm)
.map(|repr| vm.new_value_error(format!("could not convert string to float: {repr}")))
.unwrap_or_else(|e| e))
}

#[pyclass(
Expand Down
Loading