Browse Source

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

master
roker 4 months ago
parent
commit
97636fc2d0
2 changed files with 39 additions and 6 deletions
  1. +19
    -0
      src/nfc.cc
  2. +20
    -6
      src/nfc.hh

+ 19
- 0
src/nfc.cc View File

@ -766,7 +766,26 @@ UTF<CharT>::nfc_string& UTF<CharT>::nfc_string::operator+=(const UTF<CharT>::nfc
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());
// and unecessary temporary std::string etc.


+ 20
- 6
src/nfc.hh View File

@ -8,6 +8,7 @@
#include <string>
#include <stdexcept>
#include <iosfwd>
#include <boost/operators.hpp>
#include <pEp/identity_list.h>
namespace pEp {
@ -82,7 +83,7 @@ public:
/// class holding a NFC-conform Unicode string.
/// 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:
typedef std::basic_string<CharT> String;
@ -195,10 +196,10 @@ public:
}
/// stolen from C++20
bool starts_with(StringView s) const;
bool starts_with(StringView s) const noexcept;
/// stolen from C++20
bool ends_with(StringView s) const;
bool ends_with(StringView s) const noexcept;
/// delegates all 5 find() overloads to s
template<typename... Args>
@ -210,8 +211,6 @@ public:
/// 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;
private:
std::basic_string<CharT> s;
@ -249,10 +248,25 @@ inline
typename
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;
}
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:
using UTF8 = UTF<char>;
using UTF16 = UTF<char16_t>;


Loading…
Cancel
Save