| 1 |
// -*-c++-*- |
| 2 |
// vim: set ft=cpp: |
| 3 |
|
| 4 |
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 5 |
file LICENSE.rst or https://cmake.org/licensing for details. */ |
| 6 |
#pragma once |
| 7 |
|
| 8 |
#if __cplusplus >= 201703L || defined(_MSVC_LANG) && _MSVC_LANG >= 201703L |
| 9 |
# define CMake_HAVE_CXX_STRING_VIEW |
| 10 |
#endif |
| 11 |
|
| 12 |
#include <cm/bits/container_helpers.hxx> // IWYU pragma: export |
| 13 |
|
| 14 |
#ifdef CMake_HAVE_CXX_STRING_VIEW |
| 15 |
# include <string_view> // IWYU pragma: export |
| 16 |
namespace cm { |
| 17 |
using std::string_view; |
| 18 |
} |
| 19 |
#else |
| 20 |
# include <cstddef> |
| 21 |
# include <functional> |
| 22 |
# include <iosfwd> |
| 23 |
# include <iterator> |
| 24 |
# include <string> |
| 25 |
|
| 26 |
namespace cm { |
| 27 |
|
| 28 |
class string_view |
| 29 |
{ |
| 30 |
public: |
| 31 |
using traits_type = std::string::traits_type; |
| 32 |
using value_type = char; |
| 33 |
using pointer = char*; |
| 34 |
using const_pointer = char const*; |
| 35 |
using reference = char&; |
| 36 |
using const_reference = char const&; |
| 37 |
using const_iterator = char const*; |
| 38 |
using iterator = const_iterator; |
| 39 |
using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
| 40 |
using reverse_iterator = const_reverse_iterator; |
| 41 |
using size_type = std::string::size_type; |
| 42 |
using difference_type = std::string::difference_type; |
| 43 |
|
| 44 |
static size_type const npos = static_cast<size_type>(-1); |
| 45 |
|
| 46 |
string_view() noexcept = default; |
| 47 |
string_view(string_view const&) noexcept = default; |
| 48 |
|
| 49 |
string_view(char const* s, size_t count) noexcept |
| 50 |
: data_(s) |
| 51 |
, size_(count) |
| 52 |
{ |
| 53 |
} |
| 54 |
|
| 55 |
string_view(char const* s) noexcept |
| 56 |
: data_(s) |
| 57 |
, size_(traits_type::length(s)) |
| 58 |
{ |
| 59 |
} |
| 60 |
|
| 61 |
// C++17 does not define this constructor. Instead it defines |
| 62 |
// a conversion operator on std::string to create a string_view. |
| 63 |
// Since this implementation is used in C++11, std::string does |
| 64 |
// not have that conversion. |
| 65 |
string_view(std::string const& s) noexcept |
| 66 |
: data_(s.data()) |
| 67 |
, size_(s.size()) |
| 68 |
{ |
| 69 |
} |
| 70 |
|
| 71 |
// C++17 does not define this conversion. Instead it defines |
| 72 |
// a constructor on std::string that can take a string_view. |
| 73 |
// Since this implementation is used in C++11, std::string does |
| 74 |
// not have that constructor. |
| 75 |
explicit operator std::string() const { return std::string(data_, size_); } |
| 76 |
|
| 77 |
string_view& operator=(string_view const&) = default; |
| 78 |
|
| 79 |
const_iterator begin() const noexcept { return data_; } |
| 80 |
const_iterator end() const noexcept { return data_ + size_; } |
| 81 |
const_iterator cbegin() const noexcept { return begin(); } |
| 82 |
const_iterator cend() const noexcept { return end(); } |
| 83 |
|
| 84 |
const_reverse_iterator rbegin() const noexcept |
| 85 |
{ |
| 86 |
return const_reverse_iterator(end()); |
| 87 |
} |
| 88 |
const_reverse_iterator rend() const noexcept |
| 89 |
{ |
| 90 |
return const_reverse_iterator(begin()); |
| 91 |
} |
| 92 |
const_reverse_iterator crbegin() const noexcept { return rbegin(); } |
| 93 |
const_reverse_iterator crend() const noexcept { return rend(); } |
| 94 |
|
| 95 |
const_reference operator[](size_type pos) const noexcept |
| 96 |
{ |
| 97 |
return data_[pos]; |
| 98 |
} |
| 99 |
const_reference at(size_type pos) const; |
| 100 |
const_reference front() const noexcept { return data_[0]; } |
| 101 |
const_reference back() const noexcept { return data_[size_ - 1]; } |
| 102 |
const_pointer data() const noexcept { return data_; } |
| 103 |
|
| 104 |
size_type size() const noexcept { return size_; } |
| 105 |
size_type length() const noexcept { return size_; } |
| 106 |
size_type max_size() const noexcept { return npos - 1; } |
| 107 |
bool empty() const noexcept { return size_ == 0; } |
| 108 |
|
| 109 |
void remove_prefix(size_type n) noexcept |
| 110 |
{ |
| 111 |
data_ += n; |
| 112 |
size_ -= n; |
| 113 |
} |
| 114 |
void remove_suffix(size_type n) noexcept { size_ -= n; } |
| 115 |
void swap(string_view& v) noexcept |
| 116 |
{ |
| 117 |
string_view tmp = v; |
| 118 |
v = *this; |
| 119 |
*this = tmp; |
| 120 |
} |
| 121 |
|
| 122 |
size_type copy(char* dest, size_type count, size_type pos = 0) const; |
| 123 |
string_view substr(size_type pos = 0, size_type count = npos) const; |
| 124 |
|
| 125 |
int compare(string_view v) const noexcept; |
| 126 |
int compare(size_type pos1, size_type count1, string_view v) const; |
| 127 |
int compare(size_type pos1, size_type count1, string_view v, size_type pos2, |
| 128 |
size_type count2) const; |
| 129 |
int compare(char const* s) const; |
| 130 |
int compare(size_type pos1, size_type count1, char const* s) const; |
| 131 |
int compare(size_type pos1, size_type count1, char const* s, |
| 132 |
size_type count2) const; |
| 133 |
|
| 134 |
size_type find(string_view v, size_type pos = 0) const noexcept; |
| 135 |
size_type find(char c, size_type pos = 0) const noexcept; |
| 136 |
size_type find(char const* s, size_type pos, size_type count) const; |
| 137 |
size_type find(char const* s, size_type pos = 0) const; |
| 138 |
|
| 139 |
size_type rfind(string_view v, size_type pos = npos) const noexcept; |
| 140 |
size_type rfind(char c, size_type pos = npos) const noexcept; |
| 141 |
size_type rfind(char const* s, size_type pos, size_type count) const; |
| 142 |
size_type rfind(char const* s, size_type pos = npos) const; |
| 143 |
|
| 144 |
size_type find_first_of(string_view v, size_type pos = 0) const noexcept; |
| 145 |
size_type find_first_of(char c, size_type pos = 0) const noexcept; |
| 146 |
size_type find_first_of(char const* s, size_type pos, size_type count) const; |
| 147 |
size_type find_first_of(char const* s, size_type pos = 0) const; |
| 148 |
|
| 149 |
size_type find_last_of(string_view v, size_type pos = npos) const noexcept; |
| 150 |
size_type find_last_of(char c, size_type pos = npos) const noexcept; |
| 151 |
size_type find_last_of(char const* s, size_type pos, size_type count) const; |
| 152 |
size_type find_last_of(char const* s, size_type pos = npos) const; |
| 153 |
|
| 154 |
size_type find_first_not_of(string_view v, size_type pos = 0) const noexcept; |
| 155 |
size_type find_first_not_of(char c, size_type pos = 0) const noexcept; |
| 156 |
size_type find_first_not_of(char const* s, size_type pos, |
| 157 |
size_type count) const; |
| 158 |
size_type find_first_not_of(char const* s, size_type pos = 0) const; |
| 159 |
|
| 160 |
size_type find_last_not_of(string_view v, |
| 161 |
size_type pos = npos) const noexcept; |
| 162 |
size_type find_last_not_of(char c, size_type pos = npos) const noexcept; |
| 163 |
size_type find_last_not_of(char const* s, size_type pos, |
| 164 |
size_type count) const; |
| 165 |
size_type find_last_not_of(char const* s, size_type pos = npos) const; |
| 166 |
|
| 167 |
private: |
| 168 |
char const* data_ = nullptr; |
| 169 |
size_type size_ = 0; |
| 170 |
}; |
| 171 |
|
| 172 |
std::ostream& operator<<(std::ostream& o, string_view v); |
| 173 |
|
| 174 |
std::string& operator+=(std::string& s, string_view v); |
| 175 |
|
| 176 |
inline bool operator==(string_view l, string_view r) noexcept |
| 177 |
{ |
| 178 |
return l.compare(r) == 0; |
| 179 |
} |
| 180 |
|
| 181 |
inline bool operator!=(string_view l, string_view r) noexcept |
| 182 |
{ |
| 183 |
return l.compare(r) != 0; |
| 184 |
} |
| 185 |
|
| 186 |
inline bool operator<(string_view l, string_view r) noexcept |
| 187 |
{ |
| 188 |
return l.compare(r) < 0; |
| 189 |
} |
| 190 |
|
| 191 |
inline bool operator<=(string_view l, string_view r) noexcept |
| 192 |
{ |
| 193 |
return l.compare(r) <= 0; |
| 194 |
} |
| 195 |
|
| 196 |
inline bool operator>(string_view l, string_view r) noexcept |
| 197 |
{ |
| 198 |
return l.compare(r) > 0; |
| 199 |
} |
| 200 |
|
| 201 |
inline bool operator>=(string_view l, string_view r) noexcept |
| 202 |
{ |
| 203 |
return l.compare(r) >= 0; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
namespace std { |
| 208 |
|
| 209 |
template <> |
| 210 |
struct hash<cm::string_view> |
| 211 |
{ |
| 212 |
using argument_type = cm::string_view; |
| 213 |
using result_type = size_t; |
| 214 |
result_type operator()(argument_type const& s) const noexcept; |
| 215 |
}; |
| 216 |
} |
| 217 |
|
| 218 |
#endif |