From 0e0842330d1ef11f9ac6fa70d9f84bdb16084c45 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 18 Oct 2019 15:20:49 +0200 Subject: Add $quote() function for quoting values This can be useful if we want to pass a value on the command line, for example, in a testscript: $* config.cxx=$quote($recall($cxx.path) $cxx.mode, true) --- libbuild2/functions-builtin.cxx | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'libbuild2/functions-builtin.cxx') diff --git a/libbuild2/functions-builtin.cxx b/libbuild2/functions-builtin.cxx index 2acd5b4..6f1b704 100644 --- a/libbuild2/functions-builtin.cxx +++ b/libbuild2/functions-builtin.cxx @@ -2,9 +2,13 @@ // copyright : Copyright (c) 2014-2019 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file +#include + #include #include +using namespace std; + namespace build2 { // Return NULL value if an environment variable is not set, untyped value @@ -41,6 +45,27 @@ namespace build2 f["string"] = [](uint64_t i) {return to_string (i);}; f["string"] = [](name n) {return to_string (n);}; + // Quote a value returning its string representation. If escape is true, + // then also escape (with a backslash) the quote characters being added + // (this is useful if the result will be re-parsed, for example as a + // Testscript command line). + // + f["quote"] = [](value* v, optional escape) + { + if (v->null) + return string (); + + untypify (*v); // Reverse to names. + + ostringstream os; + to_stream (os, + v->as (), + true /* quote */, + '@' /* pair */, + escape && convert (move (*escape))); + return os.str (); + }; + // getenv // f["getenv"] = [](string name) -- cgit v1.1