1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #ifndef NCMPCPP_REGEX_FILTER_H
22 #define NCMPCPP_REGEX_FILTER_H
26 #ifdef BOOST_REGEX_ICU
27 # include <boost/regex/icu.hpp>
29 # include <boost/regex.hpp>
30 #endif // BOOST_REGEX_ICU
38 #ifdef BOOST_REGEX_ICU
42 #endif // BOOST_REGEX_ICU
45 template <typename StringT
>
46 inline Regex
make(StringT
&&s
, boost::regex_constants::syntax_option_type flags
)
49 # ifdef BOOST_REGEX_ICU
53 # endif // BOOST_REGEX_ICU
54 (std::forward
<StringT
>(s
), flags
);
57 template <typename StringT
>
58 inline bool search(StringT
&&s
, const Regex
&rx
)
62 # ifdef BOOST_REGEX_ICU
63 boost::u32regex_search
66 # endif // BOOST_REGEX_ICU
67 (std::forward
<StringT
>(s
), rx
);
68 } catch (std::out_of_range
&e
) {
69 // Invalid UTF-8 sequence, ignore the string.
70 std::cerr
<< "Regex::search: error while processing \"" << s
<< "\": " << e
.what() << "\n";
78 typedef NC::Menu
<T
> MenuT
;
79 typedef typename
NC::Menu
<T
>::Item Item
;
80 typedef std::function
<bool(const Regex
&, const T
&)> FilterFunction
;
83 Filter(Regex rx
, FilterFunction filter
)
84 : m_rx(std::move(rx
)), m_filter(std::move(filter
)) { }
91 bool operator()(const Item
&item
) const {
93 return m_filter(m_rx
, item
.value());
98 return m_filter
.operator bool();
103 FilterFunction m_filter
;
106 template <typename T
> struct ItemFilter
108 typedef NC::Menu
<T
> MenuT
;
109 typedef typename
NC::Menu
<T
>::Item Item
;
110 typedef std::function
<bool(const Regex
&, const Item
&)> FilterFunction
;
113 ItemFilter(Regex rx
, FilterFunction filter
)
114 : m_rx(std::move(rx
)), m_filter(std::move(filter
)) { }
121 bool operator()(const Item
&item
) {
122 return m_filter(m_rx
, item
);
127 return m_filter
.operator bool();
132 FilterFunction m_filter
;
137 #endif // NCMPCPP_REGEX_FILTER_H