9 // Escape special chars.
10 // All characters are literals except: '.|*?+(){}[]^$\'
11 // These characters are literals when preceded by a "\", which is done here
12 // This function is unfortunately copied from ../qt/GuiCitation.cpp, so we
13 // should try to make sure to keep the two in sync.
14 string const escape_special_chars(string const & expr)
16 // Search for all chars '.|*?+(){}[^$]\'
17 // Note that '[', ']', and '\' must be escaped.
18 regex reg("[.|*?+(){}^$\\[\\]\\\\]");
20 // $& is a ECMAScript format expression that expands to all
21 // of the current match
22 // To prefix a matched expression with a single literal backslash, we
23 // need to escape it for the C++ compiler and use:
24 return regex_replace(expr, reg, "\\$&");
28 typedef map<string, string> InfoMap;
30 // A functor for use with find_if, used to ascertain whether a
31 // data entry matches the required regex_
32 // This class is unfortunately copied from ../frontend_helpers.cpp, so we should
33 // try to make sure to keep the two in sync.
37 // re is used to construct an instance of lyx::regex.
38 RegexMatch(InfoMap const & m, string const & re)
39 : map_(m), regex_(re) {}
41 bool operator()(string const & key) const {
42 // the data searched is the key + its associated BibTeX/biblio
45 InfoMap::const_iterator info = map_.find(key);
46 if (info != map_.end())
47 data += ' ' + info->second;
49 // Attempts to find a match for the current RE
51 return regex_search(data, regex_);
59 void test_escape_special_chars()
61 cout << escape_special_chars("abcd") << endl;
62 cout << escape_special_chars("ab&cd") << endl;
63 cout << escape_special_chars(".|*?+(){}[]^$\"") << endl;
64 cout << escape_special_chars("..||**??++(()){{}}[[]]^^$$\\\\") << endl;
68 void test_RegexMatch()
74 RegexMatch rm(im, "h.*o");
76 cout << rm("hello") << endl;
77 cout << rm("hei") << endl;
79 catch (regex_error & regerr) {
80 cout << regerr.what() << endl;
87 test_escape_special_chars();