2014-04-13 12:21:30 +02:00
|
|
|
#include "concordia/regex_replacement.hpp"
|
|
|
|
#include <sstream>
|
|
|
|
#include <boost/exception/all.hpp>
|
|
|
|
#include <boost/throw_exception.hpp>
|
|
|
|
|
2014-04-24 08:36:48 +02:00
|
|
|
RegexReplacement::RegexReplacement(string patternString, string replacement,
|
|
|
|
bool caseSensitive)
|
2014-04-13 12:21:30 +02:00
|
|
|
throw(ConcordiaException):
|
|
|
|
_replacement(replacement) {
|
|
|
|
try {
|
2014-04-24 08:36:48 +02:00
|
|
|
if (caseSensitive) {
|
|
|
|
_pattern = boost::make_u32regex(patternString);
|
|
|
|
} else {
|
2014-04-29 14:46:04 +02:00
|
|
|
_pattern = boost::make_u32regex(patternString,
|
|
|
|
boost::regex::icase);
|
2014-04-24 08:36:48 +02:00
|
|
|
}
|
2014-04-29 14:46:04 +02:00
|
|
|
} catch(const std::exception & e) {
|
2014-04-13 12:21:30 +02:00
|
|
|
stringstream ss;
|
2014-04-29 14:46:04 +02:00
|
|
|
|
2014-04-13 12:21:30 +02:00
|
|
|
ss << "Bad regex pattern: " << patternString <<
|
|
|
|
" Detailed info: " << e.what();
|
2014-04-29 14:46:04 +02:00
|
|
|
|
|
|
|
if (std::string const * extra =
|
|
|
|
boost::get_error_info<my_tag_error_info>(e) ) {
|
2014-04-13 12:21:30 +02:00
|
|
|
ss << *extra;
|
|
|
|
}
|
|
|
|
throw ConcordiaException(ss.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RegexReplacement::~RegexReplacement() {
|
|
|
|
}
|
|
|
|
|
|
|
|
string RegexReplacement::apply(const string & text) {
|
2014-04-29 14:46:04 +02:00
|
|
|
try {
|
|
|
|
return boost::u32regex_replace(text, _pattern, _replacement,
|
|
|
|
boost::match_default | boost::format_all);
|
|
|
|
} catch(...) {
|
|
|
|
throw ConcordiaException("Exception while applying replacement rule: "
|
|
|
|
+_replacement+" to text: "+text);
|
|
|
|
}
|
2014-04-13 12:21:30 +02:00
|
|
|
}
|
|
|
|
|