From 435da86cbc1a598ec5dd26a5329dfd8914992c26 Mon Sep 17 00:00:00 2001 From: наб Date: Tue, 12 Nov 2024 18:01:32 +0100 Subject: APT::StringView: add operator std::string_view(), StringViewCompareFast() for std::string_views, operator<<(ostream&), std::string_view constructor, operator==(std::string_view) --- apt-pkg/contrib/string_view.h | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'apt-pkg/contrib/string_view.h') diff --git a/apt-pkg/contrib/string_view.h b/apt-pkg/contrib/string_view.h index 71f47f563..b062517b6 100644 --- a/apt-pkg/contrib/string_view.h +++ b/apt-pkg/contrib/string_view.h @@ -38,6 +38,7 @@ public: StringView(const char *data) : data_(data), size_(strlen(data)) {} StringView(std::string const & str): data_(str.data()), size_(str.size()) {} + inline StringView(std::string_view const & str): data_(str.data()), size_(str.size()) {} /* Modifiers */ void remove_prefix(size_t n) { data_ += n; size_ -= n; } @@ -136,17 +137,24 @@ public: constexpr char operator [](size_t i) const { return data_[i]; } constexpr size_t size() const { return size_; } constexpr size_t length() const { return size_; } + + inline constexpr operator std::string_view() const { return {data_, size_}; } }; +inline std::ostream& operator<<(std::ostream& os, const StringView& sv) +{ + return os << static_cast(sv); +} + /** * \brief Faster comparison for string views (compare size before data) * * Still stable, but faster than the normal ordering. */ -static inline int StringViewCompareFast(StringView a, StringView b) { +static inline int StringViewCompareFast(const std::string_view & a, const std::string_view & b) { if (a.size() != b.size()) return a.size() - b.size(); - return memcmp(a.data(), b.data(), a.size()); + return a.compare(b); } static constexpr inline APT::StringView operator""_sv(const char *data, size_t size) @@ -157,7 +165,8 @@ static constexpr inline APT::StringView operator""_sv(const char *data, size_t s inline bool operator ==(const char *other, APT::StringView that); inline bool operator ==(const char *other, APT::StringView that) { return that.operator==(other); } -inline bool operator ==(std::string const &other, APT::StringView that); -inline bool operator ==(std::string const &other, APT::StringView that) { return that.operator==(other); } +template bool operator ==(std::string_view const &other, APT::StringView const &that) { return that.operator==(other); } +template bool operator !=(std::string_view const &other, APT::StringView const &that) { return that.operator!=(other); } +template bool operator !=(APT::StringView const &that, std::string_view const &other) { return that.operator!=(other); } #endif -- cgit v1.2.3-70-g09d2