Compare commits

..

4 Commits

Author SHA1 Message Date
nichkara
20330f746f Implement rule to remove comments
All checks were successful
Run the tests for a working verification of xtcl. / build (ubuntu-20.04) (push) Successful in 41s
2025-09-03 09:43:52 +02:00
nichkara
47bf17cb0b Add tests for comment removal 2025-09-03 09:36:54 +02:00
2a05453f8e Rule words to generate words from a command
All checks were successful
Run the tests for a working verification of xtcl. / build (ubuntu-20.04) (push) Successful in 45s
2025-09-03 06:06:33 +02:00
b4cbc81aaa Add additional testcase containing quotes 2025-09-03 04:16:58 +02:00
2 changed files with 79 additions and 2 deletions

View File

@@ -38,5 +38,55 @@ pub fn commands(tcl: &str) -> Vec<String> {
// @brief Split command into words. // @brief Split command into words.
// @param command: &str // @param command: &str
pub fn words(command: &str) -> Vec<String> { 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;
}
// @name comments
// @return String
// @brief Remove all comments.
// @param tcl_script: &str
pub fn comments(tcl_script: &str) -> String {
let mut toggle_comment: bool = false;
let mut cleaned_tcl_script: String = String::new();
for character in tcl_script.chars() {
if toggle_comment {
if character == '\n' {
toggle_comment = false;
}
} else {
cleaned_tcl_script.push(character);
if character == ';' || character == '\n' {
toggle_comment = true;
}
}
}
return cleaned_tcl_script;
} }

View File

@@ -1,5 +1,5 @@
#[cfg(test)] #[cfg(test)]
use crate::tcl::{commands, words}; use crate::tcl::{commands, comments, words};
#[test] #[test]
fn test_commands_working() { fn test_commands_working() {
@@ -52,3 +52,30 @@ fn test_words_simple() {
let case: Vec<String> = words(testcase); let case: Vec<String> = words(testcase);
assert_eq!(case, verify); 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);
}
#[test]
fn test_remove_comments() {
let testcase_1: &str = "test#noreplacement";
let testcase_2: &str = ";#replace_this\n";
let verify_1: &str = "test#noreplacement";
let verify_2: &str = ";";
let case_1: String = comments(testcase_1);
let case_2: String = comments(testcase_2);
assert_eq!(verify_1, case_1.as_str());
assert_eq!(verify_2, case_2.as_str());
}