2006-03-06 22:33:16 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
|
|
|
|
2015-01-08 19:46:54 +00:00
|
|
|
#include "support/regex.h"
|
2006-03-06 22:33:16 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2006-03-06 22:33:16 +00:00
|
|
|
|
|
|
|
// Escape special chars.
|
|
|
|
// All characters are literals except: '.|*?+(){}[]^$\'
|
|
|
|
// These characters are literals when preceded by a "\", which is done here
|
2015-01-08 19:46:54 +00:00
|
|
|
// This function is unfortunately copied from ../qt4/GuiCitation.cpp, so we
|
|
|
|
// should try to make sure to keep the two in sync.
|
2006-03-06 22:33:16 +00:00
|
|
|
string const escape_special_chars(string const & expr)
|
|
|
|
{
|
|
|
|
// Search for all chars '.|*?+(){}[^$]\'
|
2015-11-16 22:40:52 +00:00
|
|
|
// Note that '[', ']', and '\' must be escaped.
|
|
|
|
lyx::regex reg("[.|*?+(){}^$\\[\\]\\\\]");
|
2006-03-06 22:33:16 +00:00
|
|
|
|
2015-11-16 22:40:52 +00:00
|
|
|
// $& is a ECMAScript format expression that expands to all
|
2015-01-08 19:46:54 +00:00
|
|
|
// of the current match
|
2015-11-24 19:31:14 +00:00
|
|
|
#if defined(LYX_USE_CXX11) && defined(LYX_USE_STD_REGEX)
|
|
|
|
// To prefix a matched expression with a single literal backslash, we
|
|
|
|
// need to escape it for the C++ compiler and use:
|
|
|
|
return lyx::regex_replace(expr, reg, "\\$&");
|
|
|
|
#else
|
|
|
|
// A backslash in the format string starts an escape sequence in boost.
|
|
|
|
// Thus, to prefix a matched expression with a single literal backslash,
|
|
|
|
// we need to give two backslashes to the regex engine, and escape both
|
|
|
|
// for the C++ compiler and use:
|
2015-01-08 19:46:54 +00:00
|
|
|
return lyx::regex_replace(expr, reg, "\\\\$&");
|
2015-11-24 19:31:14 +00:00
|
|
|
#endif
|
2006-03-06 22:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
typedef map<string, string> InfoMap;
|
2006-03-06 22:33:16 +00:00
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
// A functor for use with find_if, used to ascertain whether a
|
2006-03-06 22:33:16 +00:00
|
|
|
// data entry matches the required regex_
|
2007-04-25 17:15:56 +00:00
|
|
|
// This class is unfortunately copied from ../frontend_helpers.cpp, so we should
|
2006-03-06 22:33:16 +00:00
|
|
|
// try to make sure to keep the two in sync.
|
2007-12-12 19:28:07 +00:00
|
|
|
class RegexMatch : public unary_function<string, bool>
|
2006-03-06 22:33:16 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-01-08 19:46:54 +00:00
|
|
|
// re is used to construct an instance of lyx::regex.
|
|
|
|
RegexMatch(InfoMap const & m, string const & re)
|
|
|
|
: map_(m), regex_(re) {}
|
2006-03-06 22:33:16 +00:00
|
|
|
|
|
|
|
bool operator()(string const & key) const {
|
|
|
|
// the data searched is the key + its associated BibTeX/biblio
|
|
|
|
// fields
|
|
|
|
string data = key;
|
|
|
|
InfoMap::const_iterator info = map_.find(key);
|
|
|
|
if (info != map_.end())
|
|
|
|
data += ' ' + info->second;
|
|
|
|
|
|
|
|
// Attempts to find a match for the current RE
|
|
|
|
// somewhere in data.
|
2015-01-08 19:46:54 +00:00
|
|
|
return lyx::regex_search(data, regex_);
|
2006-03-06 22:33:16 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
InfoMap const map_;
|
2015-01-08 19:46:54 +00:00
|
|
|
mutable lyx::regex regex_;
|
2006-03-06 22:33:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void test_escape_special_chars()
|
|
|
|
{
|
|
|
|
cout << escape_special_chars("abcd") << endl;
|
|
|
|
cout << escape_special_chars("ab&cd") << endl;
|
|
|
|
cout << escape_special_chars(".|*?+(){}[]^$\"") << endl;
|
|
|
|
cout << escape_special_chars("..||**??++(()){{}}[[]]^^$$\\\\") << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void test_RegexMatch()
|
|
|
|
{
|
|
|
|
InfoMap im;
|
|
|
|
im["hello"] = "hei";
|
|
|
|
|
|
|
|
try {
|
2015-01-08 19:46:54 +00:00
|
|
|
RegexMatch rm(im, "h.*o");
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2006-03-06 22:33:16 +00:00
|
|
|
cout << rm("hello") << endl;
|
|
|
|
cout << rm("hei") << endl;
|
|
|
|
}
|
2015-01-08 19:46:54 +00:00
|
|
|
catch (lyx::regex_error & regerr) {
|
2006-03-06 22:33:16 +00:00
|
|
|
cout << regerr.what() << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
test_escape_special_chars();
|
|
|
|
test_RegexMatch();
|
|
|
|
}
|