Skip to content

Commit

Permalink
Merge pull request #5673 from tertsdiepraam/expr-fix-comparison
Browse files Browse the repository at this point in the history
`expr`: coerce to string before comparing values
  • Loading branch information
sylvestre committed Dec 18, 2023
2 parents b32c93e + 00b9cbe commit 52af36d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/uu/expr/src/syntax_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ impl RelationOp {
}
} else {
// These comparisons should be using locale settings
let a = a.eval_as_string();
let b = b.eval_as_string();
match self {
Self::Lt => a < b,
Self::Leq => a <= b,
Expand Down Expand Up @@ -195,7 +197,7 @@ const PRECEDENCE: &[&[(&str, BinOp)]] = &[
&[(":", BinOp::String(StringOp::Match))],
];

#[derive(Debug, PartialEq, Eq, Ord, PartialOrd)]
#[derive(Debug)]
pub enum NumOrStr {
Num(BigInt),
Str(String),
Expand Down
8 changes: 8 additions & 0 deletions tests/by-util/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,11 @@ fn test_invalid_syntax() {
.stderr_contains("syntax error");
}
}

#[test]
fn test_num_str_comparison() {
new_ucmd!()
.args(&["1a", "<", "1", "+", "1"])
.succeeds()
.stdout_is("1\n");
}

0 comments on commit 52af36d

Please sign in to comment.