Compare commits

...

2 Commits

Author SHA1 Message Date
Yannick Reiß 2a05453f8e Rule words to generate words from a command
Run the tests for a working verification of xtcl. / build (ubuntu-20.04) (push) Successful in 45s Details
2025-09-03 06:06:33 +02:00
Yannick Reiß b4cbc81aaa Add additional testcase containing quotes 2025-09-03 04:16:58 +02:00
2 changed files with 42 additions and 1 deletions

View File

@ -38,5 +38,32 @@ pub fn commands(tcl: &str) -> Vec<String> {
// @brief Split command into words.
// @param command: &str
pub fn words(command: &str) -> Vec<String> {
vec![]
let mut words: Vec<String> = vec![];
let mut new_word: String = String::new();
let mut quote: bool = false;
for character in command.chars() {
if quote {
new_word.push(character);
if character == '"' {
quote = false;
}
} else {
match character {
' ' => {
words.push(new_word.clone());
new_word = String::new();
}
'"' => {
new_word.push(character);
quote = true;
}
_ => new_word.push(character),
}
}
}
words.push(new_word.clone());
return words;
}

View File

@ -52,3 +52,17 @@ fn test_words_simple() {
let case: Vec<String> = words(testcase);
assert_eq!(case, verify);
}
#[test]
fn test_words_quoted() {
let testcase: &str = "routine \"argument with spaces\" 1234 0x543";
let verify: Vec<String> = vec![
String::from("routine"),
String::from("\"argument with spaces\""),
String::from("1234"),
String::from("0x543"),
];
let case: Vec<String> = words(testcase);
assert_eq!(case, verify);
}