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

expr: Simplify verifying indexes within regex range quantifier

This commit is contained in:
Teemu Pätsi 2025-05-27 13:08:22 +03:00
parent b1a91351bc
commit 6aeae43f3c
No known key found for this signature in database

View file

@ -302,27 +302,19 @@ where
}
// Check if parsed quantifier is valid
let is_valid_range_index = |s: &str| s.parse::<i32>().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) => {
if let Some(captures) = re.captures(&quantifier) {
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::<i32>().map_or(true, |x| x <= i16::MAX as i32),
("", Some(x)) => x.parse::<i32>().map_or(true, |x| x <= i16::MAX as i32),
(f, Some(l)) => match (f.parse::<i32>(), l.parse::<i32>()) {
(Ok(f), Ok(l)) => f <= l && f <= i16::MAX as i32 && l <= i16::MAX as i32,
_ => false,
},
}
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
}
}