From 97636fc2d06bcc3347fc8f406975c31506ac91ea Mon Sep 17 00:00:00 2001 From: roker Date: Fri, 8 Oct 2021 03:04:59 +0200 Subject: [PATCH] implement the C++20 methods. makes nfc_string a boost::totally_ordered2 with StringView. --- src/nfc.cc | 19 +++++++++++++++++++ src/nfc.hh | 26 ++++++++++++++++++++------ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/nfc.cc b/src/nfc.cc index f5ea3cc..3e09c66 100644 --- a/src/nfc.cc +++ b/src/nfc.cc @@ -766,7 +766,26 @@ UTF::nfc_string& UTF::nfc_string::operator+=(const UTF::nfc return *this; } +template +bool UTF::nfc_string::starts_with(StringView sv) const noexcept +{ + return (s.size() >= sv.size()) + && (StringView{s.data(), sv.size()} == sv); +} +template +bool UTF::nfc_string::ends_with(StringView sv) const noexcept +{ + return (s.size() >= sv.size()) + && (StringView{s.data() + s.size() - sv.size(), sv.size()} == sv); +} + +template +typename +UTF::nfc_string UTF::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(text).c_str()); // and unecessary temporary std::string etc. diff --git a/src/nfc.hh b/src/nfc.hh index 31b3922..72c286e 100644 --- a/src/nfc.hh +++ b/src/nfc.hh @@ -8,6 +8,7 @@ #include #include #include +#include #include 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> { public: typedef std::basic_string 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 @@ -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 s; @@ -249,10 +248,25 @@ inline typename UTF::nfc_string operator+(const T& left, const typename UTF::nfc_string& right) { - UTF left_s{left}; + typename UTF::nfc_string left_s{left}; return left_s+=right; } +template +inline +bool operator<(const typename UTF::nfc_string& left, std::basic_string_view right) +{ + return left +inline +bool operator==(const typename UTF::nfc_string& left, std::basic_string_view right) +{ + return left==right; +} + + /// convenient alias names: using UTF8 = UTF; using UTF16 = UTF;