mirror of https://github.com/djcb/mu.git
lib/contacts-cache: improve code
This commit is contained in:
parent
4a405ff4d4
commit
545494225a
|
@ -49,12 +49,11 @@ struct ContactsCache::Private {
|
||||||
Private(Config& config_db)
|
Private(Config& config_db)
|
||||||
:config_db_{config_db},
|
:config_db_{config_db},
|
||||||
contacts_{deserialize(config_db_.get<Config::Id::Contacts>())},
|
contacts_{deserialize(config_db_.get<Config::Id::Contacts>())},
|
||||||
personal_plain_{make_plain(config_db_.get<Config::Id::PersonalAddresses>())},
|
personal_plain_{make_matchers<Config::Id::PersonalAddresses>()},
|
||||||
personal_rx_{make_rx(config_db_.get<Config::Id::PersonalAddresses>())},
|
personal_rx_{make_rx_matchers<Config::Id::PersonalAddresses>()},
|
||||||
ignored_plain_{make_plain(config_db_.get<Config::Id::IgnoredAddresses>())},
|
ignored_plain_{make_matchers<Config::Id::IgnoredAddresses>()},
|
||||||
ignored_rx_{make_rx(config_db_.get<Config::Id::IgnoredAddresses>())},
|
ignored_rx_{make_rx_matchers<Config::Id::IgnoredAddresses>()},
|
||||||
dirty_{0}
|
dirty_{0} {}
|
||||||
{}
|
|
||||||
|
|
||||||
~Private() {
|
~Private() {
|
||||||
serialize();
|
serialize();
|
||||||
|
@ -76,43 +75,28 @@ struct ContactsCache::Private {
|
||||||
mutable size_t dirty_;
|
mutable size_t dirty_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
static bool is_rx(const std::string& p) {
|
||||||
* Return the non-regex addresses
|
return p.size() >= 2 && p.at(0) == '/' && p.at(p.length() - 1) == '/';
|
||||||
*
|
|
||||||
* @param personal
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
StringVec make_plain(const StringVec& personal) const {
|
|
||||||
StringVec svec;
|
|
||||||
std::copy_if(personal.begin(), personal.end(),
|
|
||||||
std::back_inserter(svec), [&](auto&& p) {
|
|
||||||
return p.size() < 2 || p.at(0) != '/' ||
|
|
||||||
p.at(p.length() - 1) != '/';
|
|
||||||
});
|
|
||||||
return svec;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
template<Config::Id Id> StringVec make_matchers() const {
|
||||||
* Return regexps for the regex-addresses
|
return seq_remove(config_db_.get<Id>(), is_rx);
|
||||||
*
|
}
|
||||||
* @param personal
|
template<Config::Id Id> std::vector<Regex> make_rx_matchers() const {
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
std::vector<Regex> make_rx(const StringVec& personal) const {
|
|
||||||
std::vector<Regex> rxvec;
|
std::vector<Regex> rxvec;
|
||||||
for(auto&& p: personal) {
|
for (auto&& p: config_db_.get<Id>()) {
|
||||||
if (p.size() < 2 || p[0] != '/' || p[p.length()- 1] != '/')
|
|
||||||
|
mu_debug("--> {}", p);
|
||||||
|
|
||||||
|
if (!is_rx(p))
|
||||||
continue;
|
continue;
|
||||||
// a regex pattern.
|
constexpr auto opts{static_cast<GRegexCompileFlags>(G_REGEX_OPTIMIZE|G_REGEX_CASELESS)};
|
||||||
|
|
||||||
|
const auto rxstr{p.substr(1, p.length() - 2)};
|
||||||
try {
|
try {
|
||||||
const auto rxstr{p.substr(1, p.length() - 2)};
|
rxvec.push_back(unwrap(Regex::make(rxstr, opts)));
|
||||||
auto opts = static_cast<GRegexCompileFlags>(G_REGEX_OPTIMIZE|G_REGEX_CASELESS);
|
mu_debug("match {}: '{}' {}", Config::property<Id>().name,
|
||||||
auto rx = Regex::make(rxstr, opts);
|
p, rxvec.back());
|
||||||
if (!rx)
|
|
||||||
throw rx.error();
|
|
||||||
rxvec.emplace_back(rx.value());
|
|
||||||
} catch (const Error& rex) {
|
} catch (const Error& rex) {
|
||||||
mu_warning("invalid personal address regexp '{}': {}",
|
mu_warning("invalid personal address regexp '{}': {}",
|
||||||
p, rex.what());
|
p, rex.what());
|
||||||
|
@ -354,7 +338,6 @@ address_matches(const std::string& addr, const StringVec& plain, const std::vect
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
ContactsCache::is_personal(const std::string& addr) const
|
ContactsCache::is_personal(const std::string& addr) const
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue