]> git.lyx.org Git - lyx.git/blob - src/frontends/tests/biblio.cpp
Honor pending spaces in InsetMathChar::write
[lyx.git] / src / frontends / tests / biblio.cpp
1 #include <config.h>
2
3 #include <iostream>
4 #include <map>
5 #include <regex>
6
7 using namespace std;
8
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)
15 {
16         // Search for all chars '.|*?+(){}[^$]\'
17         // Note that '[', ']', and '\' must be escaped.
18         regex reg("[.|*?+(){}^$\\[\\]\\\\]");
19
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, "\\$&");
25 }
26
27
28 typedef map<string, string> InfoMap;
29
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.
34 class RegexMatch
35 {
36 public:
37         // re is used to construct an instance of lyx::regex.
38         RegexMatch(InfoMap const & m, string const & re)
39                 : map_(m), regex_(re) {}
40
41         bool operator()(string const & key) const {
42                 // the data searched is the key + its associated BibTeX/biblio
43                 // fields
44                 string data = key;
45                 InfoMap::const_iterator info = map_.find(key);
46                 if (info != map_.end())
47                         data += ' ' + info->second;
48
49                 // Attempts to find a match for the current RE
50                 // somewhere in data.
51                 return regex_search(data, regex_);
52         }
53 private:
54         InfoMap const map_;
55         mutable regex regex_;
56 };
57
58
59 void test_escape_special_chars()
60 {
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;
65 }
66
67
68 void test_RegexMatch()
69 {
70         InfoMap im;
71         im["hello"] = "hei";
72
73         try {
74                 RegexMatch rm(im, "h.*o");
75
76                 cout << rm("hello") << endl;
77                 cout << rm("hei") << endl;
78         }
79         catch (regex_error & regerr) {
80                 cout << regerr.what() << endl;
81         }
82 }
83
84
85 int main()
86 {
87         test_escape_special_chars();
88         test_RegexMatch();
89 }