diff options
author | Jannis M. Hoffmann <jannis@fehcom.de> | 2023-10-10 22:49:54 +0200 |
---|---|---|
committer | Jannis M. Hoffmann <jannis@fehcom.de> | 2023-10-10 22:49:54 +0200 |
commit | 7d4cbf31c0b30a29aa82dd6bcedc3d3cf7e7a811 (patch) | |
tree | b953108a5deab5a3709aa222132858dba5febdcc /src | |
parent | cc8b0c0a7061a0b674077d58c28cd1ba4d3eafc1 (diff) |
turn Address into an aggregate
add new trim implementation based on string_view
Diffstat (limited to 'src')
-rw-r--r-- | src/address.cc | 48 | ||||
-rw-r--r-- | src/include/address.h | 13 | ||||
-rw-r--r-- | src/include/convert.h | 17 | ||||
-rw-r--r-- | src/maildirmessage.cc | 2 |
4 files changed, 42 insertions, 38 deletions
diff --git a/src/address.cc b/src/address.cc index 378eff5..94c812f 100644 --- a/src/address.cc +++ b/src/address.cc @@ -1,8 +1,8 @@ /** - * @file address.cc - * @brief Implementation of the Address class - * @author Andreas Aardal Hanssen - * @date 2005 + * @file address.cc + * @brief Implementation of the Address class + * @author Andreas Aardal Hanssen + * @date 2005 */ #include "address.h" @@ -13,48 +13,34 @@ using namespace Binc; using std::string; +using std::string_view; -Address::Address(const string &name, const string &addr) +Address Address::from(string name, string_view addr) { - auto pos = addr.find('@'); - - this->name = name; - - if (pos != string::npos) { - this->local = addr.substr(0, pos); - this->host = addr.substr(pos + 1); - } else { - this->local = addr; - } + if (auto pos = addr.find('@'); pos != string::npos) + return Address{std::move(name), string{addr.substr(0, pos)}, string{addr.substr(pos + 1)}}; + else + return Address{std::move(name), string{addr}}; } -Address::Address(const string &wholeaddress) +Address Address::from(string_view wholeaddress) { auto start = wholeaddress.find('<'); - string addr; - - if (start != string::npos) - addr = wholeaddress.substr(start + 1); - else - addr = wholeaddress; + auto addr = start != string::npos ? wholeaddress.substr(start + 1) : wholeaddress; + auto name = start != string::npos ? wholeaddress.substr(0, start) : ""; trim(addr, "<>"); - - if (start != string::npos) - name = wholeaddress.substr(0, start); - else - name = ""; - trim(name); trim(name, "\""); start = addr.find('@'); - local = addr.substr(0, start); - host = addr.substr(start + 1); + auto local = addr.substr(0, start); + auto host = addr.substr(start + 1); trim(local); trim(host); - trim(name); + + return Address{string{name}, string{local}, string{host}}; } string Address::toParenList() const diff --git a/src/include/address.h b/src/include/address.h index 2cb4aef..2e354be 100644 --- a/src/include/address.h +++ b/src/include/address.h @@ -1,14 +1,15 @@ /** - * @file address.h - * @brief Declaration of the Address class. - * @author Andreas Aardal Hanssen - * @date 2002-2005 + * @file address.h + * @brief Declaration of the Address class. + * @author Andreas Aardal Hanssen + * @date 2002-2005 */ #ifndef address_h_included #define address_h_included #include <string> +#include <string_view> namespace Binc { @@ -19,8 +20,8 @@ namespace Binc { std::string toParenList() const; - Address(const std::string &name, const std::string &addr); - Address(const std::string &wholeaddr); + static Address from(std::string name, std::string_view addr); + static Address from(std::string_view wholeaddr); }; } diff --git a/src/include/convert.h b/src/include/convert.h index 0c7e703..030c1b5 100644 --- a/src/include/convert.h +++ b/src/include/convert.h @@ -15,6 +15,7 @@ #include <iomanip> #include <iostream> #include <string> +#include <string_view> #include <vector> #include <stdio.h> @@ -130,6 +131,22 @@ namespace Binc { chomp(s_in, chars); } + inline void trim(std::string_view &s_in, std::string_view chars = " \t\r\n") + { + size_t n = 0; + while (n < s_in.length() && chars.find(s_in[n]) != std::string_view::npos) { + n++; + } + s_in = s_in.substr(n); + + if (s_in.empty()) return; + + n = s_in.length(); + while (n > 0 && chars.find(s_in[--n]) != std::string_view::npos) { + } + s_in = s_in.substr(0, n + 1); + } + inline const std::string unfold(const std::string &a, bool removecomment = true) { std::string tmp; diff --git a/src/maildirmessage.cc b/src/maildirmessage.cc index 0ebbad7..95951db 100644 --- a/src/maildirmessage.cc +++ b/src/maildirmessage.cc @@ -65,7 +65,7 @@ namespace { if (addr.size() != 0) { io << "("; for (const auto &i : addr) - io << Address(i).toParenList(); + io << Address::from(i).toParenList(); io << ")"; } else { io << "NIL"; |