Hyper API for C++ 0.0.20027
Hyper client library for C++ applications
Loading...
Searching...
No Matches
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
20namespace hyperapi {
21#ifdef hyper_use_own_string_view
26class string_view final {
27 public:
29 string_view(const char* data, size_t length) noexcept
30 : data_(data), length_(length) {
31 }
32
34 string_view(const char* c)
35 : data_(c), length_(std::strlen(c)) {
36 }
37
39 string_view(const std::string& s)
40 : data_(s.data()), length_(s.size()) {
41 }
42
46 size_t size() const noexcept { return length_; }
55 const char* data() const noexcept { return 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 length_ == 0; }
100
101 private:
103 const char* data_ = nullptr;
104
106 size_t length_ = 0;
107};
108}
109
110namespace std {
111
113template <>
114struct hash<hyperapi::string_view> {
116 size_t operator()(const hyperapi::string_view& sv) const noexcept;
117};
118}
119#else /* C++17 or greater */
120using 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
Describes an object that can refer to a constant, contiguous sequence of char-like objects.
friend bool operator>=(const string_view &a, const string_view &b) noexcept
Greater or equal operator.
friend bool operator!=(const string_view &a, const string_view &b) noexcept
Not equal operator.
string_view(const char *c)
Constructor, implicit conversion from a null terminated character array.
friend bool operator<=(const string_view &a, const string_view &b) noexcept
Smaller or equal operator.
friend bool operator<(const string_view &a, const string_view &b) noexcept
Smaller operator.
const char * data() const noexcept
Returns a pointer to the underlying character array.
string_view(const std::string &s)
Constructor, implicit conversion from a std::string.
friend std::ostream & operator<<(std::ostream &os, const string_view &v)
Stream output operator.
friend bool operator==(const string_view &a, const string_view &b) noexcept
Equality operator.
friend bool operator>(const string_view &a, const string_view &b) noexcept
Greater operator.
string_view(const char *data, size_t length) noexcept
Constructor.
int compare(const string_view &other) const noexcept
Compares two character sequences.
bool empty()
Returns whether the view is empty.
size_t size() const noexcept
Returns the number of elements in the view.
The primary namespace of the Hyper API for C++.
Definition ByteSpan.hpp:15
size_t operator()(const hyperapi::string_view &sv) const noexcept
Calculates the hash value of the given string view.