add join function (#204)

* add join function

* fix formatting to match single include

* add join test

* add join to documentation

* fix MSVC warning: signed/unsigned mismatch

Co-authored-by: Wim Leflere <wleflere@cochlear.com>
This commit is contained in:
Wim Leflere
2021-06-17 20:54:22 +02:00
committed by GitHub
parent 9cf7db8a06
commit 91c93bfb77
5 changed files with 49 additions and 0 deletions

View File

@@ -65,6 +65,7 @@ public:
Sort,
Upper,
Super,
Join,
Callback,
ParenLeft,
ParenRight,
@@ -109,6 +110,7 @@ private:
{std::make_pair("upper", 1), FunctionData { Operation::Upper }},
{std::make_pair("super", 0), FunctionData { Operation::Super }},
{std::make_pair("super", 1), FunctionData { Operation::Super }},
{std::make_pair("join", 2), FunctionData { Operation::Join }},
};
public:

View File

@@ -528,6 +528,24 @@ class Renderer : public NodeVisitor {
json_tmp_stack.push_back(result_ptr);
json_eval_stack.push(result_ptr.get());
} break;
case Op::Join: {
const auto args = get_arguments<2>(node);
const auto separator = args[1]->get<std::string>();
std::ostringstream os;
std::string sep;
for (const auto& value : *args[0]) {
os << sep;
if (value.is_string()) {
os << value.get<std::string>(); // otherwise the value is surrounded with ""
} else {
os << value;
}
sep = separator;
}
result_ptr = std::make_shared<json>(os.str());
json_tmp_stack.push_back(result_ptr);
json_eval_stack.push(result_ptr.get());
} break;
case Op::ParenLeft:
case Op::ParenRight:
case Op::None: