Add a substring function to the formula language.

Also added a unit test for the function.
This commit is contained in:
Mark de Wever 2010-12-26 09:47:09 +00:00
parent c2b37db47b
commit 243f9fd87d
6 changed files with 148 additions and 0 deletions

View File

@ -3,6 +3,8 @@ Version 1.9.3+svn:
* Delfadors Memoirs:
* Fixed bug #17273: Made difficulty selection conform to the style of all
other mainline campaigns
* Formula language:
* Added substring function.
* Graphics:
* Terrain: added transitions for the wood floor.
* Language and i18n:

View File

@ -794,6 +794,7 @@ if(ENABLE_TESTS)
tests/utils/play_scenario.cpp
tests/test_config_cache.cpp
tests/test_formula_ai.cpp
tests/test_formula_function.cpp
tests/test_lexical_cast.cpp
tests/test_network_worker.cpp
tests/test_team.cpp

View File

@ -450,6 +450,7 @@ test_SOURCES = \
tests/utils/play_scenario.cpp \
tests/test_config_cache.cpp \
tests/test_formula_ai.cpp \
tests/formula_function.cpp \
tests/test_lexical_cast.cpp \
tests/test_network_worker.cpp \
tests/test_team.cpp \

View File

@ -516,6 +516,7 @@ libtest_utils = test_env.Library("test_utils", test_utils_sources)
test_sources = Split("""
tests/main.cpp
tests/test_formula_ai.cpp
tests/test_formula_function.cpp
tests/test_lexical_cast.cpp
tests/test_network_worker.cpp
tests/test_team.cpp

View File

@ -29,6 +29,8 @@ static lg::log_domain log_engine("engine");
#define DBG_NG LOG_STREAM(debug, log_engine)
static lg::log_domain log_scripting_formula("scripting/formula");
#define LOG_SF LOG_STREAM(info, log_scripting_formula)
#define WRN_SF LOG_STREAM(warn, log_scripting_formula)
#define ERR_SF LOG_STREAM(err, log_scripting_formula)
namespace game_logic {
@ -369,6 +371,55 @@ private:
}
};
class substring_function
: public function_expression {
public:
explicit substring_function(const args_list& args)
: function_expression("substring", args, 2, 3)
{}
variant execute(const formula_callable& variables
, formula_debugger *fdb) const {
std::string result = args()[0]->evaluate(variables, fdb).as_string();
int offset = args()[1]->evaluate(variables, fdb).as_int();
if(offset < 0) {
offset += result.size();
if(offset < 0) {
WRN_SF << "[concatenate] Offset '"
<< args()[1]->evaluate(variables, fdb).as_int()
<< "' results in a negative start in string '"
<< result
<< "' and is reset at the beginning of the string.\n";
offset = 0;
}
} else {
if(static_cast<size_t>(offset) >= result.size()) {
WRN_SF << "[concatenate] Offset '" << offset
<< "' is larger as the size of '" << result
<< "' and results in an empty string.\n";
return variant(std::string());
}
}
if(args().size() > 2) {
const int size = args()[2]->evaluate(variables, fdb).as_int();
if(size < 0) {
ERR_SF << "[concatenate] Size is negative an "
<< "empty string is returned.\n";
return variant(std::string());
}
return variant(result.substr(offset, size));
} else {
return variant(result.substr(offset));
}
}
};
class index_of_function : public function_expression {
public:
explicit index_of_function(const args_list& args)
@ -1050,6 +1101,7 @@ functions_map& get_functions_map() {
FUNCTION(values);
FUNCTION(tolist);
FUNCTION(tomap);
FUNCTION(substring);
#undef FUNCTION
}

View File

@ -0,0 +1,91 @@
/* $Id$ */
/*
Copyright (C) 2010 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-test"
#include <boost/test/unit_test.hpp>
#include "formula_function.hpp"
BOOST_AUTO_TEST_SUITE(formula_function)
BOOST_AUTO_TEST_CASE(test_formula_function_substring)
{
BOOST_CHECK_EQUAL(
game_logic::formula("substring('hello world', 0)")
.evaluate().as_string()
, "hello world");
BOOST_CHECK_EQUAL(
game_logic::formula("substring('hello world', 6)")
.evaluate().as_string()
, "world");
BOOST_CHECK_EQUAL(
game_logic::formula("substring('hello world', 11)")
.evaluate().as_string()
, "");
BOOST_CHECK_EQUAL(
game_logic::formula("substring('hello world', -1)")
.evaluate().as_string()
, "d");
BOOST_CHECK_EQUAL(
game_logic::formula("substring('hello world', -5)")
.evaluate().as_string()
, "world");
BOOST_CHECK_EQUAL(
game_logic::formula("substring('hello world', -11)")
.evaluate().as_string()
, "hello world");
BOOST_CHECK_EQUAL(
game_logic::formula("substring('hello world', -12)")
.evaluate().as_string()
, "hello world");
BOOST_CHECK_EQUAL(
game_logic::formula("substring('hello world', 0, 0)")
.evaluate().as_string()
, "");
BOOST_CHECK_EQUAL(
game_logic::formula("substring('hello world', 0, -1)")
.evaluate().as_string()
, "");
BOOST_CHECK_EQUAL(
game_logic::formula("substring('hello world', 5, 1)")
.evaluate().as_string()
, " ");
BOOST_CHECK_EQUAL(
game_logic::formula("substring('hello world', 1, 9)")
.evaluate().as_string()
, "ello worl");
BOOST_CHECK_EQUAL(
game_logic::formula("substring('hello world', -10, 9)")
.evaluate().as_string()
, "ello worl");
}
BOOST_AUTO_TEST_SUITE_END()