SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
regex.hpp
Go to the documentation of this file.
1 /*
2 Copyright© 2014 Akihiro Nishimura
3 
4 This software is released under the MIT License.
5 http://opensource.org/licenses/mit-license.php
6 */
7 
8 #ifndef SIG_UTIL_REGEX_HPP
9 #define SIG_UTIL_REGEX_HPP
10 
11 #include "../helper/type_convert.hpp"
12 #include "../helper/maybe.hpp"
13 #include <regex>
14 
15 #if SIG_MSVC_ENV
16 #define NOMINMAX
17 #include <windows.h>
18 #pragma warning ( disable : 4996 )
19 #endif
20 
21 #if !SIG_MSVC_ENV && SIG_ENABLE_BOOST
22 #include <boost/regex.hpp>
23 #endif
24 
25 #if (__GLIBCXX__ || __GLIBCPP__)
26 #define SIG_USE_GLIBCPP 1
27 #endif
28 
29 // 正規表現ライブラリの指定 (gcc標準はバグが多いため避ける)
30 #if (!SIG_GCC_ENV)
31  using SIG_Regex = std::regex;
32  using SIG_WRegex = std::wregex;
33  using SIG_SMatch = std::smatch;
34  using SIG_WSMatch = std::wsmatch;
35  #define SIG_RegexSearch std::regex_search
36  #define SIG_RegexReplace std::regex_replace
37 #elif SIG_ENABLE_BOOST
38  using SIG_Regex = typename boost::regex;
39  using SIG_WRegex = typename boost::wregex;
40  using SIG_SMatch = typename boost::smatch;
41  using SIG_WSMatch = typename boost::wsmatch;
42  #define SIG_RegexSearch boost::regex_search
43  #define SIG_RegexReplace boost::regex_replace
44 #else
45  #define SIG_REGEX_UNABLE 1
46 #endif
47 
48 
50 
51 #if !SIG_REGEX_UNABLE
52 namespace sig
53 {
54 
55 namespace impl
56 {
57 // string type to associated in regex type
58 template <class T>
60 template <>
61 struct Str2RegexSelector<std::string>{
62  typedef SIG_Regex regex;
63  typedef SIG_SMatch smatch;
64 };
65 template <>
66 struct Str2RegexSelector<std::wstring>{
67  typedef SIG_WRegex regex;
69 };
70 }
71 
72 #if SIG_MSVC_ENV
73 
75 
88 inline auto escape_regex(std::string const& expression) ->std::string
89 {
90  static const SIG_Regex escape_reg(R"(([(){}\[\]|^?$.+*\\]))");
91 
92  return SIG_RegexReplace(expression, escape_reg, std::string(R"(\$1)"));
93 }
94 
98 inline auto escape_regex(std::wstring const& expression) ->std::wstring
99 {
100  static const SIG_WRegex escape_reg(LR"(([(){}\[\]|^?$.+*\\]))");
101 
102  return SIG_RegexReplace(expression, escape_reg, std::wstring(LR"(\$1)"));
103 }
104 
105 #elif (SIG_USE_GLIBCPP && SIG_ENABLE_BOOST)
106 
108 
111 inline auto escape_regex(std::string const& expression) ->std::string
112 {
113  static const SIG_Regex escape_reg(R"(([(){}\[\]|^?$.+*\\]))");
114 
115  return SIG_RegexReplace(expression, escape_reg, std::string(R"(\\$1)"));
116 }
117 
121 inline auto escape_regex(std::wstring const& expression) ->std::wstring
122 {
123  static const SIG_WRegex escape_reg(LR"(([(){}\[\]|^?$.+*\\]))");
124 
125  return SIG_RegexReplace(expression, escape_reg, std::wstring(LR"(\\$1)"));
126 }
127 #endif
128 
129 
130 #if SIG_MSVC_ENV || (SIG_USE_GLIBCPP && SIG_ENABLE_BOOST)
131 
133 
147 template <class S>
148 auto make_regex(S const& expression) ->typename impl::Str2RegexSelector<impl::string_t<S>>::regex
149 {
150  return typename impl::Str2RegexSelector<impl::string_t<S>>::regex(escape_regex(expression));
151 }
152 
153 
155 
171 template <class S, class TS = impl::string_t<S>>
173  S&& src,
174  typename impl::Str2RegexSelector<TS>::regex const& expression)
175 ->Maybe<std::vector<std::vector<TS>>>
176 {
177  using R = std::vector<std::vector<TS>>;
178 
180  auto tmp = static_cast<TS>(src);
181  R d;
182 
183  while (SIG_RegexSearch(tmp, match, expression)){
184  d.push_back(std::vector<TS>());
185  for (auto const& m : match) d.back().push_back(m);
186  tmp = match.suffix().str();
187  }
188 
189  return d.empty() ? Nothing(std::move(d)) : Just(std::move(d));
190 }
191 #endif
192 
193 }
194 #endif
195 #endif
std::wregex SIG_WRegex
Definition: regex.hpp:32
auto Nothing() -> typename impl::SameIf< T, void, typename boost::none_t, Maybe< T >>::type
値コンストラクタ
Definition: maybe.hpp:67
std::wsmatch SIG_WSMatch
Definition: regex.hpp:34
Maybe< R > Just(T &&v)
値コンストラクタ
Definition: maybe.hpp:55
auto make_regex(S const &expression) -> typename impl::Str2RegexSelector< impl::string_t< S >>::regex
エスケープ処理を行い、regexオブジェクトを返す
Definition: regex.hpp:148
std::smatch SIG_SMatch
Definition: regex.hpp:33
std::regex SIG_Regex
Definition: regex.hpp:31
#define SIG_RegexReplace
Definition: regex.hpp:36
auto regex_search(S &&src, typename impl::Str2RegexSelector< TS >::regex const &expression) -> Maybe< std::vector< std::vector< TS >>>
std::regex_search のラッパ関数
Definition: regex.hpp:172
auto escape_regex(std::string const &expression) -> std::string
与えられた文字中に含まれる、正規表現の特殊文字をエスケープする
Definition: regex.hpp:88
Definition: array.hpp:15
#define SIG_RegexSearch
Definition: regex.hpp:35