diff options
author | swarmer <swarmer@yandex-team.ru> | 2022-06-01 08:35:40 +0300 |
---|---|---|
committer | swarmer <swarmer@yandex-team.ru> | 2022-06-01 08:35:40 +0300 |
commit | 4ee953e82965c006d3eb1ceef4e0451178c58064 (patch) | |
tree | 274e6fdcd57ab368d9a9bbfa2f4fdb1f9b0bbcd4 | |
parent | b9ca6add54055c55757fe7afcfcb408d8530e8f9 (diff) | |
download | ydb-4ee953e82965c006d3eb1ceef4e0451178c58064.tar.gz |
[util] constexpr сравнения для TStringBuf
ref:695923dc5d543fcc10893896407694ebc55f3e84
-rw-r--r-- | util/generic/strbase.h | 118 | ||||
-rw-r--r-- | util/generic/strbuf_ut.cpp | 12 |
2 files changed, 71 insertions, 59 deletions
diff --git a/util/generic/strbase.h b/util/generic/strbase.h index ab39fc7537..08824943b1 100644 --- a/util/generic/strbase.h +++ b/util/generic/strbase.h @@ -135,35 +135,35 @@ public: return begin() <= it && end() > it ? size_t(it - begin()) : npos; } - inline const_iterator begin() const noexcept { + constexpr const_iterator begin() const noexcept { return Ptr(); } - inline const_iterator end() const noexcept { + constexpr const_iterator end() const noexcept { return Ptr() + size(); } - inline const_iterator cbegin() const noexcept { + constexpr const_iterator cbegin() const noexcept { return begin(); } - inline const_iterator cend() const noexcept { + constexpr const_iterator cend() const noexcept { return end(); } - inline const_reverse_iterator rbegin() const noexcept { + constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(Ptr() + size()); } - inline const_reverse_iterator rend() const noexcept { + constexpr const_reverse_iterator rend() const noexcept { return const_reverse_iterator(Ptr()); } - inline const_reverse_iterator crbegin() const noexcept { + constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); } - inline const_reverse_iterator crend() const noexcept { + constexpr const_reverse_iterator crend() const noexcept { return rend(); } @@ -211,57 +211,57 @@ public: // style-guide compliant methods } private: - static inline TStringView LegacySubString(const TStringView view, size_t p, size_t n) noexcept { + static constexpr TStringView LegacySubString(const TStringView view, size_t p, size_t n) noexcept { p = Min(p, view.length()); return view.substr(p, n); } public: // ~~~ Comparison ~~~ : FAMILY0(int, compare) - static int compare(const TSelf& s1, const TSelf& s2) noexcept { + static constexpr int compare(const TSelf& s1, const TSelf& s2) noexcept { return s1.AsStringView().compare(s2.AsStringView()); } - static int compare(const TCharType* p, const TSelf& s2) noexcept { + static constexpr int compare(const TCharType* p, const TSelf& s2) noexcept { TCharType null{0}; return TStringViewWithTraits(p ? p : &null).compare(s2.AsStringView()); } - static int compare(const TSelf& s1, const TCharType* p) noexcept { + static constexpr int compare(const TSelf& s1, const TCharType* p) noexcept { TCharType null{0}; return s1.AsStringView().compare(p ? p : &null); } - static int compare(const TStringView s1, const TStringView s2) noexcept { + static constexpr int compare(const TStringView s1, const TStringView s2) noexcept { return TStringViewWithTraits(s1.data(), s1.size()).compare(TStringViewWithTraits(s2.data(), s2.size())); } template <class T> - inline int compare(const T& t) const noexcept { + constexpr int compare(const T& t) const noexcept { return compare(*this, t); } - inline int compare(size_t p, size_t n, const TStringView t) const noexcept { + constexpr int compare(size_t p, size_t n, const TStringView t) const noexcept { return compare(LegacySubString(*this, p, n), t); } - inline int compare(size_t p, size_t n, const TStringView t, size_t p1, size_t n1) const noexcept { + constexpr int compare(size_t p, size_t n, const TStringView t, size_t p1, size_t n1) const noexcept { return compare(LegacySubString(*this, p, n), LegacySubString(t, p1, n1)); } - inline int compare(size_t p, size_t n, const TStringView t, size_t n1) const noexcept { + constexpr int compare(size_t p, size_t n, const TStringView t, size_t n1) const noexcept { return compare(LegacySubString(*this, p, n), LegacySubString(t, 0, n1)); } - inline int compare(const TCharType* p, size_t len) const noexcept { + constexpr int compare(const TCharType* p, size_t len) const noexcept { return compare(*this, TStringView(p, len)); } - static bool equal(const TSelf& s1, const TSelf& s2) noexcept { + static constexpr bool equal(const TSelf& s1, const TSelf& s2) noexcept { return s1.AsStringView() == s2.AsStringView(); } - static bool equal(const TSelf& s1, const TCharType* p) noexcept { + static constexpr bool equal(const TSelf& s1, const TCharType* p) noexcept { if (p == nullptr) { return s1.Len() == 0; } @@ -269,164 +269,164 @@ public: return s1.AsStringView() == p; } - static bool equal(const TCharType* p, const TSelf& s2) noexcept { + static constexpr bool equal(const TCharType* p, const TSelf& s2) noexcept { return equal(s2, p); } - static bool equal(const TStringView s1, const TStringView s2) noexcept { + static constexpr bool equal(const TStringView s1, const TStringView s2) noexcept { return TStringViewWithTraits{s1.data(), s1.size()} == TStringViewWithTraits{s2.data(), s2.size()}; } template <class T> - inline bool equal(const T& t) const noexcept { + constexpr bool equal(const T& t) const noexcept { return equal(*this, t); } - inline bool equal(size_t p, size_t n, const TStringView t) const noexcept { + constexpr bool equal(size_t p, size_t n, const TStringView t) const noexcept { return equal(LegacySubString(*this, p, n), t); } - inline bool equal(size_t p, size_t n, const TStringView t, size_t p1, size_t n1) const noexcept { + constexpr bool equal(size_t p, size_t n, const TStringView t, size_t p1, size_t n1) const noexcept { return equal(LegacySubString(*this, p, n), LegacySubString(t, p1, n1)); } - inline bool equal(size_t p, size_t n, const TStringView t, size_t n1) const noexcept { + constexpr bool equal(size_t p, size_t n, const TStringView t, size_t n1) const noexcept { return equal(LegacySubString(*this, p, n), LegacySubString(t, 0, n1)); } - static inline bool StartsWith(const TCharType* what, size_t whatLen, const TCharType* with, size_t withLen) noexcept { + static constexpr bool StartsWith(const TCharType* what, size_t whatLen, const TCharType* with, size_t withLen) noexcept { return withLen <= whatLen && TStringViewWithTraits(what, withLen) == TStringViewWithTraits(with, withLen); } - static inline bool EndsWith(const TCharType* what, size_t whatLen, const TCharType* with, size_t withLen) noexcept { + static constexpr bool EndsWith(const TCharType* what, size_t whatLen, const TCharType* with, size_t withLen) noexcept { return withLen <= whatLen && TStringViewWithTraits(what + whatLen - withLen, withLen) == TStringViewWithTraits(with, withLen); } - inline bool StartsWith(const TCharType* s, size_t n) const noexcept { + constexpr bool StartsWith(const TCharType* s, size_t n) const noexcept { return StartsWith(Ptr(), Len(), s, n); } - inline bool StartsWith(const TStringView s) const noexcept { + constexpr bool StartsWith(const TStringView s) const noexcept { return StartsWith(s.data(), s.length()); } - inline bool StartsWith(TCharType ch) const noexcept { + constexpr bool StartsWith(TCharType ch) const noexcept { return !empty() && TTraits::eq(*Ptr(), ch); } - inline bool EndsWith(const TCharType* s, size_t n) const noexcept { + constexpr bool EndsWith(const TCharType* s, size_t n) const noexcept { return EndsWith(Ptr(), Len(), s, n); } - inline bool EndsWith(const TStringView s) const noexcept { + constexpr bool EndsWith(const TStringView s) const noexcept { return EndsWith(s.data(), s.length()); } - inline bool EndsWith(TCharType ch) const noexcept { + constexpr bool EndsWith(TCharType ch) const noexcept { return !empty() && TTraits::eq(Ptr()[Len() - 1], ch); } template <typename TDerived2, typename TTraits2> - bool operator==(const TStringBase<TDerived2, TChar, TTraits2>& s2) const noexcept { + constexpr bool operator==(const TStringBase<TDerived2, TChar, TTraits2>& s2) const noexcept { return equal(*this, s2); } - bool operator==(TStringView s2) const noexcept { + constexpr bool operator==(TStringView s2) const noexcept { return equal(*this, s2); } - bool operator==(const TCharType* pc) const noexcept { + constexpr bool operator==(const TCharType* pc) const noexcept { return equal(*this, pc); } #ifndef __cpp_impl_three_way_comparison - friend bool operator==(const TCharType* pc, const TSelf& s) noexcept { + friend constexpr bool operator==(const TCharType* pc, const TSelf& s) noexcept { return equal(pc, s); } template <typename TDerived2, typename TTraits2> - friend bool operator!=(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept { + friend constexpr bool operator!=(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept { return !(s1 == s2); } - friend bool operator!=(const TSelf& s1, TStringView s2) noexcept { + friend constexpr bool operator!=(const TSelf& s1, TStringView s2) noexcept { return !(s1 == s2); } - friend bool operator!=(const TSelf& s, const TCharType* pc) noexcept { + friend constexpr bool operator!=(const TSelf& s, const TCharType* pc) noexcept { return !(s == pc); } - friend bool operator!=(const TCharType* pc, const TSelf& s) noexcept { + friend constexpr bool operator!=(const TCharType* pc, const TSelf& s) noexcept { return !(pc == s); } #endif template <typename TDerived2, typename TTraits2> - friend bool operator<(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept { + friend constexpr bool operator<(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept { return compare(s1, s2) < 0; } - friend bool operator<(const TSelf& s1, TStringView s2) noexcept { + friend constexpr bool operator<(const TSelf& s1, TStringView s2) noexcept { return compare(s1, s2) < 0; } - friend bool operator<(const TSelf& s, const TCharType* pc) noexcept { + friend constexpr bool operator<(const TSelf& s, const TCharType* pc) noexcept { return compare(s, pc) < 0; } - friend bool operator<(const TCharType* pc, const TSelf& s) noexcept { + friend constexpr bool operator<(const TCharType* pc, const TSelf& s) noexcept { return compare(pc, s) < 0; } template <typename TDerived2, typename TTraits2> - friend bool operator<=(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept { + friend constexpr bool operator<=(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept { return compare(s1, s2) <= 0; } - friend bool operator<=(const TSelf& s1, TStringView s2) noexcept { + friend constexpr bool operator<=(const TSelf& s1, TStringView s2) noexcept { return compare(s1, s2) <= 0; } - friend bool operator<=(const TSelf& s, const TCharType* pc) noexcept { + friend constexpr bool operator<=(const TSelf& s, const TCharType* pc) noexcept { return compare(s, pc) <= 0; } - friend bool operator<=(const TCharType* pc, const TSelf& s) noexcept { + friend constexpr bool operator<=(const TCharType* pc, const TSelf& s) noexcept { return compare(pc, s) <= 0; } template <typename TDerived2, typename TTraits2> - friend bool operator>(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept { + friend constexpr bool operator>(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept { return compare(s1, s2) > 0; } - friend bool operator>(const TSelf& s1, TStringView s2) noexcept { + friend constexpr bool operator>(const TSelf& s1, TStringView s2) noexcept { return compare(s1, s2) > 0; } - friend bool operator>(const TSelf& s, const TCharType* pc) noexcept { + friend constexpr bool operator>(const TSelf& s, const TCharType* pc) noexcept { return compare(s, pc) > 0; } - friend bool operator>(const TCharType* pc, const TSelf& s) noexcept { + friend constexpr bool operator>(const TCharType* pc, const TSelf& s) noexcept { return compare(pc, s) > 0; } template <typename TDerived2, typename TTraits2> - friend bool operator>=(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept { + friend constexpr bool operator>=(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept { return compare(s1, s2) >= 0; } - friend bool operator>=(const TSelf& s1, TStringView s2) noexcept { + friend constexpr bool operator>=(const TSelf& s1, TStringView s2) noexcept { return compare(s1, s2) >= 0; } - friend bool operator>=(const TSelf& s, const TCharType* pc) noexcept { + friend constexpr bool operator>=(const TSelf& s, const TCharType* pc) noexcept { return compare(s, pc) >= 0; } - friend bool operator>=(const TCharType* pc, const TSelf& s) noexcept { + friend constexpr bool operator>=(const TCharType* pc, const TSelf& s) noexcept { return compare(pc, s) >= 0; } @@ -581,7 +581,7 @@ public: private: using GenericFinder = const TCharType* (*)(const TCharType*, size_t, const TCharType*, size_t); - TStringViewWithTraits AsStringView() const { + constexpr TStringViewWithTraits AsStringView() const { return static_cast<TStringViewWithTraits>(*this); } diff --git a/util/generic/strbuf_ut.cpp b/util/generic/strbuf_ut.cpp index 69cde785af..0b1a5d0eb5 100644 --- a/util/generic/strbuf_ut.cpp +++ b/util/generic/strbuf_ut.cpp @@ -49,6 +49,16 @@ Y_UNIT_TEST_SUITE(TStrBufTest) { static_assert(str1.size() == str4.size()); } + Y_UNIT_TEST(TestConstExprComparison) { + static constexpr TStringBuf str1("qwe\0rty"sv); + static constexpr TStringBuf str2("qw"); + + static_assert(str1 != str2); + static_assert(str1 >= str2); + static_assert(str1.StartsWith(str2)); + static_assert(!str1.EndsWith(str2)); + } + Y_UNIT_TEST(TestAfter) { TStringBuf str("qwerty"); @@ -369,5 +379,7 @@ Y_UNIT_TEST_SUITE(TWtrBufTest) { UNIT_ASSERT_VALUES_EQUAL(str1, str4); static_assert(str1.data() == str4.data()); static_assert(str1.size() == str4.size()); + + static_assert(str1 == str2); } } |