mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 21:06:59 -04:00
Added regex_escape script function (was already used by keyword code);
Added icons for 'sort special rarity' choice. git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@625 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -157,55 +157,6 @@ size_t Keyword::findMode(const vector<KeywordModeP>& modes) const {
|
||||
|
||||
// ----------------------------------------------------------------------------- : Regex stuff
|
||||
|
||||
/// Make sure the given regex does no capturing
|
||||
/** Basicly replaces "(" with "(?:" */
|
||||
String make_non_capturing(const String& re) {
|
||||
String ret;
|
||||
bool escape = false, bracket = false, capture = false;
|
||||
FOR_EACH_CONST(c, re) {
|
||||
if (capture && c != _('?')) {
|
||||
// change this capture into a non-capturing "(" by appending "?:"
|
||||
ret += _("?:");
|
||||
capture = false;
|
||||
}
|
||||
if (escape) { // second char of escape sequence
|
||||
escape = false;
|
||||
} else if (c == _('\\')) { // start of escape sequence
|
||||
escape = true;
|
||||
} else if (c == _('[')) { // start of [...]
|
||||
bracket = true;
|
||||
} else if (c == _(']')) { // end of [...]
|
||||
bracket = false;
|
||||
} else if (bracket && c == _('(')) {
|
||||
// wx has a bug, it counts the '(' in "[(]" as a matching group
|
||||
// escape it so wx doesn't see it
|
||||
ret += _('\\');
|
||||
} else if (c == _('(')) { // start of capture?
|
||||
capture = true;
|
||||
}
|
||||
ret += c;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// Escape a single character for use in regular expressions
|
||||
String regex_escape(Char c) {
|
||||
if (c == _('(') || c == _(')') || c == _('[') || c == _(']') || c == _('{') ||
|
||||
c == _('.') || c == _('^') || c == _('$') || c == _('#') || c == _('\\') ||
|
||||
c == _('|') || c == _('+') || c == _('*') || c == _('?')) {
|
||||
// c needs to be escaped
|
||||
return _("\\") + String(1,c);
|
||||
} else {
|
||||
return String(1,c);
|
||||
}
|
||||
}
|
||||
/// Escape a string for use in regular expressions
|
||||
String regex_escape(const String& s) {
|
||||
String ret;
|
||||
FOR_EACH_CONST(c,s) ret += regex_escape(c);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Keyword::prepare(const vector<KeywordParamP>& param_types, bool force) {
|
||||
if (!force && match_re.IsValid()) return;
|
||||
parameters.clear();
|
||||
|
||||
@@ -114,6 +114,11 @@ SCRIPT_FUNCTION(curly_quotes) {
|
||||
SCRIPT_RETURN(input);
|
||||
}
|
||||
|
||||
// regex escape a string
|
||||
SCRIPT_FUNCTION(regex_escape) {
|
||||
SCRIPT_PARAM(String, input);
|
||||
SCRIPT_RETURN(regex_escape(input));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Tagged string
|
||||
|
||||
@@ -593,6 +598,7 @@ void init_script_basic_functions(Context& ctx) {
|
||||
ctx.setVariable(_("format"), script_format);
|
||||
ctx.setVariable(_("format rule"), script_format_rule);
|
||||
ctx.setVariable(_("curly quotes"), script_curly_quotes);
|
||||
ctx.setVariable(_("regex_escape"), script_regex_escape);
|
||||
// tagged string
|
||||
ctx.setVariable(_("tag contents"), script_tag_contents);
|
||||
ctx.setVariable(_("remove tag"), script_tag_remove);
|
||||
@@ -608,7 +614,7 @@ void init_script_basic_functions(Context& ctx) {
|
||||
ctx.setVariable(_("expand keywords"), script_expand_keywords);
|
||||
ctx.setVariable(_("expand keywords rule"), script_expand_keywords_rule);
|
||||
ctx.setVariable(_("keyword usage"), script_keyword_usage);
|
||||
// advanced string rules
|
||||
// advanced string rules/functions
|
||||
ctx.setVariable(_("replace"), script_replace);
|
||||
ctx.setVariable(_("filter text"), script_filter_text);
|
||||
ctx.setVariable(_("match"), script_match);
|
||||
|
||||
+50
-1
@@ -384,4 +384,53 @@ String clean_filename(const String& name) {
|
||||
clean = _("no-name") + clean;
|
||||
}
|
||||
return clean;
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Regular expressions
|
||||
|
||||
/// Escape a single character for use in regular expressions
|
||||
String regex_escape(Char c) {
|
||||
if (c == _('(') || c == _(')') || c == _('[') || c == _(']') || c == _('{') ||
|
||||
c == _('.') || c == _('^') || c == _('$') || c == _('#') || c == _('\\') ||
|
||||
c == _('|') || c == _('+') || c == _('*') || c == _('?')) {
|
||||
// c needs to be escaped
|
||||
return _("\\") + String(1,c);
|
||||
} else {
|
||||
return String(1,c);
|
||||
}
|
||||
}
|
||||
/// Escape a string for use in regular expressions
|
||||
String regex_escape(const String& s) {
|
||||
String ret;
|
||||
FOR_EACH_CONST(c,s) ret += regex_escape(c);
|
||||
return ret;
|
||||
}
|
||||
|
||||
String make_non_capturing(const String& re) {
|
||||
String ret;
|
||||
bool escape = false, bracket = false, capture = false;
|
||||
FOR_EACH_CONST(c, re) {
|
||||
if (capture && c != _('?')) {
|
||||
// change this capture into a non-capturing "(" by appending "?:"
|
||||
ret += _("?:");
|
||||
capture = false;
|
||||
}
|
||||
if (escape) { // second char of escape sequence
|
||||
escape = false;
|
||||
} else if (c == _('\\')) { // start of escape sequence
|
||||
escape = true;
|
||||
} else if (c == _('[')) { // start of [...]
|
||||
bracket = true;
|
||||
} else if (c == _(']')) { // end of [...]
|
||||
bracket = false;
|
||||
} else if (bracket && c == _('(')) {
|
||||
// wx has a bug, it counts the '(' in "[(]" as a matching group
|
||||
// escape it so wx doesn't see it
|
||||
ret += _('\\');
|
||||
} else if (c == _('(')) { // start of capture?
|
||||
capture = true;
|
||||
}
|
||||
ret += c;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -179,5 +179,16 @@ bool cannocial_name_compare(const String& a, const Char* b);
|
||||
/// Make sure a string is safe to use as a filename
|
||||
String clean_filename(const String& name);
|
||||
|
||||
// ----------------------------------------------------------------------------- : Regular expressions
|
||||
|
||||
/// Escape a single character for use in regular expressions
|
||||
String regex_escape(Char c);
|
||||
/// Escape a string for use in regular expressions
|
||||
String regex_escape(const String& s);
|
||||
|
||||
/// Make sure the given regex does no capturing
|
||||
/** Basicly replaces "(" with "(?:" */
|
||||
String make_non_capturing(const String& re);
|
||||
|
||||
// ----------------------------------------------------------------------------- : EOF
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user