apt: apt-pkg/contrib/string_view.h Source File

apt 2.7.6 commandline package manager

string_view.h
1/*
2 * Basic implementation of string_view
3 *
4 * (C) 2015 Julian Andres Klode <jak@debian.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#if !defined(APT_STRINGVIEW_H)
13#define APT_STRINGVIEW_H
14#include <apt-pkg/macros.h>
15#include <string>
16#include <string.h>
17
18namespace APT {
19
27 class StringView {
28 const char *data_;
29 size_t size_;
30
31public:
32 static constexpr size_t npos = static_cast<size_t>(-1);
33 static_assert(APT::StringView::npos == std::string::npos, "npos values are different");
34
35 /* Constructors */
36 constexpr StringView() : data_(""), size_(0) {}
37 constexpr StringView(const char *data, size_t size) : data_(data), size_(size) {}
38
39 StringView(const char *data) : data_(data), size_(strlen(data)) {}
40 StringView(std::string const & str): data_(str.data()), size_(str.size()) {}
41
42 /* Modifiers */
43 void remove_prefix(size_t n) { data_ += n; size_ -= n; }
44 void remove_suffix(size_t n) { size_ -= n; }
45 void clear() { size_ = 0; }
46
47 /* Viewers */
48 constexpr StringView substr(size_t pos, size_t n = npos) const {
49 return StringView(data_ + pos, n > (size_ - pos) ? (size_ - pos) : n);
50 }
51
52 size_t find(int c, size_t pos) const {
53 if (pos == 0)
54 return find(c);
55 size_t const found = substr(pos).find(c);
56 if (found == npos)
57 return npos;
58 return pos + found;
59 }
60 size_t find(int c) const {
61 const char *found = static_cast<const char*>(memchr(data_, c, size_));
62
63 if (found == NULL)
64 return npos;
65
66 return found - data_;
67 }
68
69 size_t rfind(int c, size_t pos) const {
70 if (pos == npos)
71 return rfind(c);
72 return APT::StringView(data_, pos).rfind(c);
73 }
74 size_t rfind(int c) const {
75 const char *found = static_cast<const char*>(memrchr(data_, c, size_));
76
77 if (found == NULL)
78 return npos;
79
80 return found - data_;
81 }
82
83 size_t find(APT::StringView const needle) const {
84 if (needle.empty())
85 return npos;
86 if (needle.length() == 1)
87 return find(*needle.data());
88 size_t found = 0;
89 while ((found = find(*needle.data(), found)) != npos) {
90 if (compare(found, needle.length(), needle) == 0)
91 return found;
92 ++found;
93 }
94 return found;
95 }
96 size_t find(APT::StringView const needle, size_t pos) const {
97 if (pos == 0)
98 return find(needle);
99 size_t const found = substr(pos).find(needle);
100 if (found == npos)
101 return npos;
102 return pos + found;
103 }
104
105 /* Conversions */
106 std::string to_string() const {
107 return std::string(data_, size_);
108 }
109
110 /* Comparisons */
111 int compare(size_t pos, size_t n, StringView other) const {
112 return substr(pos, n).compare(other);
113 }
114
115 int compare(StringView other) const {
116 int res;
117
118 res = memcmp(data_, other.data_, std::min(size_, other.size_));
119 if (res != 0)
120 return res;
121 if (size_ == other.size_)
122 return res;
123
124 return (size_ > other.size_) ? 1 : -1;
125 }
126
127 /* Optimization: If size not equal, string cannot be equal */
128 bool operator ==(StringView other) const { return size_ == other.size_ && compare(other) == 0; }
129 bool operator !=(StringView other) const { return !(*this == other); }
130
131 /* Accessors */
132 constexpr bool empty() const { return size_ == 0; }
133 constexpr const char* data() const { return data_; }
134 constexpr const char* begin() const { return data_; }
135 constexpr const char* end() const { return data_ + size_; }
136 constexpr char operator [](size_t i) const { return data_[i]; }
137 constexpr size_t size() const { return size_; }
138 constexpr size_t length() const { return size_; }
139};
140
145static inline int StringViewCompareFast(StringView a, StringView b) {
146 if (a.size() != b.size())
147 return a.size() - b.size();
148
149 return memcmp(a.data(), b.data(), a.size());
150}
151
152static constexpr inline APT::StringView operator""_sv(const char *data, size_t size)
153{
154 return APT::StringView(data, size);
155}
156}
157
158inline bool operator ==(const char *other, APT::StringView that);
159inline bool operator ==(const char *other, APT::StringView that) { return that.operator==(other); }
160inline bool operator ==(std::string const &other, APT::StringView that);
161inline bool operator ==(std::string const &other, APT::StringView that) { return that.operator==(other); }
162
163#endif
APT::StringView
Simple subset of std::string_view from C++17.
Definition string_view.h:27