diff --git a/src/nfc.cc b/src/nfc.cc index 61f2925..5a04436 100644 --- a/src/nfc.cc +++ b/src/nfc.cc @@ -684,10 +684,32 @@ size_t UTF::utf_length(std::u32string_view s) template -UTF::nfc_string::nfc_string(std::basic_string_view src) +UTF::nfc_string::nfc_string(StringView src) : s{ UTF::toNFC(src) } {} +template +UTF::nfc_string::nfc_string(String&& src) +: s{ isNFC_quick_check(src)==IsNFC::Yes ? std::move(src) : toNFC(src) } +{} + + +template +typename +UTF::nfc_string& UTF::nfc_string::assign(StringView src) +{ + s = toNFC(src); + return *this; +} + +template +typename +UTF::nfc_string& UTF::nfc_string::assign(String&& src) +{ + s = (isNFC_quick_check(src)==IsNFC::Yes) ? std::move(src) : toNFC(src); + return *this; +} + // 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 f52fb2a..93cfece 100644 --- a/src/nfc.hh +++ b/src/nfc.hh @@ -82,7 +82,8 @@ public: class nfc_string { public: - typedef std::basic_string String; + typedef std::basic_string String; + typedef std::basic_string_view StringView; /// only const_reference is supported. typedef typename String::const_reference const_reference; @@ -92,13 +93,17 @@ public: typedef typename String::const_iterator const_iterator; - explicit nfc_string(std::basic_string_view src); - explicit nfc_string(std::basic_string && src); + explicit nfc_string(StringView src); + explicit nfc_string(String && src); /// construct from a NUL-terminated src - explicit nfc_string(const CharT* src); + explicit nfc_string(const CharT* src) + : nfc_string{ StringView{src} } + {} - nfc_string(const CharT* src, size_t length); + nfc_string(const CharT* src, size_t length) + : nfc_string{ StringView{src, length} } + {} nfc_string(const nfc_string& src) = default; nfc_string( nfc_string&& src) = default; @@ -106,8 +111,8 @@ public: nfc_string& operator=(const nfc_string& src) = default; nfc_string& operator=( nfc_string&& src) = default; - nfc_string& assign(String&& src); - nfc_string& assign(std::basic_string_view src); + nfc_string& assign(StringView src); + nfc_string& assign(String && src); /// read-only: shares representation @@ -121,6 +126,11 @@ public: std::size_t size() const noexcept { return s.size(); } bool empty() const noexcept { return s.empty(); } + const_iterator begin() const noexcept { return s.cbegin(); } + const_iterator cbegin() const noexcept { return s.cbegin(); } /// r/o access only + const_iterator end() const noexcept { return s.cend(); } + const_iterator cend() const noexcept { return s.cend(); } /// r/o access only + private: std::basic_string s; };