]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/string_view.h
ea38224e888f62b08f4d8b3850fe76fae8105789
[apt.git] / apt-pkg / contrib / 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) && defined(APT_PKG_EXPOSE_STRING_VIEW)
13 #define APT_STRINGVIEW_H
14 #include <string.h>
15 #include <string>
16
17 namespace APT {
18
19 /**
20 * \brief Simple subset of std::string_view from C++17
21 *
22 * This is an internal implementation of the subset of std::string_view
23 * used by APT. It is not meant to be used in programs, only inside the
24 * library for performance critical paths.
25 */
26 class StringView {
27 const char *data_;
28 size_t size_;
29
30 // without this little stunt the "char const *" overload is always preferred
31 // over char[], but if we got a char[] we can deduct the length at compile time
32 #define APT_T_IS_CHARPOINTER template<class T, typename std::enable_if<std::is_pointer<T>{} && !std::is_array<T>{}>::type* = nullptr>
33
34 public:
35 static constexpr size_t npos = static_cast<size_t>(-1);
36 static_assert(APT::StringView::npos == std::string::npos, "npos values are different");
37
38 /* Constructors */
39 constexpr StringView() : data_(""), size_(0) {}
40 constexpr StringView(const char *data, size_t size) : data_(data), size_(size) {}
41
42 template<size_t N> constexpr StringView(char const (&data)[N]) : StringView(data, N-1) {}
43 APT_T_IS_CHARPOINTER StringView(T data) : data_(data), size_(strlen(data)) {}
44
45 StringView(std::string const & str): data_(str.data()), size_(str.size()) {}
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 /* Conversions */
84 std::string to_string() const {
85 return std::string(data_, size_);
86 }
87
88 /* Comparisons */
89 int compare(size_t pos, size_t n, StringView other) const {
90 return substr(pos, n).compare(other);
91 }
92
93 int compare(StringView other) const {
94 int res;
95
96 res = memcmp(data_, other.data_, std::min(size_, other.size_));
97 if (res != 0)
98 return res;
99 if (size_ == other.size_)
100 return res;
101
102 return (size_ > other.size_) ? 1 : -1;
103 }
104
105 /* Optimization: If size not equal, string cannot be equal */
106 bool operator ==(StringView other) const { return size_ == other.size_ && compare(other) == 0; }
107 bool operator !=(StringView other) const { return !(*this == other); }
108
109 /* Accessors */
110 constexpr bool empty() const { return size_ == 0; }
111 constexpr const char* data() const { return data_; }
112 constexpr const char* begin() const { return data_; }
113 constexpr const char* end() const { return data_ + size_; }
114 constexpr char operator [](size_t i) const { return data_[i]; }
115 constexpr size_t size() const { return size_; }
116 constexpr size_t length() const { return size_; }
117 };
118
119
120 }
121
122 template<size_t N> inline bool operator ==(char const (&other)[N], APT::StringView that) { return that.operator==(other); }
123 APT_T_IS_CHARPOINTER inline bool operator ==(T other, APT::StringView that) { return that.operator==(other); }
124
125 #undef APT_T_IS_CHARPOINTER
126 #endif