From 6aeae43f3ccfa8111cb2fa63e4145207db2a5e1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teemu=20P=C3=A4tsi?= Date: Tue, 27 May 2025 13:08:22 +0300 Subject: [PATCH] expr: Simplify verifying indexes within regex range quantifier --- src/uu/expr/src/syntax_tree.rs | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/uu/expr/src/syntax_tree.rs b/src/uu/expr/src/syntax_tree.rs index 4f5a4c241..c1abfb9a1 100644 --- a/src/uu/expr/src/syntax_tree.rs +++ b/src/uu/expr/src/syntax_tree.rs @@ -302,27 +302,19 @@ where } // Check if parsed quantifier is valid + let is_valid_range_index = |s: &str| s.parse::().map_or(true, |x| x <= i16::MAX as i32); let re = Regex::new(r"^(\d*,\d*|\d+)").expect("valid regular expression"); - match re.captures(&quantifier) { - None => false, - Some(captures) => { - let matched = captures.at(0).unwrap_or_default(); - let mut repetition = matched.splitn(2, ','); - match ( - repetition - .next() - .expect("splitn always returns at least one string"), - repetition.next(), - ) { - ("", Some("")) => true, - (x, None | Some("")) => x.parse::().map_or(true, |x| x <= i16::MAX as i32), - ("", Some(x)) => x.parse::().map_or(true, |x| x <= i16::MAX as i32), - (f, Some(l)) => match (f.parse::(), l.parse::()) { - (Ok(f), Ok(l)) => f <= l && f <= i16::MAX as i32 && l <= i16::MAX as i32, - _ => false, - }, - } + if let Some(captures) = re.captures(&quantifier) { + let matched = captures.at(0).unwrap_or_default(); + match matched.split_once(',') { + None => is_valid_range_index(matched), + Some(("", "")) => true, + Some((x, "")) => is_valid_range_index(x), + Some(("", x)) => is_valid_range_index(x), + Some((f, l)) => f <= l && is_valid_range_index(f) && is_valid_range_index(l), } + } else { + false } }