Skip to content

Commit 95dfe5e

Browse files
Simplify split_args code, add a unit test for it and run it into CI
1 parent db9b932 commit 95dfe5e

File tree

2 files changed

+67
-37
lines changed

2 files changed

+67
-37
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,12 @@ jobs:
131131
steps:
132132
- uses: actions/checkout@v3
133133
- run: python tools/check_intrinsics_duplicates.py
134+
135+
build_system:
136+
runs-on: ubuntu-latest
137+
steps:
138+
- uses: actions/checkout@v3
139+
- name: Test build system
140+
run: |
141+
cd build_system
142+
cargo test

build_system/src/utils.rs

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -306,46 +306,44 @@ pub fn split_args(args: &str) -> Result<Vec<String>, String> {
306306
let args = args.trim();
307307
let mut iter = args.char_indices().peekable();
308308

309-
while iter.peek().is_some() {
310-
while let Some((pos, c)) = iter.next() {
311-
if c == ' ' {
312-
out.push(args[start..pos].to_string());
313-
let mut found_start = false;
314-
while let Some((pos, c)) = iter.peek() {
315-
if *c != ' ' {
316-
start = *pos;
317-
found_start = true;
318-
break;
319-
} else {
320-
iter.next();
321-
}
309+
while let Some((pos, c)) = iter.next() {
310+
if c == ' ' {
311+
out.push(args[start..pos].to_string());
312+
let mut found_start = false;
313+
while let Some((pos, c)) = iter.peek() {
314+
if *c != ' ' {
315+
start = *pos;
316+
found_start = true;
317+
break;
318+
} else {
319+
iter.next();
322320
}
323-
if !found_start {
324-
return Ok(out);
325-
}
326-
} else if c == '"' || c == '\'' {
327-
let end = c;
328-
let mut found_end = false;
329-
while let Some((_, c)) = iter.next() {
330-
if c == end {
331-
found_end = true;
332-
break;
333-
} else if c == '\\' {
334-
// We skip the escaped character.
335-
iter.next();
336-
}
337-
}
338-
if !found_end {
339-
return Err(format!(
340-
"Didn't find `{}` at the end of `{}`",
341-
end,
342-
&args[start..]
343-
));
321+
}
322+
if !found_start {
323+
return Ok(out);
324+
}
325+
} else if c == '"' || c == '\'' {
326+
let end = c;
327+
let mut found_end = false;
328+
while let Some((_, c)) = iter.next() {
329+
if c == end {
330+
found_end = true;
331+
break;
332+
} else if c == '\\' {
333+
// We skip the escaped character.
334+
iter.next();
344335
}
345-
} else if c == '\\' {
346-
// We skip the escaped character.
347-
iter.next();
348336
}
337+
if !found_end {
338+
return Err(format!(
339+
"Didn't find `{}` at the end of `{}`",
340+
end,
341+
&args[start..]
342+
));
343+
}
344+
} else if c == '\\' {
345+
// We skip the escaped character.
346+
iter.next();
349347
}
350348
}
351349
let s = args[start..].trim();
@@ -364,3 +362,26 @@ pub fn remove_file<P: AsRef<Path>>(file_path: &P) -> Result<(), String> {
364362
)
365363
})
366364
}
365+
366+
#[cfg(test)]
367+
mod tests {
368+
use super::*;
369+
370+
#[test]
371+
fn test_split_args() {
372+
// Missing `"` at the end.
373+
assert!(split_args("\"tada").is_err());
374+
// Missing `'` at the end.
375+
assert!(split_args("\'tada").is_err());
376+
377+
assert_eq!(
378+
split_args("a \"b\" c"),
379+
Ok(vec!["a".to_string(), "\"b\"".to_string(), "c".to_string()])
380+
);
381+
// Trailing whitespace characters.
382+
assert_eq!(
383+
split_args(" a \"b\" c "),
384+
Ok(vec!["a".to_string(), "\"b\"".to_string(), "c".to_string()])
385+
);
386+
}
387+
}

0 commit comments

Comments
 (0)