summaryrefslogtreecommitdiff
path: root/apt-pkg
diff options
context:
space:
mode:
authorнаб <nabijaczleweli@nabijaczleweli.xyz>2024-11-12 18:01:32 +0100
committerнаб <nabijaczleweli@nabijaczleweli.xyz>2024-11-12 20:23:28 +0100
commit435da86cbc1a598ec5dd26a5329dfd8914992c26 (patch)
tree7ffe4a9c9ad3b4a2b974d477e6869fccc63d44bb /apt-pkg
parentd324b6cc265b6ac3183d722cc11229e10158d960 (diff)
APT::StringView: add operator std::string_view(), StringViewCompareFast() for std::string_views, operator<<(ostream&), std::string_view constructor, operator==(std::string_view)
Diffstat (limited to 'apt-pkg')
-rw-r--r--apt-pkg/contrib/string_view.h17
1 files changed, 13 insertions, 4 deletions
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<std::string_view>(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<class = void> bool operator ==(std::string_view const &other, APT::StringView const &that) { return that.operator==(other); }
+template<class = void> bool operator !=(std::string_view const &other, APT::StringView const &that) { return that.operator!=(other); }
+template<class = void> bool operator !=(APT::StringView const &that, std::string_view const &other) { return that.operator!=(other); }
#endif