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