Hyper API for C++  0.0.15888
Hyper client library for C++ applications
string_view.hpp
Go to the documentation of this file.
1 
5 #ifndef TABLEAU_HYPER_STRING_VIEW_HPP
6 #define TABLEAU_HYPER_STRING_VIEW_HPP
7 
8 #if !defined(__cplusplus) || (__cplusplus < 201703L) /* Use own string view if C++ is older than C++17 */
9 #define hyper_use_own_string_view
10 #endif
11 
12 #ifdef hyper_use_own_string_view
13 #include <cstring>
14 #include <ostream>
15 #include <string>
16 #else
17 #include <string_view>
18 #endif
19 
20 namespace hyperapi {
21 #ifdef hyper_use_own_string_view
22 
26 class string_view final {
27  public:
29  string_view(const char* data, size_t length) noexcept
30  : m_data(data), m_length(length) {
31  }
32 
34  string_view(const char* c)
35  : m_data(c), m_length(std::strlen(c)) {
36  }
37 
39  string_view(const std::string& s)
40  : m_data(s.data()), m_length(s.size()) {
41  }
42 
46  size_t size() const noexcept { return m_length; }
55  const char* data() const noexcept { return m_data; }
56 
63  int compare(const string_view& other) const noexcept;
64 
68  operator std::string() const { return std::string(data(), size()); }
69 
73  friend bool operator==(const string_view& a, const string_view& b) noexcept { return a.compare(b) == 0; }
77  friend bool operator>(const string_view& a, const string_view& b) noexcept { return a.compare(b) > 0; }
81  friend bool operator!=(const string_view& a, const string_view& b) noexcept { return !(a == b); }
85  friend bool operator<(const string_view& a, const string_view& b) noexcept { return (b > a); }
89  friend bool operator<=(const string_view& a, const string_view& b) noexcept { return (a == b) || (a < b); }
93  friend bool operator>=(const string_view& a, const string_view& b) noexcept { return (a == b) || (a > b); }
94 
96  friend std::ostream& operator<<(std::ostream& os, const string_view& v) { return os.write(v.data(), static_cast<long>(v.size())); }
97 
99  bool empty() { return m_length == 0; }
100 
101  private:
103  const char* m_data = nullptr;
104 
106  size_t m_length = 0;
107 };
108 }
109 
110 namespace std {
111 
113 template <>
114 struct hash<hyperapi::string_view> {
116  size_t operator()(const hyperapi::string_view& sv) const noexcept;
117 };
118 }
119 #else /* C++17 or greater */
120 using string_view = std::string_view;
121 }
122 #endif
123 
124 #ifdef hyper_use_own_string_view
125 #include <hyperapi/impl/string_view.impl.hpp>
126 #endif
127 
128 #endif
hyperapi::string_view::size
size_t size() const noexcept
Returns the number of elements in the view.
Definition: string_view.hpp:46
hyperapi::string_view::compare
int compare(const string_view &other) const noexcept
Compares two character sequences.
hyperapi::string_view::data
const char * data() const noexcept
Returns a pointer to the underlying character array.
Definition: string_view.hpp:55
hyperapi::string_view::operator!=
friend bool operator!=(const string_view &a, const string_view &b) noexcept
Not equal operator.
Definition: string_view.hpp:81
hyperapi::string_view::string_view
string_view(const std::string &s)
Constructor, implicit conversion from a std::string.
Definition: string_view.hpp:39
hyperapi::string_view
Describes an object that can refer to a constant, contiguous sequence of char-like objects.
Definition: string_view.hpp:26
hyperapi::string_view::operator>
friend bool operator>(const string_view &a, const string_view &b) noexcept
Greater operator.
Definition: string_view.hpp:77
hyperapi::string_view::operator<=
friend bool operator<=(const string_view &a, const string_view &b) noexcept
Smaller or equal operator.
Definition: string_view.hpp:89
hyperapi::string_view::operator<<
friend std::ostream & operator<<(std::ostream &os, const string_view &v)
Stream output operator.
Definition: string_view.hpp:96
hyperapi::string_view::operator<
friend bool operator<(const string_view &a, const string_view &b) noexcept
Smaller operator.
Definition: string_view.hpp:85
hyperapi
The primary namespace of the Hyper API for C++.
Definition: ByteSpan.hpp:15
hyperapi::string_view::string_view
string_view(const char *data, size_t length) noexcept
Constructor.
Definition: string_view.hpp:29
hyperapi::string_view::operator>=
friend bool operator>=(const string_view &a, const string_view &b) noexcept
Greater or equal operator.
Definition: string_view.hpp:93
hyperapi::string_view::string_view
string_view(const char *c)
Constructor, implicit conversion from a null terminated character array.
Definition: string_view.hpp:34
hyperapi::string_view::operator==
friend bool operator==(const string_view &a, const string_view &b) noexcept
Equality operator.
Definition: string_view.hpp:73
hyperapi::string_view::empty
bool empty()
Returns whether the view is empty.
Definition: string_view.hpp:99