X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/0e58689ae76e8155bc1f418323aa9e56c34187f6..cc9745a0d81a3e1aa5ef6f99f7ad638d26bdb950:/apt-pkg/contrib/string_view.h diff --git a/apt-pkg/contrib/string_view.h b/apt-pkg/contrib/string_view.h index 37a30a6df..f158ef8d6 100644 --- a/apt-pkg/contrib/string_view.h +++ b/apt-pkg/contrib/string_view.h @@ -13,6 +13,7 @@ #define APT_STRINGVIEW_H #include #include +#include namespace APT { @@ -23,12 +24,13 @@ namespace APT { * used by APT. It is not meant to be used in programs, only inside the * library for performance critical paths. */ -class StringView { +class APT_HIDDEN StringView { const char *data_; size_t size_; public: static constexpr size_t npos = static_cast(-1); + static_assert(APT::StringView::npos == std::string::npos, "npos values are different"); /* Constructors */ constexpr StringView() : data_(""), size_(0) {} @@ -43,10 +45,15 @@ public: return StringView(data_ + pos, n > (size_ - pos) ? (size_ - pos) : n); } - size_t find(int c, size_t pos=0) const { - if (pos != 0) - return substr(pos).find(c); - + size_t find(int c, size_t pos) const { + if (pos == 0) + return find(c); + size_t const found = substr(pos).find(c); + if (found == npos) + return npos; + return pos + found; + } + size_t find(int c) const { const char *found = static_cast(memchr(data_, c, size_)); if (found == NULL) @@ -55,10 +62,12 @@ public: return found - data_; } - size_t rfind(int c, size_t pos=npos) const { - if (pos != npos) - return substr(0, pos).rfind(c); - + size_t rfind(int c, size_t pos) const { + if (pos == npos) + return rfind(c); + return APT::StringView(data_, pos).rfind(c); + } + size_t rfind(int c) const { const char *found = static_cast(memrchr(data_, c, size_)); if (found == NULL) @@ -107,6 +116,6 @@ public: } inline bool operator ==(const char *other, APT::StringView that); -inline bool operator ==(const char *other, APT::StringView that) { return that.compare(other) == 0; } +inline bool operator ==(const char *other, APT::StringView that) { return that.operator==(other); } #endif