Made a Regex class that wraps either boost::regex or wxRegEx (split from ScriptRegex).

Use Regex for keywords.

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1196 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2008-09-03 14:43:11 +00:00
parent 9c9a2aec61
commit 7c83ff01f4
7 changed files with 373 additions and 210 deletions
+48
View File
@@ -0,0 +1,48 @@
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2008 Twan van Laarhoven and "coppro" |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <util/regex.hpp>
#include <util/error.hpp>
#if USE_BOOST_REGEX
// ----------------------------------------------------------------------------- : Regex : boost
void Regex::assign(const String& code) {
// compile string
try {
regex.assign(code.begin(),code.end());
} catch (const boost::regex_error& e) {
/// TODO: be more precise
throw ScriptError(String::Format(_("Error while compiling regular expression: '%s'\nAt position: %d\n%s"),
code.c_str(), e.position(), String(e.what(), IF_UNICODE(wxConvUTF8,String::npos))));
}
}
void Regex::replace_all(String* input, const String& format) {
//std::basic_string<Char> fmt; format_string(format,fmt);
std::basic_string<Char> fmt(format.begin(),format.end());
String output;
regex_replace(insert_iterator<String>(output, output.end()),
input->begin(), input->end(), regex, fmt, boost::format_sed);
*input = output;
}
#else // USE_BOOST_REGEX
// ----------------------------------------------------------------------------- : Regex : wx
void Regex::assign(const String& code) {
// compile string
if (!regex.Compile(code, wxRE_ADVANCED)) {
throw ScriptError(_("Error while compiling regular expression: '") + code + _("'"));
}
assert(regex.IsValid());
}
#endif // USE_BOOST_REGEX
// ----------------------------------------------------------------------------- : Regex : common