1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

expr: short-circuit evaluation for &

This commit is contained in:
Zhuoxun Yang 2023-10-14 01:56:38 +08:00
parent 5f4305df6f
commit ae1c4ccfd2

View file

@ -166,16 +166,26 @@ impl AstNode {
{ {
let mut out = Vec::with_capacity(operands.len()); let mut out = Vec::with_capacity(operands.len());
let mut operands = operands.iter(); let mut operands = operands.iter();
// check the first value before `|`, stop evaluate and return directly if it is true.
// push dummy to pass the check of `len() == 2` if let Some(value) = operands.next() {
if op_type == "|" { let value = value.evaluate()?;
if let Some(value) = operands.next() { out.push(value.clone());
let value = value.evaluate()?; // short-circuit evaluation for `|` and `&`
out.push(value.clone()); // push dummy to pass `assert!(values.len() == 2);`
if value_as_bool(&value) { match op_type.as_ref() {
out.push(String::from("dummy")); "|" => {
return Ok(out); if value_as_bool(&value) {
out.push(String::from("dummy"));
return Ok(out);
}
} }
"&" => {
if !value_as_bool(&value) {
out.push(String::from("dummy"));
return Ok(out);
}
}
_ => {}
} }
} }