Skip to main content

normalize_expression

Function normalize_expression 

Source
pub fn normalize_expression(left: &str, op: &str, right: &str) -> String
Expand description

CAP-AE-02: Normalize binary expression to canonical form.

For commutative operators, operands are sorted alphabetically to ensure that expressions like “a + b” and “b + a” produce the same normalized text.

§TIGER Mitigations

  • ELEPHANT-PASS1-5: Applies trim() to operands for whitespace normalization
  • TIGER-PASS1-3: Documents that bitwise/logical conflation is safe (see COMMUTATIVE_OPS)

§Arguments

  • left - Left operand (will be trimmed)
  • op - The binary operator
  • right - Right operand (will be trimmed)

§Returns

Normalized expression string in the form “left op right” or “right op left” (for commutative operators, alphabetically sorted).

§Examples

// Commutative operators - operands sorted
assert_eq!(normalize_expression("b", "+", "a"), "a + b");
assert_eq!(normalize_expression("y", "*", "x"), "x * y");
assert_eq!(normalize_expression("foo", "==", "bar"), "bar == foo");

// Non-commutative operators - order preserved
assert_eq!(normalize_expression("a", "-", "b"), "a - b");
assert_eq!(normalize_expression("x", "/", "y"), "x / y");

// Whitespace is trimmed (ELEPHANT-PASS1-5)
assert_eq!(normalize_expression("  a  ", "+", "  b  "), "a + b");