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:
parent
b1a91351bc
commit
6aeae43f3c
1 changed files with 11 additions and 19 deletions
|
@ -302,27 +302,19 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if parsed quantifier is valid
|
// 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");
|
let re = Regex::new(r"^(\d*,\d*|\d+)").expect("valid regular expression");
|
||||||
match re.captures(&quantifier) {
|
if let Some(captures) = re.captures(&quantifier) {
|
||||||
None => false,
|
let matched = captures.at(0).unwrap_or_default();
|
||||||
Some(captures) => {
|
match matched.split_once(',') {
|
||||||
let matched = captures.at(0).unwrap_or_default();
|
None => is_valid_range_index(matched),
|
||||||
let mut repetition = matched.splitn(2, ',');
|
Some(("", "")) => true,
|
||||||
match (
|
Some((x, "")) => is_valid_range_index(x),
|
||||||
repetition
|
Some(("", x)) => is_valid_range_index(x),
|
||||||
.next()
|
Some((f, l)) => f <= l && is_valid_range_index(f) && is_valid_range_index(l),
|
||||||
.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,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue