1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include <cstddef>
#include <cstring>
#include <iosfwd>
#include <iterator>
#include <limits>
#include <type_traits>
#include "caf/detail/comparable.hpp"
namespace caf {
namespace detail {
// Catches `std::string`, `std::string_view` and all classes mimicing those,
// but not `std::vector<char>` or other buffers.
template <class T>
struct is_string_like {
// SFINAE checks.
template <class U>
static bool sfinae(
const U* x,
// check if `(*x)[0]` returns `const char&`
typename std::enable_if<
std::is_same<
const char&,
decltype((*x)[0])
>::value
>::type* = nullptr,
// check if `x->size()` returns an integer
typename std::enable_if<
std::is_integral<
decltype(x->size())
>::value
>::type* = nullptr,
// check if `x->find('?', 0)` is well-formed and returns an integer
// (distinguishes vectors from strings)
typename std::enable_if<
std::is_integral<
decltype(x->find('?', 0))
>::value
>::type* = nullptr);
// SFINAE fallback.
static void sfinae(void*);
// Result of SFINAE test.
using result_type =
decltype(sfinae(static_cast<typename std::decay<T>::type*>(nullptr)));
// Trait result.
static constexpr bool value = std::is_same<bool, result_type>::value;
};
} // namespace detail
/// Drop-in replacement for C++17 std::string_view.
class string_view : detail::comparable<string_view> {
public:
// -- member types -----------------------------------------------------------
using value_type = char;
using pointer = value_type*;
using const_pointer = const value_type*;
using reference = value_type&;
using const_reference = const value_type&;
using const_iterator = const_pointer;
using iterator = const_iterator;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using reverse_iterator = const_reverse_iterator;
using size_type = size_t;
using difference_type = ptrdiff_t;
// -- constants --------------------------------------------------------------
static constexpr size_type npos = std::numeric_limits<size_type>::max();
// -- constructors, destructors, and assignment operators --------------------
constexpr string_view() noexcept : data_(nullptr), size_(0) {
// nop
}
constexpr string_view(const char* cstr, size_t length) noexcept
: data_(cstr),
size_(length) {
// nop
}
#ifdef CAF_GCC
constexpr string_view(const char* cstr) noexcept
: data_(cstr),
size_(strlen(cstr)) {
// nop
}
#else
template <size_t N>
constexpr string_view(const char (&cstr)[N]) noexcept
: data_(cstr),
size_(N - 1) {
// nop
}
#endif
constexpr string_view(const string_view&) noexcept = default;
template <
class T,
class = typename std::enable_if<detail::is_string_like<T>::value>::type
>
string_view(const T& str) noexcept {
auto len = str.size();
if (len == 0) {
data_ = nullptr;
size_ = 0;
} else {
data_ = &(str[0]);
size_ = str.size();
}
}
string_view& operator=(const string_view&) noexcept = default;
// -- capacity ---------------------------------------------------------------
constexpr size_type size() const noexcept {
return size_;
}
constexpr size_type length() const noexcept {
return size_;
}
constexpr size_type max_size() const noexcept {
return std::numeric_limits<size_type>::max();
}
constexpr bool empty() const noexcept {
return size_ == 0;
}
// -- iterator access --------------------------------------------------------
constexpr const_iterator begin() const noexcept {
return data_;
}
constexpr const_iterator end() const noexcept {
return data_ + size_;
}
constexpr const_iterator cbegin() const noexcept {
return begin();
}
constexpr const_iterator cend() const noexcept {
return end();
}
const_reverse_iterator rbegin() const noexcept;
const_reverse_iterator rend() const noexcept;
const_reverse_iterator crbegin() const noexcept;
const_reverse_iterator crend() const noexcept;
// -- element access ---------------------------------------------------------
constexpr const_reference operator[](size_type pos) const {
return data_[pos];
}
const_reference at(size_type pos) const;
constexpr const_reference front() const {
return *data_;
}
constexpr const_reference back() const {
return data_[size_ - 1];
}
constexpr const_pointer data() const noexcept {
return data_;
}
// -- modifiers --------------------------------------------------------------
void remove_prefix(size_type n);
void remove_suffix(size_type n);
void assign(const_pointer data, size_type len);
// -- algortihms -------------------------------------------------------------
size_type copy(pointer dest, size_type n, size_type pos = 0) const;
string_view substr(size_type pos = 0, size_type n = npos) const noexcept;
int compare(string_view s) const noexcept;
int compare(size_type pos1, size_type n1, string_view str) const noexcept;
int compare(size_type pos1, size_type n1, string_view str,
size_type pos2, size_type n2) const noexcept;
int compare(const_pointer str) const noexcept;
int compare(size_type pos, size_type n, const_pointer str) const noexcept;
int compare(size_type pos1, size_type n1,
const_pointer s, size_type n2) const noexcept;
size_type find(string_view str, size_type pos = 0) const noexcept;
size_type find(value_type ch, size_type pos = 0) const noexcept;
size_type find(const_pointer str, size_type pos, size_type n) const noexcept;
size_type find(const_pointer str, size_type pos = 0) const noexcept;
size_type rfind(string_view str, size_type pos = npos) const noexcept;
size_type rfind(value_type ch, size_type pos = npos) const noexcept;
size_type rfind(const_pointer str, size_type pos, size_type n) const noexcept;
size_type rfind(const_pointer str, size_type pos = npos) const noexcept;
size_type find_first_of(string_view str, size_type pos = 0) const noexcept;
size_type find_first_of(value_type ch, size_type pos = 0) const noexcept;
size_type find_first_of(const_pointer str, size_type pos,
size_type n) const noexcept;
size_type find_first_of(const_pointer str, size_type pos = 0) const noexcept;
size_type find_last_of(string_view str, size_type pos = npos) const noexcept;
size_type find_last_of(value_type ch, size_type pos = npos) const noexcept;
size_type find_last_of(const_pointer str, size_type pos,
size_type n) const noexcept;
size_type find_last_of(const_pointer str,
size_type pos = npos) const noexcept;
size_type find_first_not_of(string_view str,
size_type pos = 0) const noexcept;
size_type find_first_not_of(value_type ch, size_type pos = 0) const noexcept;
size_type find_first_not_of(const_pointer str, size_type pos,
size_type n) const noexcept;
size_type find_first_not_of(const_pointer str,
size_type pos = 0) const noexcept;
size_type find_last_not_of(string_view str,
size_type pos = npos) const noexcept;
size_type find_last_not_of(value_type ch,
size_type pos = npos) const noexcept;
size_type find_last_not_of(const_pointer str, size_type pos,
size_type n) const noexcept;
size_type find_last_not_of(const_pointer str,
size_type pos = npos) const noexcept;
private:
const char* data_;
size_t size_;
};
} // namespace caf
namespace std {
std::ostream& operator<<(std::ostream& out, caf::string_view);
} // namespace std
|