]> git.lyx.org Git - lyx.git/blob - src/support/regex.h
eb3b679a8053711877c9029c4e2937fdbf10f89a
[lyx.git] / src / support / regex.h
1 // -*- C++ -*-
2 /**
3  * \file regexp.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Peter Kümmel
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef LYX_REGEXP_H
13 #define LYX_REGEXP_H
14
15 #if defined(LYX_USE_TR1) && defined(LYX_USE_TR1_REGEX)
16 #  ifdef _MSC_VER
17 #    include <regex>
18 #    define match_partial _Match_partial
19 namespace lyx {
20   // inheriting 'private' to see which functions are used and if there are
21   // other ECMAScrip defaults
22   class regex : private std::tr1::regex
23   {
24   public:
25     regex() {}
26     regex(const regex& rhs) : std::tr1::regex(rhs) {}
27     template<class T>
28     regex(T t) : std::tr1::regex(t, std::tr1::regex_constants::grep) {}
29     template<class T>
30     void assign(T t) { std::tr1::regex::assign(t, std::tr1::regex_constants::grep); }
31     template<class T, class V>
32     void assign(T t, V v) { std::tr1::regex::assign(t, v); }
33     const std::tr1::regex& toTr1() const { return *this; }
34   };
35   template<class T>
36   bool regex_match(T t, const regex& r) { return std::tr1::regex_match(t, r.toTr1()); }
37   template<class T, class V>
38   bool regex_match(T t, V v, const regex& r) { return std::tr1::regex_match(t, v, r.toTr1()); }
39   template<class T, class V, class U, class H>
40   bool regex_match(T t, V v, H h, const regex& r, U u) { return std::tr1::regex_match(t, v, h, r.toTr1(), u); }
41   template<class T, class V>
42   std::string regex_replace(T t, const regex& r, V v) { return std::tr1::regex_replace(t, r.toTr1(), v); }
43   //template<class T, class V, class U, class H>
44   //std::string regex_replace(T t, V v, U u, const regex& r, H h) { return std::tr1::regex_replace(t, v, u, r.toTr1(), h); }
45   template<class T>
46   bool regex_search(T t, const regex& r) { return std::tr1::regex_search(t, r.toTr1()); }
47   template<class T, class V>
48   bool regex_search(T t, V v, const regex& r) { return std::tr1::regex_search(t, v, r.toTr1()); }
49   template<class T, class V, class U>
50   bool regex_search(T t, V v, U u, const regex& r) { return std::tr1::regex_search(t, v, u, r.toTr1()); }
51
52   struct sregex_iterator : std::tr1::sregex_iterator
53   {
54     sregex_iterator() {}
55     template<class T, class V>
56     sregex_iterator(T t, V v, const regex& r) : std::tr1::sregex_iterator(t, v, r.toTr1()) {}
57   };
58 }
59 #  else
60 #    include <tr1/regex>
61 //   TODO no match_partial in TR1, how to replace?
62 #  endif
63 #  define LR_NS std::tr1
64 namespace lyx {
65 using LR_NS::regex;
66 using LR_NS::regex_match;
67 using LR_NS::sregex_iterator;
68 }
69 #elif LYX_USE_TR1_REGEX
70 #  include <regex>
71 // <regex> in gcc is unusable in versions less than 4.9.0
72 // see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53631
73 // TODO no match_partial in std, how to replace?
74 #  define LR_NS std
75 namespace lyx {
76 using LR_NS::regex;
77 using LR_NS::regex_match;
78 using LR_NS::sregex_iterator;
79 }
80 #else 
81 #  include <boost/regex.hpp>
82 #  define LR_NS boost
83 namespace lyx {
84 using LR_NS::regex;
85 using LR_NS::regex_match;
86 using LR_NS::sregex_iterator;
87 }
88 #endif
89
90 namespace lyx {
91 using LR_NS::smatch;
92 using LR_NS::regex_replace;
93 using LR_NS::basic_regex;
94 using LR_NS::regex_error;
95 using LR_NS::regex_search;
96 using LR_NS::match_results;
97
98 namespace regex_constants
99 {
100 using namespace LR_NS::regex_constants;
101 using LR_NS::regex_constants::match_flag_type;
102 }
103
104 }
105
106 #undef LR_NS
107
108 #endif