Improve external quoting logic (#3579)

* Add tests and improve quoting logic

* fmt

* Fix clippy ling

* Fix clippy ling
This commit is contained in:
JT
2021-06-09 08:59:53 +12:00
committed by GitHub
parent e9de4c08b0
commit a021b99614
5 changed files with 56 additions and 4 deletions

View File

@ -38,6 +38,21 @@ pub(crate) fn run_external_command(
run_with_stdin(command, context, input, external_redirection)
}
#[allow(unused)]
fn trim_double_quotes(input: &str) -> String {
let mut chars = input.chars();
match (chars.next(), chars.next_back()) {
(Some('"'), Some('"')) => chars.collect(),
_ => input.to_string(),
}
}
#[allow(unused)]
fn escape_where_needed(input: &str) -> String {
input.split(' ').join("\\ ").split('\'').join("\\'")
}
fn run_with_stdin(
command: ExternalCommand,
context: &mut EvaluationContext,
@ -81,6 +96,7 @@ fn run_with_stdin(
}
_ => {
let trimmed_value_string = value.as_string()?.trim_end_matches('\n').to_string();
//let trimmed_value_string = trim_quotes(&trimmed_value_string);
command_args.push((trimmed_value_string, is_literal));
}
}
@ -108,7 +124,12 @@ fn run_with_stdin(
let escaped = escape_double_quotes(&arg);
add_double_quotes(&escaped)
} else {
arg.as_ref().to_string()
let trimmed = trim_double_quotes(&arg);
if trimmed != arg {
escape_where_needed(&trimmed)
} else {
trimmed
}
}
}
#[cfg(windows)]

View File

@ -25,6 +25,15 @@ pub fn cococo() {
}
}
pub fn meow() {
let args: Vec<String> = args();
for arg in args.iter().skip(1) {
let contents = std::fs::read_to_string(arg).expect("Expected a filepath");
println!("{}", contents);
}
}
pub fn nonu() {
args().iter().skip(1).for_each(|arg| print!("{}", arg));
}