]> git.lyx.org Git - lyx.git/blob - src/frontends/tests/biblio.cpp
fc4e211b58ea61d4adf19c0e3c946484ae809364
[lyx.git] / src / frontends / tests / biblio.cpp
1 #include <config.h>
2
3 #include <iostream>
4 #include <map>
5
6 #include <boost/regex.hpp>
7
8
9 using std::cout;
10 using std::endl;
11 using std::string;
12
13 // Escape special chars.
14 // All characters are literals except: '.|*?+(){}[]^$\'
15 // These characters are literals when preceded by a "\", which is done here
16 // This function is unfortunately copied from ../frontend_helpers.cpp, so we should
17 // try to make sure to keep the two in sync.
18 string const escape_special_chars(string const & expr)
19 {
20         // Search for all chars '.|*?+(){}[^$]\'
21         // Note that '[' and '\' must be escaped.
22         // This is a limitation of boost::regex, but all other chars in BREs
23         // are assumed literal.
24         boost::regex reg("[].|*?+(){}^$\\[\\\\]");
25
26         // $& is a perl-like expression that expands to all of the current match
27         // The '$' must be prefixed with the escape character '\' for
28         // boost to treat it as a literal.
29         // Thus, to prefix a matched expression with '\', we use:
30         return boost::regex_replace(expr, reg, "\\\\$&");
31 }
32
33
34 typedef std::map<string, string> InfoMap;
35
36 // A functor for use with std::find_if, used to ascertain whether a
37 // data entry matches the required regex_
38 // This class is unfortunately copied from ../frontend_helpers.cpp, so we should
39 // try to make sure to keep the two in sync.
40 class RegexMatch : public std::unary_function<string, bool>
41 {
42 public:
43         // re and icase are used to construct an instance of boost::RegEx.
44         // if icase is true, then matching is insensitive to case
45         RegexMatch(InfoMap const & m, string const & re, bool icase)
46                 : map_(m), regex_(re, icase) {}
47
48         bool operator()(string const & key) const {
49                 // the data searched is the key + its associated BibTeX/biblio
50                 // fields
51                 string data = key;
52                 InfoMap::const_iterator info = map_.find(key);
53                 if (info != map_.end())
54                         data += ' ' + info->second;
55
56                 // Attempts to find a match for the current RE
57                 // somewhere in data.
58                 return boost::regex_search(data, regex_);
59         }
60 private:
61         InfoMap const map_;
62         mutable boost::regex regex_;
63 };
64
65
66 void test_escape_special_chars()
67 {
68         cout << escape_special_chars("abcd") << endl;
69         cout << escape_special_chars("ab&cd") << endl;
70         cout << escape_special_chars(".|*?+(){}[]^$\"") << endl;
71         cout << escape_special_chars("..||**??++(()){{}}[[]]^^$$\\\\") << endl;
72 }
73
74
75 void test_RegexMatch()
76 {
77         InfoMap im;
78         im["hello"] = "hei";
79
80         try {
81                 RegexMatch rm(im, "h.*o", false);
82
83                 cout << rm("hello") << endl;
84                 cout << rm("hei") << endl;
85         }
86         catch (boost::regex_error & regerr) {
87                 cout << regerr.what() << endl;
88         }
89 }
90
91
92 int main()
93 {
94         test_escape_special_chars();
95         test_RegexMatch();
96 }