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