mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 13:06:59 -04:00
Added break_text function
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@857 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -447,7 +447,7 @@ ScriptValueP replace_rule(Context& ctx) {
|
||||
ret->replacement = replace->toString();
|
||||
}
|
||||
// in_context
|
||||
SCRIPT_OPTIONAL_PARAM_N(String, _("in context"), in_context) {
|
||||
SCRIPT_OPTIONAL_PARAM_C(String, in_context) {
|
||||
if (!ret->context.Compile(in_context, wxRE_ADVANCED)) {
|
||||
throw ScriptError(_("Error while compiling regular expression: '")+in_context+_("'"));
|
||||
}
|
||||
@@ -479,7 +479,7 @@ class ScriptFilterRule : public ScriptValue {
|
||||
bool ok = regex.GetMatch(&start, &len, 0);
|
||||
assert(ok);
|
||||
String inside = input.substr(start, len); // the match
|
||||
String next_input = input.substr(start + len); // everything after the match
|
||||
String next_input = input.substr(start + len); // everything after the match
|
||||
if (!context.IsValid() || context.Matches(input.substr(0,start) + _("<match>") + next_input)) {
|
||||
// no context or context match
|
||||
ret += inside;
|
||||
@@ -497,7 +497,7 @@ class ScriptFilterRule : public ScriptValue {
|
||||
ScriptValueP filter_rule(Context& ctx) {
|
||||
// cached?
|
||||
SCRIPT_PARAM_C(String, match);
|
||||
SCRIPT_PARAM_DEFAULT_N(String, _("in context"), in_context, String());
|
||||
SCRIPT_PARAM_DEFAULT_C(String, in_context, String());
|
||||
|
||||
// cache
|
||||
const int CACHE_SIZE = 6;
|
||||
@@ -540,6 +540,59 @@ SCRIPT_FUNCTION(filter_text) {
|
||||
return filter_rule(ctx)->eval(ctx);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Rules : regex filter/break
|
||||
|
||||
class ScriptBreakRule : public ScriptValue {
|
||||
public:
|
||||
virtual ScriptType type() const { return SCRIPT_FUNCTION; }
|
||||
virtual String typeName() const { return _("break_rule"); }
|
||||
virtual ScriptValueP eval(Context& ctx) const {
|
||||
SCRIPT_PARAM_C(String, input);
|
||||
intrusive_ptr<ScriptCustomCollection> ret(new ScriptCustomCollection);
|
||||
while (regex.Matches(input)) {
|
||||
// match, append to result
|
||||
size_t start, len;
|
||||
bool ok = regex.GetMatch(&start, &len, 0);
|
||||
assert(ok);
|
||||
String inside = input.substr(start, len); // the match
|
||||
String next_input = input.substr(start + len); // everything after the match
|
||||
if (!context.IsValid() || context.Matches(input.substr(0,start) + _("<match>") + next_input)) {
|
||||
// no context or context match
|
||||
ret->value.push_back(to_script(inside));
|
||||
}
|
||||
input = next_input;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
wxRegEx regex; ///< Regex to match
|
||||
wxRegEx context; ///< Match only in a given context, optional
|
||||
};
|
||||
|
||||
// Create a regular expression rule for breaking strings
|
||||
ScriptValueP break_rule(Context& ctx) {
|
||||
intrusive_ptr<ScriptBreakRule> ret(new ScriptBreakRule);
|
||||
// match
|
||||
SCRIPT_PARAM_C(String, match);
|
||||
if (!ret->regex.Compile(match, wxRE_ADVANCED)) {
|
||||
throw ScriptError(_("Error while compiling regular expression: '")+match+_("'"));
|
||||
}
|
||||
// in_context
|
||||
SCRIPT_OPTIONAL_PARAM_C(String, in_context) {
|
||||
if (!ret->context.Compile(in_context, wxRE_ADVANCED)) {
|
||||
throw ScriptError(_("Error while compiling regular expression: '")+in_context+_("'"));
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
SCRIPT_FUNCTION(break_rule) {
|
||||
return break_rule(ctx);
|
||||
}
|
||||
SCRIPT_FUNCTION(break_text) {
|
||||
return break_rule(ctx)->eval(ctx);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Rules : regex match
|
||||
|
||||
class ScriptMatchRule : public ScriptValue {
|
||||
@@ -657,10 +710,12 @@ void init_script_basic_functions(Context& ctx) {
|
||||
// advanced string rules/functions
|
||||
ctx.setVariable(_("replace"), script_replace);
|
||||
ctx.setVariable(_("filter text"), script_filter_text);
|
||||
ctx.setVariable(_("break text"), script_break_text);
|
||||
ctx.setVariable(_("match"), script_match);
|
||||
ctx.setVariable(_("sort text"), script_sort_text);
|
||||
ctx.setVariable(_("replace rule"), script_replace_rule);
|
||||
ctx.setVariable(_("filter rule"), script_filter_rule);
|
||||
ctx.setVariable(_("break rule"), script_break_rule);
|
||||
ctx.setVariable(_("match rule"), script_match_rule);
|
||||
ctx.setVariable(_("sort rule"), script_sort_rule);
|
||||
}
|
||||
|
||||
@@ -136,6 +136,8 @@ inline Type from_script(const ScriptValueP& v, Variable var) {
|
||||
#define SCRIPT_PARAM_DEFAULT_N(Type, str, name, def) \
|
||||
ScriptValueP name##_ = ctx.getVariableOpt(str); \
|
||||
Type name = name##_ ? from_script<Type>(name##_, str) : def
|
||||
#define SCRIPT_PARAM_DEFAULT_C(Type, name, def) \
|
||||
SCRIPT_PARAM_DEFAULT_N(Type, SCRIPT_VAR_ ## name, name, name)
|
||||
|
||||
// ----------------------------------------------------------------------------- : Rules
|
||||
|
||||
|
||||
Reference in New Issue
Block a user