apt 2.9.14 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 <algorithm>
16#include <cstring>
17#include <iosfwd>
18#include <string>
19#include <string_view>
20
21#if APT_PKG_ABI > 600
22 namespace APT {
23 using StringView = std::string_view;
24}
25#else
26 namespace APT {
27
35 class StringView {
36 const char *data_;
37 size_t size_;
38
39 public:
40 static constexpr size_t npos = static_cast<size_t>(-1);
41 static_assert(APT::StringView::npos == std::string::npos, "npos values are different");
42
43 /* Constructors */
44 constexpr StringView() : data_(""), size_(0) {}
45 constexpr StringView(const char *data, size_t size) : data_(data), size_(size) {}
46
47 StringView(const char *data) : data_(data), size_(strlen(data)) {}
48 StringView(std::string const & str): data_(str.data()), size_(str.size()) {}
49 inline StringView(std::string_view const & str): data_(str.data()), size_(str.size()) {}
50
51 /* Modifiers */
52 void remove_prefix(size_t n) { data_ += n; size_ -= n; }
53 void remove_suffix(size_t n) { size_ -= n; }
54 void clear() { size_ = 0; }
55
56 /* Viewers */
57 constexpr StringView substr(size_t pos, size_t n = npos) const {
58 return StringView(data_ + pos, n > (size_ - pos) ? (size_ - pos) : n);
59 }
60
61 size_t find(int c, size_t pos) const {
62 if (pos == 0)
63 return find(c);
64 size_t const found = substr(pos).find(c);
65 if (found == npos)
66 return npos;
67 return pos + found;
68 }
69 size_t find(int c) const {
70 const char *found = static_cast<const char*>(memchr(data_, c, size_));
71
72 if (found == NULL)
73 return npos;
74
75 return found - data_;
76 }
77
78 size_t rfind(int c, size_t pos) const {
79 if (pos == npos)
80 return rfind(c);
81 return APT::StringView(data_, pos).rfind(c);
82 }
83 size_t rfind(int c) const {
84 const char *found = static_cast<const char*>(memrchr(data_, c, size_));
85
86 if (found == NULL)
87 return npos;
88
89 return found - data_;
90 }
91
92 size_t find(APT::StringView const needle) const {
93 if (needle.empty())
94 return npos;
95 if (needle.length() == 1)
96 return find(*needle.data());
97 size_t found = 0;
98 while ((found = find(*needle.data(), found)) != npos) {
99 if (compare(found, needle.length(), needle) == 0)
100 return found;
101 ++found;
102 }
103 return found;
104 }
105 size_t find(APT::StringView const needle, size_t pos) const {
106 if (pos == 0)
107 return find(needle);
108 size_t const found = substr(pos).find(needle);
109 if (found == npos)
110 return npos;
111 return pos + found;
112 }
113
114 /* Conversions */
115 std::string to_string() const {
116 return std::string(data_, size_);
117 }
118
119 /* Comparisons */
120 int compare(size_t pos, size_t n, StringView other) const {
121 return substr(pos, n).compare(other);
122 }
123
124 int compare(StringView other) const {
125 int res;
126
127 res = memcmp(data_, other.data_, std::min(size_, other.size_));
128 if (res != 0)
129 return res;
130 if (size_ == other.size_)
131 return res;
132
133 return (size_ > other.size_) ? 1 : -1;
134 }
135
136 /* Optimization: If size not equal, string cannot be equal */
137 bool operator ==(StringView other) const { return size_ == other.size_ && compare(other) == 0; }
138 bool operator !=(StringView other) const { return !(*this == other); }
139
140 /* Accessors */
141 constexpr bool empty() const { return size_ == 0; }
142 constexpr const char* data() const { return data_; }
143 constexpr const char* begin() const { return data_; }
144 constexpr const char* end() const { return data_ + size_; }
145 constexpr char operator [](size_t i) const { return data_[i]; }
146 constexpr size_t size() const { return size_; }
147 constexpr size_t length() const { return size_; }
148
149 inline constexpr operator std::string_view() const { return {data_, size_}; }
150};
151
152 inline std::ostream& operator<<(std::ostream& os, const StringView& sv)
153{
154 return os << static_cast<std::string_view>(sv);
155}
156
157 static constexpr inline APT::StringView operator""_sv(const char *data, size_t size)
158{
159 return APT::StringView(data, size);
160}
161}
162
163 inline bool operator ==(const char *other, APT::StringView that);
164 inline bool operator ==(const char *other, APT::StringView that) { return that.operator==(other); }
165 template<class = void> bool operator ==(std::string_view const &other, APT::StringView const &that) { return that.operator==(other); }
166 template<class = void> bool operator !=(std::string_view const &other, APT::StringView const &that) { return that.operator!=(other); }
167 template<class = void> bool operator !=(APT::StringView const &that, std::string_view const &other) { return that.operator!=(other); }
168#endif
169
170#endif
APT::StringView
Simple subset of std::string_view from C++17.
Definition string_view.h:35