diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2019-10-18 15:20:49 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2019-10-18 15:20:49 +0200 |
commit | 0e0842330d1ef11f9ac6fa70d9f84bdb16084c45 (patch) | |
tree | ade516cf85cfa50b88b1b6105fc6364204275183 /libbuild2/functions-builtin.cxx | |
parent | 0d6f835ffb582296d24a8d1dd479e2703e075ee3 (diff) |
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)
Diffstat (limited to 'libbuild2/functions-builtin.cxx')
-rw-r--r-- | libbuild2/functions-builtin.cxx | 25 |
1 files changed, 25 insertions, 0 deletions
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 <sstream> + #include <libbuild2/function.hxx> #include <libbuild2/variable.hxx> +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<value> escape) + { + if (v->null) + return string (); + + untypify (*v); // Reverse to names. + + ostringstream os; + to_stream (os, + v->as<names> (), + true /* quote */, + '@' /* pair */, + escape && convert<bool> (move (*escape))); + return os.str (); + }; + // getenv // f["getenv"] = [](string name) |