From 2330093cffc2b3c846a4128539bf57ac4277c0fb Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Sat, 2 Oct 2021 23:28:15 +0200 Subject: [PATCH] remove support for C++ < 17 and add more convenience functions --- src/nfc.cc | 34 ++++++++++++++++++++++++++++++++++ src/nfc.hh | 5 ++++- src/string_view.hh | 39 +-------------------------------------- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/nfc.cc b/src/nfc.cc index 2c178f3..5116793 100644 --- a/src/nfc.cc +++ b/src/nfc.cc @@ -708,6 +708,40 @@ char* strdup_NFC(string_view s) return ret; } +pEp_identity *identity_dup_NFC(const ::pEp_identity* value) +{ + ::pEp_identity* result = (::pEp_identity*) malloc(sizeof(::pEp_identity)); + if (!result) + throw std::bad_alloc(); + + memcpy(result, value, sizeof(::pEp_identity)); + + result->address = pEp::strdup_NFC(value->address); + result->fpr = pEp::strdup_NFC(value->fpr); + result->user_id = pEp::strdup_NFC(value->user_id); + result->username = pEp::strdup_NFC(value->username); + + return result; +} + +::identity_list* identity_list_dup_NFC(const ::identity_list* value) +{ + ::identity_list* result = ::new_identity_list(nullptr); + if (!result) + throw std::bad_alloc(); + + const ::identity_list* il = value; + ::identity_list* ir = result; + + for (; il && il->ident; il = il->next) { + ir = ::identity_list_add(ir, identity_dup_NFC(il->ident)); + if (!ir) + throw std::bad_alloc(); + } + + return result; +} + template class UTF; template class UTF; diff --git a/src/nfc.hh b/src/nfc.hh index 5e8eb0c..11b6ddc 100644 --- a/src/nfc.hh +++ b/src/nfc.hh @@ -8,6 +8,7 @@ #include #include #include +#include namespace pEp { @@ -109,9 +110,11 @@ std::basic_string toNFC(basic_string_view s); // creates a UTF-8-encoded NFC string from s std::string toNFC_8(u16string_view s); -// convenience function to avoid ::strdup(pEp::toNFC(text).c_str()); +// convenience functions to avoid ::strdup(pEp::toNFC(text).c_str()); // and unecessary temporary std::string etc. char* strdup_NFC(string_view s); +pEp_identity *identity_dup_NFC(const ::pEp_identity* value); +::identity_list* identity_list_dup_NFC(const ::identity_list* value); } // end of namespace pEp diff --git a/src/string_view.hh b/src/string_view.hh index 8f85ca2..fc00d42 100644 --- a/src/string_view.hh +++ b/src/string_view.hh @@ -4,8 +4,7 @@ #ifndef PEP_DATATYPES_STRING_VIEW_HH #define PEP_DATATYPES_STRING_VIEW_HH -#if (__cplusplus >= 201606) // std::string_view is C++17. -# include +#include namespace pEp { @@ -17,42 +16,6 @@ namespace pEp typedef std::u32string_view u32string_view; } -#else // in C++11 / C++14 use boost::string_view instead. -# include - -namespace pEp -{ - template - using basic_string_view = boost::basic_string_view; - - typedef boost::string_view string_view; - typedef boost::u16string_view u16string_view; - typedef boost::u32string_view u32string_view; - - // boost::string_view does not provide these operations, neither by boost nor by the stdlib. :-( - template - inline - std::basic_string& operator+=(std::basic_string& s, const basic_string_view v) - { - s.append(v.data(), v.size()); - return s; - } - - template - inline - std::basic_string operator+(std::basic_string s, const basic_string_view v) - { - return s += v; - } - - -// using ::std::operator+=; -// using ::boost::operator+=; -} // end of namespace pEp - -#endif // C++17 switch - - namespace pEp { constexpr