1 /***************************************************************************
2 * Copyright (C) 2008-2017 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>
28 # include <unicode/errorcode.h>
29 # include <unicode/translit.h>
31 # include <boost/regex.hpp>
32 #endif // BOOST_REGEX_ICU
37 #include "utility/functional.h"
41 #ifdef BOOST_REGEX_ICU
43 struct StripDiacritics
45 static void convert(icu::UnicodeString
&s
)
47 if (m_converter
== nullptr)
49 icu::ErrorCode result
;
50 m_converter
= icu::Transliterator::createInstance(
51 "NFD; [:M:] Remove; NFC", UTRANS_FORWARD
, result
);
52 if (result
.isFailure())
53 throw std::runtime_error(
54 "instantiation of transliterator instance failed with "
55 + std::string(result
.errorName()));
57 m_converter
->transliterate(s
);
61 static icu::Transliterator
*m_converter
;
64 icu::Transliterator
*StripDiacritics::m_converter
;
66 #endif // BOOST_REGEX_ICU
73 #ifdef BOOST_REGEX_ICU
77 #endif // BOOST_REGEX_ICU
80 template <typename StringT
>
81 inline Regex
make(StringT
&&s
,
82 boost::regex_constants::syntax_option_type flags
)
85 #ifdef BOOST_REGEX_ICU
89 #endif // BOOST_REGEX_ICU
90 (std::forward
<StringT
>(s
), flags
);
93 template <typename CharT
>
94 inline bool search(const std::basic_string
<CharT
> &s
,
96 bool ignore_diacritics
)
99 #ifdef BOOST_REGEX_ICU
100 if (ignore_diacritics
)
102 auto us
= icu::UnicodeString::fromUTF8(
103 icu::StringPiece(convertString
<char, CharT
>::apply(s
)));
104 StripDiacritics::convert(us
);
105 return boost::u32regex_search(us
, rx
);
108 return boost::u32regex_search(s
, rx
);
110 return boost::regex_search(s
, rx
);
111 #endif // BOOST_REGEX_ICU
112 } catch (std::out_of_range
&e
) {
113 // Invalid UTF-8 sequence, ignore the string.
114 std::cerr
<< "Regex::search: error while processing \""
123 template <typename T
>
126 typedef NC::Menu
<T
> MenuT
;
127 typedef typename
NC::Menu
<T
>::Item Item
;
128 typedef std::function
<bool(const Regex
&, const T
&)> FilterFunction
;
132 template <typename FilterT
>
133 Filter(const std::string
&constraint_
,
134 boost::regex_constants::syntax_option_type flags
,
136 : m_rx(make(constraint_
, flags
))
137 , m_constraint(constraint_
)
138 , m_filter(std::forward
<FilterT
>(filter
))
146 const std::string
&constraint() const {
150 bool operator()(const Item
&item
) const {
152 return m_filter(m_rx
, item
.value());
157 return m_filter
.operator bool();
162 std::string m_constraint
;
163 FilterFunction m_filter
;
166 template <typename T
> struct ItemFilter
168 typedef NC::Menu
<T
> MenuT
;
169 typedef typename
NC::Menu
<T
>::Item Item
;
170 typedef std::function
<bool(const Regex
&, const Item
&)> FilterFunction
;
174 template <typename FilterT
>
175 ItemFilter(const std::string
&constraint_
,
176 boost::regex_constants::syntax_option_type flags
,
178 : m_rx(make(constraint_
, flags
))
179 , m_constraint(constraint_
)
180 , m_filter(std::forward
<FilterT
>(filter
))
188 const std::string
&constraint() const {
192 bool operator()(const Item
&item
) {
193 return m_filter(m_rx
, item
);
198 return m_filter
.operator bool();
203 std::string m_constraint
;
204 FilterFunction m_filter
;
209 #endif // NCMPCPP_REGEX_FILTER_H