]> git.lyx.org Git - lyx.git/blob - src/support/regex.h
04f93cac5a05ab881733a2dba156d71fd6fbd7a7
[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 __cplusplus >= 201103L && defined(LYX_USE_STD_REGEX)
16 #  include <regex>
17 #  ifdef _MSC_VER
18 namespace lyx {
19   // inheriting 'private' to see which functions are used and if there are
20   // other ECMAScrip defaults
21   class regex : private std::regex
22   {
23   public:
24     regex() {}
25     regex(const regex& rhs) : std::regex(rhs) {}
26     template<class T>
27     regex(T t) : std::regex(t, std::regex_constants::grep) {}
28     template<class T>
29     void assign(T t) { std::regex::assign(t, std::regex_constants::grep); }
30     template<class T, class V>
31     void assign(T t, V v) { std::regex::assign(t, v); }
32     const std::regex& toStd() const { return *this; }
33   };
34   template<class T>
35   bool regex_match(T t, const regex& r) { return std::regex_match(t, r.toStd()); }
36   template<class T, class V>
37   bool regex_match(T t, V v, const regex& r) { return std::regex_match(t, v, r.toStd()); }
38   template<class T, class V, class U, class H>
39   bool regex_match(T t, V v, H h, const regex& r, U u) { return std::regex_match(t, v, h, r.toStd(), u); }
40   template<class T, class V>
41   std::string regex_replace(T t, const regex& r, V v) { return std::regex_replace(t, r.toStd(), v); }
42   //template<class T, class V, class U, class H>
43   //std::string regex_replace(T t, V v, U u, const regex& r, H h) { return std::regex_replace(t, v, u, r.toStd(), h); }
44   template<class T>
45   bool regex_search(T t, const regex& r) { return std::regex_search(t, r.toStd()); }
46   template<class T, class V>
47   bool regex_search(T t, V v, const regex& r) { return std::regex_search(t, v, r.toStd()); }
48   template<class T, class V, class U>
49   bool regex_search(T t, V v, U u, const regex& r) { return std::regex_search(t, v, u, r.toStd()); }
50
51   struct sregex_iterator : std::sregex_iterator
52   {
53     sregex_iterator() {}
54     template<class T, class V>
55     sregex_iterator(T t, V v, const regex& r) : std::sregex_iterator(t, v, r.toStd()) {}
56   };
57 }
58 #  else
59 // <regex> in gcc is unusable in versions less than 4.9.0
60 // see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53631
61 #  define LR_NS std
62 namespace lyx {
63 using LR_NS::regex;
64 using LR_NS::regex_match;
65 using LR_NS::sregex_iterator;
66 }
67 #  endif
68 #else
69 #  include <boost/regex.hpp>
70 #  define LR_NS boost
71 namespace lyx {
72 using LR_NS::regex;
73 using LR_NS::regex_match;
74 using LR_NS::sregex_iterator;
75 }
76 #endif
77
78 namespace lyx {
79 using LR_NS::smatch;
80 using LR_NS::regex_replace;
81 using LR_NS::basic_regex;
82 using LR_NS::regex_error;
83 using LR_NS::regex_search;
84 using LR_NS::match_results;
85
86 namespace regex_constants
87 {
88 using namespace LR_NS::regex_constants;
89 using LR_NS::regex_constants::match_flag_type;
90 }
91
92 }
93
94 #undef LR_NS
95
96 #endif