From 95dfe5ec9040bcba53a8dd61d3593d182defab71 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 16 Dec 2023 17:39:58 +0100 Subject: [PATCH] Simplify `split_args` code, add a unit test for it and run it into CI --- .github/workflows/ci.yml | 9 ++++ build_system/src/utils.rs | 97 ++++++++++++++++++++++++--------------- 2 files changed, 68 insertions(+), 38 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8e361bf617b1..b04ea1550ba2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -131,3 +131,12 @@ jobs: steps: - uses: actions/checkout@v3 - run: python tools/check_intrinsics_duplicates.py + + build_system: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Test build system + run: | + cd build_system + cargo test diff --git a/build_system/src/utils.rs b/build_system/src/utils.rs index 9d785e7f57cf..ebfa41c761ce 100644 --- a/build_system/src/utils.rs +++ b/build_system/src/utils.rs @@ -306,46 +306,44 @@ pub fn split_args(args: &str) -> Result, String> { let args = args.trim(); let mut iter = args.char_indices().peekable(); - while iter.peek().is_some() { - while let Some((pos, c)) = iter.next() { - if c == ' ' { - out.push(args[start..pos].to_string()); - let mut found_start = false; - while let Some((pos, c)) = iter.peek() { - if *c != ' ' { - start = *pos; - found_start = true; - break; - } else { - iter.next(); - } + while let Some((pos, c)) = iter.next() { + if c == ' ' { + out.push(args[start..pos].to_string()); + let mut found_start = false; + while let Some((pos, c)) = iter.peek() { + if *c != ' ' { + start = *pos; + found_start = true; + break; + } else { + iter.next(); } - if !found_start { - return Ok(out); - } - } else if c == '"' || c == '\'' { - let end = c; - let mut found_end = false; - while let Some((_, c)) = iter.next() { - if c == end { - found_end = true; - break; - } else if c == '\\' { - // We skip the escaped character. - iter.next(); - } - } - if !found_end { - return Err(format!( - "Didn't find `{}` at the end of `{}`", - end, - &args[start..] - )); - } - } else if c == '\\' { - // We skip the escaped character. - iter.next(); } + if !found_start { + return Ok(out); + } + } else if c == '"' || c == '\'' { + let end = c; + let mut found_end = false; + while let Some((_, c)) = iter.next() { + if c == end { + found_end = true; + break; + } else if c == '\\' { + // We skip the escaped character. + iter.next(); + } + } + if !found_end { + return Err(format!( + "Didn't find `{}` at the end of `{}`", + end, + &args[start..] + )); + } + } else if c == '\\' { + // We skip the escaped character. + iter.next(); } } let s = args[start..].trim(); @@ -364,3 +362,26 @@ pub fn remove_file>(file_path: &P) -> Result<(), String> { ) }) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_split_args() { + // Missing `"` at the end. + assert!(split_args("\"tada").is_err()); + // Missing `'` at the end. + assert!(split_args("\'tada").is_err()); + + assert_eq!( + split_args("a \"b\" c"), + Ok(vec!["a".to_string(), "\"b\"".to_string(), "c".to_string()]) + ); + // Trailing whitespace characters. + assert_eq!( + split_args(" a \"b\" c "), + Ok(vec!["a".to_string(), "\"b\"".to_string(), "c".to_string()]) + ); + } +}