about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2023-12-16 17:39:58 +0100
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2023-12-16 17:39:58 +0100
commit95dfe5ec9040bcba53a8dd61d3593d182defab71 (patch)
treebb6bed6ba9a0fdc94b8ca75b85bc6d4f55793dc5
parentdb9b932314023318f49b0b5941d09f034a12b31e (diff)
downloadrust-95dfe5ec9040bcba53a8dd61d3593d182defab71.tar.gz
rust-95dfe5ec9040bcba53a8dd61d3593d182defab71.zip
Simplify `split_args` code, add a unit test for it and run it into CI
-rw-r--r--.github/workflows/ci.yml9
-rw-r--r--build_system/src/utils.rs95
2 files changed, 67 insertions, 37 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 8e361bf617b..b04ea1550ba 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 9d785e7f57c..ebfa41c761c 100644
--- a/build_system/src/utils.rs
+++ b/build_system/src/utils.rs
@@ -306,46 +306,44 @@ pub fn split_args(args: &str) -> Result<Vec<String>, 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..]
-                    ));
+            }
+            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();
                 }
-            } 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<P: AsRef<Path>>(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()])
+        );
+    }
+}