Browse Source

implement the C++20 methods. makes nfc_string a boost::totally_ordered2 with StringView.

master
roker 4 years ago
parent
commit
97636fc2d0
  1. 19
      src/nfc.cc
  2. 26
      src/nfc.hh

19
src/nfc.cc

@ -766,7 +766,26 @@ UTF<CharT>::nfc_string& UTF<CharT>::nfc_string::operator+=(const UTF<CharT>::nfc
return *this; return *this;
} }
template<class CharT>
bool UTF<CharT>::nfc_string::starts_with(StringView sv) const noexcept
{
return (s.size() >= sv.size())
&& (StringView{s.data(), sv.size()} == sv);
}
template<class CharT>
bool UTF<CharT>::nfc_string::ends_with(StringView sv) const noexcept
{
return (s.size() >= sv.size())
&& (StringView{s.data() + s.size() - sv.size(), sv.size()} == sv);
}
template<class CharT>
typename
UTF<CharT>::nfc_string UTF<CharT>::nfc_string::substr(std::size_t pos, std::size_t count) const
{
return nfc_string{s.substr(pos,count)};
}
// convenience function to avoid ::strdup(pEp::toNFC<char>(text).c_str()); // convenience function to avoid ::strdup(pEp::toNFC<char>(text).c_str());
// and unecessary temporary std::string etc. // and unecessary temporary std::string etc.

26
src/nfc.hh

@ -8,6 +8,7 @@
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#include <iosfwd> #include <iosfwd>
#include <boost/operators.hpp>
#include <pEp/identity_list.h> #include <pEp/identity_list.h>
namespace pEp { namespace pEp {
@ -82,7 +83,7 @@ public:
/// class holding a NFC-conform Unicode string. /// class holding a NFC-conform Unicode string.
/// content is mostly read-only, because arbitrary modifications might destroy NFC conformacy. /// content is mostly read-only, because arbitrary modifications might destroy NFC conformacy.
class nfc_string class nfc_string : public boost::totally_ordered2<nfc_string, std::basic_string_view<CharT>>
{ {
public: public:
typedef std::basic_string<CharT> String; typedef std::basic_string<CharT> String;
@ -195,10 +196,10 @@ public:
} }
/// stolen from C++20 /// stolen from C++20
bool starts_with(StringView s) const; bool starts_with(StringView s) const noexcept;
/// stolen from C++20 /// stolen from C++20
bool ends_with(StringView s) const; bool ends_with(StringView s) const noexcept;
/// delegates all 5 find() overloads to s /// delegates all 5 find() overloads to s
template<typename... Args> template<typename... Args>
@ -210,8 +211,6 @@ public:
/// might throw illegal_utf, if a multi-char sequence is clipped. /// might throw illegal_utf, if a multi-char sequence is clipped.
nfc_string substr(std::size_t pos=0, std::size_t count=npos) const; nfc_string substr(std::size_t pos=0, std::size_t count=npos) const;
private: private:
std::basic_string<CharT> s; std::basic_string<CharT> s;
@ -249,10 +248,25 @@ inline
typename typename
UTF<CharT>::nfc_string operator+(const T& left, const typename UTF<CharT>::nfc_string& right) UTF<CharT>::nfc_string operator+(const T& left, const typename UTF<CharT>::nfc_string& right)
{ {
UTF<CharT> left_s{left}; typename UTF<CharT>::nfc_string left_s{left};
return left_s+=right; return left_s+=right;
} }
template<class CharT>
inline
bool operator<(const typename UTF<CharT>::nfc_string& left, std::basic_string_view<CharT> right)
{
return left<right;
}
template<class CharT>
inline
bool operator==(const typename UTF<CharT>::nfc_string& left, std::basic_string_view<CharT> right)
{
return left==right;
}
/// convenient alias names: /// convenient alias names:
using UTF8 = UTF<char>; using UTF8 = UTF<char>;
using UTF16 = UTF<char16_t>; using UTF16 = UTF<char16_t>;

Loading…
Cancel
Save