From 2af3b94d584f08cb037510070a9bd6c1f88cd8a9 Mon Sep 17 00:00:00 2001 From: Janik Rabe Date: Sun, 17 Feb 2019 13:31:55 +0100 Subject: [PATCH] Do not assume /\A["']/ strings are quoted Do not assume that strings starting with a quote are quoted. For instance, the string `"a b` contains a space that needs to be escaped, so quoteIfNeeded should return `"\"a b"` rather than `"a b`. --- src/util.cpp | 3 --- test/util.t.cpp | 1 + 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index 38027855..92c61bb0 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -54,9 +54,6 @@ std::string escape (const std::string& input, int c) //////////////////////////////////////////////////////////////////////////////// std::string quoteIfNeeded (const std::string& input) { - if (input[0] == '"' || input[0] == '\'') - return input; - auto quote = input.find ('"'); auto space = input.find (' '); auto op = input.find_first_of ("+-/()<^!=~_"); diff --git a/test/util.t.cpp b/test/util.t.cpp index 83e4a33c..549506d9 100644 --- a/test/util.t.cpp +++ b/test/util.t.cpp @@ -43,6 +43,7 @@ int main (int, char**) // std::string quoteIfNeeded (const std::string& input) t.is (quoteIfNeeded ("foo"), "foo", "quoteIfNeeded 'foo' --> 'foo'"); t.is (quoteIfNeeded ("f o o"), "\"f o o\"", "quoteIfNeeded 'f o o' --> '\"f o o\"'"); + t.is (quoteIfNeeded ("\"a b\" c"), "\"\\\"a b\\\" c\"", "quoteIfNeeded '\"a b\" c' --> '\"\\\"a b\\\" c'\""); { std::set unjoined;