From f56aca7c2329c69edfac00f2b1eead0af8c6aade Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Tue, 16 Mar 2021 17:09:37 +0200 Subject: [PATCH] lib/contacts: Use remove_ctrl Use the new helper function rather than `wash'. --- lib/mu-contacts.cc | 30 +++++++++++++----------------- lib/mu-contacts.hh | 5 ++++- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/mu-contacts.cc b/lib/mu-contacts.cc index f562010a..6efe9673 100644 --- a/lib/mu-contacts.cc +++ b/lib/mu-contacts.cc @@ -212,40 +212,36 @@ Contacts::serialize() const return s; } - -static void -wash (std::string& str) -{ - str.erase(std::remove(str.begin(), str.end(), '\n'), str.end()); -} - -void +const ContactInfo Contacts::add (ContactInfo&& ci) { std::lock_guard l_{priv_->mtx_}; auto it = priv_->contacts_.find(ci.email); - if (it == priv_->contacts_.end()) { // completely new contact - wash(ci.name); - wash(ci.full_address); + if (it == priv_->contacts_.end()) { // completely new contact + + ci.name = Mu::remove_ctrl(ci.name); + ci.full_address = remove_ctrl(ci.full_address); + auto email{ci.email}; - priv_->contacts_.emplace(ContactUMap::value_type(email, std::move(ci))); + return priv_->contacts_.emplace(ContactUMap::value_type(email, std::move(ci))).first->second; } else { // existing contact. auto& ci_existing{it->second}; ++ci_existing.freq; if (ci.last_seen > ci_existing.last_seen) { // update. - wash(ci.name); - ci_existing.name = std::move(ci.name); - ci_existing.email = std::move(ci.email); - wash(ci.full_address); - ci_existing.full_address = std::move(ci.full_address); + ci_existing.email = std::move(ci.email); + ci_existing.name = Mu::remove_ctrl(ci.name); + ci_existing.full_address = Mu::remove_ctrl(ci.full_address); + ci_existing.tstamp = g_get_monotonic_time(); ci_existing.last_seen = ci.last_seen; } + + return ci; } } diff --git a/lib/mu-contacts.hh b/lib/mu-contacts.hh index 282d797b..565a4fad 100644 --- a/lib/mu-contacts.hh +++ b/lib/mu-contacts.hh @@ -84,8 +84,11 @@ public: * Add a contact * * @param ci A contact-info object + * + * @return the inserted / updated / washed contact info. Note that + * this is return _as copy_ to make it thread-safe. */ - void add(ContactInfo&& ci); + const ContactInfo add(ContactInfo&& ci); /** * Clear all contacts