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

expr: interpret numbers != 0 as true for | and &

This commit is contained in:
Daniel Hofstetter 2023-09-26 16:12:59 +02:00
parent ffbe452b01
commit ff500d7d6f
2 changed files with 20 additions and 3 deletions

View file

@ -8,10 +8,10 @@
//! * `<https://en.wikipedia.org/wiki/Shunting-yard_algorithm>` //! * `<https://en.wikipedia.org/wiki/Shunting-yard_algorithm>`
//! //!
// spell-checker:ignore (ToDO) binop binops ints paren prec multibytes // spell-checker:ignore (ToDO) ints paren prec multibytes
use num_bigint::BigInt; use num_bigint::BigInt;
use num_traits::{One, Zero}; use num_traits::Zero;
use onig::{Regex, RegexOptions, Syntax}; use onig::{Regex, RegexOptions, Syntax};
use crate::tokens::Token; use crate::tokens::Token;
@ -515,7 +515,7 @@ fn value_as_bool(s: &str) -> bool {
return false; return false;
} }
match s.parse::<BigInt>() { match s.parse::<BigInt>() {
Ok(n) => n.is_one(), Ok(n) => n != Zero::zero(),
Err(_) => true, Err(_) => true,
} }
} }

View file

@ -113,6 +113,16 @@ fn test_or() {
.args(&["foo", "|", "bar"]) .args(&["foo", "|", "bar"])
.succeeds() .succeeds()
.stdout_only("foo\n"); .stdout_only("foo\n");
new_ucmd!()
.args(&["14", "|", "1"])
.succeeds()
.stdout_only("14\n");
new_ucmd!()
.args(&["-14", "|", "1"])
.succeeds()
.stdout_only("-14\n");
} }
#[test] #[test]
@ -123,6 +133,13 @@ fn test_and() {
.stdout_only("foo\n"); .stdout_only("foo\n");
new_ucmd!().args(&["", "&", "1"]).run().stdout_is("0\n"); new_ucmd!().args(&["", "&", "1"]).run().stdout_is("0\n");
new_ucmd!().args(&["14", "&", "1"]).run().stdout_is("14\n");
new_ucmd!()
.args(&["-14", "&", "1"])
.run()
.stdout_is("-14\n");
} }
#[test] #[test]