mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
expr: Rework branches and add tests for check_posix_regex_errors
This commit is contained in:
parent
cfb539d672
commit
6701c6ef44
1 changed files with 26 additions and 13 deletions
|
@ -193,7 +193,7 @@ fn check_posix_regex_errors(pattern: &str) -> ExprResult<()> {
|
||||||
let mut escaped_braces: u64 = 0;
|
let mut escaped_braces: u64 = 0;
|
||||||
let mut escaped = false;
|
let mut escaped = false;
|
||||||
|
|
||||||
let mut repeating_pattern_text = String::with_capacity(13);
|
let mut repeating_pattern_text = String::new();
|
||||||
let mut invalid_content_error = false;
|
let mut invalid_content_error = false;
|
||||||
|
|
||||||
for c in pattern.chars() {
|
for c in pattern.chars() {
|
||||||
|
@ -212,22 +212,27 @@ fn check_posix_regex_errors(pattern: &str) -> ExprResult<()> {
|
||||||
.ok_or(ExprError::UnmatchedClosingBrace)?;
|
.ok_or(ExprError::UnmatchedClosingBrace)?;
|
||||||
let mut repetition = repeating_pattern_text[..repeating_pattern_text.len() - 1]
|
let mut repetition = repeating_pattern_text[..repeating_pattern_text.len() - 1]
|
||||||
.splitn(2, |x| x == ',');
|
.splitn(2, |x| x == ',');
|
||||||
match (repetition.next(), repetition.next()) {
|
match (
|
||||||
(None, None) => {
|
repetition
|
||||||
|
.next()
|
||||||
|
.expect("splitn always returns at least one string"),
|
||||||
|
repetition.next(),
|
||||||
|
) {
|
||||||
|
("", None) => {
|
||||||
// Empty repeating pattern
|
// Empty repeating pattern
|
||||||
invalid_content_error = true;
|
invalid_content_error = true;
|
||||||
}
|
}
|
||||||
(Some(x), None) | (Some(x), Some("")) => {
|
(x, None) | (x, Some("")) => {
|
||||||
if x.parse::<i16>().is_err() {
|
if x.parse::<i16>().is_err() {
|
||||||
invalid_content_error = true;
|
invalid_content_error = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(None, Some(x)) | (Some(""), Some(x)) => {
|
("", Some(x)) => {
|
||||||
if x.parse::<i16>().is_err() {
|
if x.parse::<i16>().is_err() {
|
||||||
invalid_content_error = true;
|
invalid_content_error = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(Some(f), Some(l)) => {
|
(f, Some(l)) => {
|
||||||
if let (Ok(f), Ok(l)) = (f.parse::<i16>(), l.parse::<i16>()) {
|
if let (Ok(f), Ok(l)) = (f.parse::<i16>(), l.parse::<i16>()) {
|
||||||
invalid_content_error = invalid_content_error || f > l;
|
invalid_content_error = invalid_content_error || f > l;
|
||||||
} else {
|
} else {
|
||||||
|
@ -736,17 +741,17 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_regex_valid() {
|
fn check_regex_valid() {
|
||||||
assert!(check_posix_regex_errors(r"(a+b) \(a* b\)").is_ok());
|
assert!(check_posix_regex_errors(r"(a+b) \(a* b\)").is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_regex_simple_repeating_pattern() {
|
fn check_regex_simple_repeating_pattern() {
|
||||||
assert!(check_posix_regex_errors(r"(a+b){4}").is_ok());
|
assert!(check_posix_regex_errors(r"\(a+b\)\{4\}").is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_regex_missing_closing() {
|
fn check_regex_missing_closing() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
check_posix_regex_errors(r"\(abc"),
|
check_posix_regex_errors(r"\(abc"),
|
||||||
Err(ExprError::UnmatchedOpeningParenthesis)
|
Err(ExprError::UnmatchedOpeningParenthesis)
|
||||||
|
@ -759,7 +764,7 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_regex_missing_opening() {
|
fn check_regex_missing_opening() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
check_posix_regex_errors(r"abc\)"),
|
check_posix_regex_errors(r"abc\)"),
|
||||||
Err(ExprError::UnmatchedClosingParenthesis)
|
Err(ExprError::UnmatchedClosingParenthesis)
|
||||||
|
@ -772,7 +777,7 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_regex_empty_repeating_pattern() {
|
fn check_regex_empty_repeating_pattern() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
check_posix_regex_errors("ab\\{\\}"),
|
check_posix_regex_errors("ab\\{\\}"),
|
||||||
Err(InvalidContent(r"\{\}".to_string()))
|
Err(InvalidContent(r"\{\}".to_string()))
|
||||||
|
@ -780,7 +785,7 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_regex_intervals_two_numbers() {
|
fn check_regex_intervals_two_numbers() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
// out of order
|
// out of order
|
||||||
check_posix_regex_errors("ab\\{1,0\\}"),
|
check_posix_regex_errors("ab\\{1,0\\}"),
|
||||||
|
@ -798,5 +803,13 @@ mod test {
|
||||||
check_posix_regex_errors("ab\\{a,b\\}"),
|
check_posix_regex_errors("ab\\{a,b\\}"),
|
||||||
Err(InvalidContent(r"\{\}".to_string()))
|
Err(InvalidContent(r"\{\}".to_string()))
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
check_posix_regex_errors("ab\\{a,\\}"),
|
||||||
|
Err(InvalidContent(r"\{\}".to_string()))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
check_posix_regex_errors("ab\\{,b\\}"),
|
||||||
|
Err(InvalidContent(r"\{\}".to_string()))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue