In my function for parsing the input, in my current function below, there is no extra space that will be added if we’ve finished taking in the characters with single input, but we see a whitespace on the failing test.
pub fn parse_input(input: &str) -> Vec<String> {
let mut args: Vec<String> = Vec::new();
let mut extracted_quoted_strings = HashSet::new();
let mut current_arg_buffer = String::new();
let mut in_single_quote = false;
let mut chars = input.chars().peekable();
while let Some(ch) = chars.next() {
if ch == SINGLE_QUOTE {
if in_single_quote {
args.push(current_arg_buffer.clone());
extracted_quoted_strings.insert(current_arg_buffer.clone());
current_arg_buffer.clear();
in_single_quote = false;
} else {
if !current_arg_buffer.is_empty() {
args.push(current_arg_buffer.clone());
current_arg_buffer.clear();
}
in_single_quote = true;
}
} else if ch.is_whitespace() && !in_single_quote {
if !current_arg_buffer.is_empty() {
args.push(current_arg_buffer.clone());
current_arg_buffer.clear();
}
} else {
current_arg_buffer.push(ch);
}
}
if !current_arg_buffer.is_empty() {
if in_single_quote {
args.push(current_arg_buffer.clone());
extracted_quoted_strings.insert(current_arg_buffer.clone());
} else {
args.push(current_arg_buffer.clone());
}
}
println!("{:?}", extracted_quoted_strings);
args
}
The test is telling you what is wrong. You are parsing 'hello''example' as two arguments.
In Bash “arguments” are referred to as tokens. They are separated by “metacharacters”. When you find the ending single quote you think you are done, so you add the text between ' as its own element to your argument list. This is wrong because we haven’t reached a metacharacter yet (like an unquoted space). Same thing with double quotes .
Thank you for the explanation, I’ve gotten a good understanding and tried to modify my solution but when testing with echo for the Backslash outside quotes on #YT5 I get error that they were not correct. Yet, on running echo with each of the cat “/tmp/foo/f\n86” “/tmp/foo/f\79” “/tmp/foo/f’'55” they output correctly.
This would help you pass the stage you’re stuck at while ensuring the previous stages aren’t disturbed. Let me know if something is unclear in the same!