From 00b9cbe09e7dd0ef68156f541c9dd462c4f4a3bb Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Mon, 18 Dec 2023 22:11:04 +0100 Subject: [PATCH] expr: coerce to string before comparing values --- src/uu/expr/src/syntax_tree.rs | 4 +++- tests/by-util/test_expr.rs | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/uu/expr/src/syntax_tree.rs b/src/uu/expr/src/syntax_tree.rs index 28e4ff0bd..5aa9c9398 100644 --- a/src/uu/expr/src/syntax_tree.rs +++ b/src/uu/expr/src/syntax_tree.rs @@ -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, @@ -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), diff --git a/tests/by-util/test_expr.rs b/tests/by-util/test_expr.rs index ebc2c832f..7c9290d6a 100644 --- a/tests/by-util/test_expr.rs +++ b/tests/by-util/test_expr.rs @@ -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"); +}