Fix compilation with ICU >= 61
[ncmpcpp.git] / src / regex_filter.h
blob4a173b559e34f66924827ea1c656a082090b8158
1 /***************************************************************************
2 * Copyright (C) 2008-2017 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
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. *
9 * *
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. *
14 * *
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
24 #include "config.h"
26 #ifdef BOOST_REGEX_ICU
27 # include <boost/regex/icu.hpp>
28 # include <unicode/errorcode.h>
29 # include <unicode/translit.h>
30 #else
31 # include <boost/regex.hpp>
32 #endif // BOOST_REGEX_ICU
34 #include <cassert>
35 #include <iostream>
37 #include "utility/functional.h"
39 namespace {
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);
60 private:
61 static icu::Transliterator *m_converter;
64 icu::Transliterator *StripDiacritics::m_converter;
66 #endif // BOOST_REGEX_ICU
70 namespace Regex {
72 typedef
73 #ifdef BOOST_REGEX_ICU
74 boost::u32regex
75 #else
76 boost::regex
77 #endif // BOOST_REGEX_ICU
78 Regex;
80 template <typename StringT>
81 inline Regex make(StringT &&s,
82 boost::regex_constants::syntax_option_type flags)
84 return
85 #ifdef BOOST_REGEX_ICU
86 boost::make_u32regex
87 #else
88 boost::regex
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,
95 const Regex &rx,
96 bool ignore_diacritics)
98 try {
99 #ifdef BOOST_REGEX_ICU
100 if (ignore_diacritics)
102 auto us = icu::UnicodeString::fromUTF8(
103 StringPiece(convertString<char, CharT>::apply(s)));
104 StripDiacritics::convert(us);
105 return boost::u32regex_search(us, rx);
107 else
108 return boost::u32regex_search(s, rx);
109 #else
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 \""
115 << s
116 << "\": "
117 << e.what()
118 << "\n";
119 return false;
123 template <typename T>
124 struct Filter
126 typedef NC::Menu<T> MenuT;
127 typedef typename NC::Menu<T>::Item Item;
128 typedef std::function<bool(const Regex &, const T &)> FilterFunction;
130 Filter() { }
132 template <typename FilterT>
133 Filter(const std::string &constraint_,
134 boost::regex_constants::syntax_option_type flags,
135 FilterT &&filter)
136 : m_rx(make(constraint_, flags))
137 , m_constraint(constraint_)
138 , m_filter(std::forward<FilterT>(filter))
141 void clear()
143 m_filter = nullptr;
146 const std::string &constraint() const {
147 return m_constraint;
150 bool operator()(const Item &item) const {
151 assert(defined());
152 return m_filter(m_rx, item.value());
155 bool defined() const
157 return m_filter.operator bool();
160 private:
161 Regex m_rx;
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;
172 ItemFilter() { }
174 template <typename FilterT>
175 ItemFilter(const std::string &constraint_,
176 boost::regex_constants::syntax_option_type flags,
177 FilterT &&filter)
178 : m_rx(make(constraint_, flags))
179 , m_constraint(constraint_)
180 , m_filter(std::forward<FilterT>(filter))
183 void clear()
185 m_filter = nullptr;
188 const std::string &constraint() const {
189 return m_constraint;
192 bool operator()(const Item &item) {
193 return m_filter(m_rx, item);
196 bool defined() const
198 return m_filter.operator bool();
201 private:
202 Regex m_rx;
203 std::string m_constraint;
204 FilterFunction m_filter;
209 #endif // NCMPCPP_REGEX_FILTER_H