#define APT_STRINGVIEW_H
#include <string.h>
#include <string>
+#include <apt-pkg/macros.h>
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<size_t>(-1);
+ static_assert(APT::StringView::npos == std::string::npos, "npos values are different");
/* Constructors */
constexpr StringView() : data_(""), size_(0) {}
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<const char*>(memchr(data_, c, size_));
if (found == NULL)
return found - data_;
}
- size_t rfind(int c, size_t pos=npos) const {
- if (pos != npos)
- return substr(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<const char*>(memrchr(data_, c, size_));
if (found == NULL)
}
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