Browse Source

remove support for C++ < 17 and add more convenience functions

master
Volker Birk 8 months ago
parent
commit
2330093cff
3 changed files with 39 additions and 39 deletions
  1. +34
    -0
      src/nfc.cc
  2. +4
    -1
      src/nfc.hh
  3. +1
    -38
      src/string_view.hh

+ 34
- 0
src/nfc.cc View File

@ -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<char>;
template class UTF<char16_t>;


+ 4
- 1
src/nfc.hh View File

@ -8,6 +8,7 @@
#include <string>
#include <stdexcept>
#include <iosfwd>
#include <pEp/identity_list.h>
namespace pEp {
@ -109,9 +110,11 @@ std::basic_string<CharT> toNFC(basic_string_view<CharT> s);
// creates a UTF-8-encoded NFC string from s
std::string toNFC_8(u16string_view s);
// convenience function to avoid ::strdup(pEp::toNFC<char>(text).c_str());
// convenience functions to avoid ::strdup(pEp::toNFC<char>(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


+ 1
- 38
src/string_view.hh View File

@ -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 <string_view>
#include <string_view>
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 <boost/utility/string_view.hpp>
namespace pEp
{
template<class CharT>
using basic_string_view = boost::basic_string_view<CharT>;
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<class CharT>
inline
std::basic_string<CharT>& operator+=(std::basic_string<CharT>& s, const basic_string_view<CharT> v)
{
s.append(v.data(), v.size());
return s;
}
template<class CharT>
inline
std::basic_string<CharT> operator+(std::basic_string<CharT> s, const basic_string_view<CharT> v)
{
return s += v;
}
// using ::std::operator+=;
// using ::boost::operator+=;
} // end of namespace pEp
#endif // C++17 switch
namespace pEp
{
constexpr


Loading…
Cancel
Save