Add concatenate function.

This commit is contained in:
Mark de Wever 2010-12-26 14:10:22 +00:00
parent 04112eb00b
commit 1203d74e32
3 changed files with 46 additions and 0 deletions

View File

@ -6,6 +6,7 @@ Version 1.9.3+svn:
* Formula language:
* Added substring function.
* Added length function, to determine the length of a string.
* Added concatenate function.
* Graphics:
* Terrain: added transitions for the wood floor.
* Language and i18n:

View File

@ -435,6 +435,27 @@ public:
}
};
class concatenate_function
: public function_expression {
public:
explicit concatenate_function(const args_list& args)
: function_expression("concatenate", args, 1, -1)
{}
private:
variant execute(const formula_callable& variables
, formula_debugger *fdb) const {
std::string result;
foreach(expression_ptr arg, args()) {
result += arg->evaluate(variables, fdb).string_cast();
}
return variant(result);
}
};
class index_of_function : public function_expression {
public:
explicit index_of_function(const args_list& args)
@ -1118,6 +1139,7 @@ functions_map& get_functions_map() {
FUNCTION(tomap);
FUNCTION(substring);
FUNCTION(length);
FUNCTION(concatenate);
#undef FUNCTION
}

View File

@ -98,5 +98,28 @@ BOOST_AUTO_TEST_CASE(test_formula_function_length)
, 11);
}
BOOST_AUTO_TEST_CASE(test_formula_function_concatenate)
{
BOOST_CHECK_EQUAL(
game_logic::formula("concatenate(100)").evaluate().as_string()
, "100");
BOOST_CHECK_EQUAL(
game_logic::formula("concatenate(100, 200, 'a')")
.evaluate().as_string()
, "100200a");
BOOST_CHECK_EQUAL(
game_logic::formula("concatenate([1,2,3])")
.evaluate().as_string()
, "1, 2, 3");
BOOST_CHECK_EQUAL(
game_logic::formula(
"concatenate([1.0, 1.00, 1.000, 1.2, 1.23, 1.234])")
.evaluate().as_string()
, "1.000, 1.000, 1.000, 1.200, 1.230, 1.234");
}
BOOST_AUTO_TEST_SUITE_END()