| 1 |
// Copyright 2016 Glyn Matthews. |
| 2 |
// Distributed under the Boost Software License, Version 1.0. |
| 3 |
// (See accompanying file LICENSE_1_0.txt or copy at |
| 4 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 5 |
|
| 6 |
#ifndef NETWORK_STRING_VIEW_INC |
| 7 |
#define NETWORK_STRING_VIEW_INC |
| 8 |
|
| 9 |
/** |
| 10 |
* \file |
| 11 |
* \brief Contains an implementation of C++17 string_view (n3921). |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <string> |
| 15 |
#include <iterator> |
| 16 |
#include <cassert> |
| 17 |
#include <stdexcept> |
| 18 |
#include <algorithm> |
| 19 |
|
| 20 |
namespace network { |
| 21 |
/** |
| 22 |
* \class basic_string_view string_view.hpp network/uri/string_view.hpp |
| 23 |
* \brief An implementation of C++17 string_view (n3921) |
| 24 |
*/ |
| 25 |
template <class charT, class traits = std::char_traits<charT>> |
| 26 |
class basic_string_view { |
| 27 |
public: |
| 28 |
typedef traits traits_type; |
| 29 |
typedef charT value_type; |
| 30 |
typedef charT *pointer; |
| 31 |
typedef const charT *const_pointer; |
| 32 |
typedef charT &reference; |
| 33 |
typedef const charT &const_reference; |
| 34 |
typedef const charT *const_iterator; |
| 35 |
typedef const_iterator iterator; |
| 36 |
typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 37 |
typedef const_reverse_iterator reverse_iterator; |
| 38 |
typedef std::size_t size_type; |
| 39 |
typedef std::ptrdiff_t difference_type; |
| 40 |
static constexpr size_type npos = size_type(-1); |
| 41 |
|
| 42 |
/** |
| 43 |
* \brief Constructor. |
| 44 |
*/ |
| 45 |
constexpr basic_string_view() noexcept : data_(nullptr), size_(0) {} |
| 46 |
|
| 47 |
/** |
| 48 |
* \brief Copy constructor. |
| 49 |
*/ |
| 50 |
constexpr basic_string_view(const basic_string_view &) noexcept = default; |
| 51 |
|
| 52 |
/** |
| 53 |
* \brief Assignment operator. |
| 54 |
*/ |
| 55 |
basic_string_view &operator=(const basic_string_view &) noexcept = default; |
| 56 |
|
| 57 |
/** |
| 58 |
* \brief Constructor. |
| 59 |
*/ |
| 60 |
template <class Allocator> |
| 61 |
basic_string_view( |
| 62 |
const std::basic_string<charT, traits, Allocator> &str) noexcept |
| 63 |
: data_(str.data()), size_(str.size()) {} |
| 64 |
|
| 65 |
/** |
| 66 |
* \brief Constructor. |
| 67 |
*/ |
| 68 |
constexpr basic_string_view(const charT *str) |
| 69 |
: data_(str), size_(traits::length(str)) {} |
| 70 |
|
| 71 |
/** |
| 72 |
* \brief Constructor. |
| 73 |
*/ |
| 74 |
constexpr basic_string_view(const charT *str, size_type len) |
| 75 |
: data_(str), size_(len) {} |
| 76 |
|
| 77 |
constexpr const_iterator begin() const noexcept { return data_; } |
| 78 |
|
| 79 |
constexpr const_iterator end() const noexcept { return data_ + size_; } |
| 80 |
|
| 81 |
constexpr const_iterator cbegin() const noexcept { return begin(); } |
| 82 |
|
| 83 |
constexpr const_iterator cend() const noexcept { return end(); } |
| 84 |
|
| 85 |
const_reverse_iterator rbegin() const noexcept { |
| 86 |
return const_reverse_iterator(end()); |
| 87 |
} |
| 88 |
const_reverse_iterator rend() const noexcept { |
| 89 |
return const_reverse_iterator(begin()); |
| 90 |
} |
| 91 |
const_reverse_iterator crbegin() const noexcept { return rbegin(); } |
| 92 |
|
| 93 |
const_reverse_iterator crend() const noexcept { return rend(); } |
| 94 |
|
| 95 |
constexpr size_type size() const noexcept { return size_; } |
| 96 |
|
| 97 |
constexpr size_type length() const noexcept { return size_; } |
| 98 |
|
| 99 |
constexpr size_type max_size() const noexcept { return size_; } |
| 100 |
|
| 101 |
constexpr bool empty() const noexcept { return size_ == 0; } |
| 102 |
|
| 103 |
constexpr const_reference operator[](size_type pos) const { |
| 104 |
return data_[pos]; |
| 105 |
} |
| 106 |
|
| 107 |
const_reference at(size_type pos) const { |
| 108 |
if (pos >= size_) { |
| 109 |
throw std::out_of_range("Index out of range."); |
| 110 |
} |
| 111 |
return data_[pos]; |
| 112 |
} |
| 113 |
|
| 114 |
const_reference front() const { return *begin(); } |
| 115 |
|
| 116 |
const_reference back() const { |
| 117 |
auto last = (end()) - 1; |
| 118 |
return *last; |
| 119 |
} |
| 120 |
|
| 121 |
constexpr const_pointer data() const noexcept { return data_; } |
| 122 |
|
| 123 |
void clear() noexcept { |
| 124 |
data_ = nullptr; |
| 125 |
size_ = 0; |
| 126 |
} |
| 127 |
|
| 128 |
void remove_prefix(size_type n) { |
| 129 |
data_ += n; |
| 130 |
size_ -= n; |
| 131 |
} |
| 132 |
|
| 133 |
void remove_suffix(size_type n) { size_ -= n; } |
| 134 |
|
| 135 |
void swap(basic_string_view &s) noexcept { |
| 136 |
std::swap(data_, s.data_); |
| 137 |
std::swap(size_, s.size_); |
| 138 |
} |
| 139 |
|
| 140 |
template <class Allocator> |
| 141 |
explicit operator std::basic_string<charT, traits, Allocator>() const { |
| 142 |
return to_string<charT, traits, Allocator>(); |
| 143 |
} |
| 144 |
|
| 145 |
template <class Allocator = std::allocator<charT>> |
| 146 |
std::basic_string<charT, traits, Allocator> to_string( |
| 147 |
const Allocator &a = Allocator()) const { |
| 148 |
return std::basic_string<charT, traits, Allocator>(begin(), end(), a); |
| 149 |
} |
| 150 |
|
| 151 |
size_type copy(charT *s, size_type n, size_type pos = 0) const { |
| 152 |
size_type rlen = std::min(n, size() - pos); |
| 153 |
std::copy_n(begin() + pos, rlen, s); |
| 154 |
return rlen; |
| 155 |
} |
| 156 |
|
| 157 |
constexpr basic_string_view substr(size_type pos = 0, |
| 158 |
size_type n = npos) const { |
| 159 |
return basic_string_view(data() + pos, std::min(n, size() - pos)); |
| 160 |
} |
| 161 |
|
| 162 |
constexpr int compare(basic_string_view s) const noexcept { |
| 163 |
return size() == s.size() |
| 164 |
? traits::compare(data(), s.data(), size()) |
| 165 |
: (size() < s.size() |
| 166 |
? (traits::compare(data(), s.data(), size()) > 0 ? 1 : -1) |
| 167 |
: (traits::compare(data(), s.data(), size()) < 0 ? -1 |
| 168 |
: 1)); |
| 169 |
} |
| 170 |
|
| 171 |
constexpr int compare(size_type pos1, size_type n1, |
| 172 |
basic_string_view s) const { |
| 173 |
return substr(pos1, n1).compare(s); |
| 174 |
} |
| 175 |
|
| 176 |
constexpr int compare(size_type pos1, size_type n1, basic_string_view s, |
| 177 |
size_type pos2, size_type n2) const { |
| 178 |
return substr(pos1, n1).compare(s.substr(pos2, n2)); |
| 179 |
} |
| 180 |
|
| 181 |
constexpr int compare(const charT *s) const { |
| 182 |
return compare(basic_string_view(s)); |
| 183 |
} |
| 184 |
|
| 185 |
constexpr int compare(size_type pos1, size_type n1, const charT *s) const { |
| 186 |
return substr(pos1, n1).compare(basic_string_view(s)); |
| 187 |
} |
| 188 |
|
| 189 |
constexpr int compare(size_type pos1, size_type n1, const charT *s, |
| 190 |
size_type n2) const { |
| 191 |
return substr(pos1, n1).compare(basic_string_view(s, n2)); |
| 192 |
} |
| 193 |
|
| 194 |
private: |
| 195 |
const_pointer data_; |
| 196 |
size_type size_; |
| 197 |
}; |
| 198 |
|
| 199 |
/** |
| 200 |
* \brief Equality operator. |
| 201 |
* \returns <tt>lhs.compare(rhs) == 0</tt>. |
| 202 |
*/ |
| 203 |
template <class charT, class traits> |
| 204 |
constexpr bool operator==(basic_string_view<charT, traits> lhs, |
| 205 |
basic_string_view<charT, traits> rhs) noexcept { |
| 206 |
return lhs.compare(rhs) == 0; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* \brief Inequality operator. |
| 211 |
* \returns <tt>!(lhs == rhs)</tt>. |
| 212 |
*/ |
| 213 |
template <class charT, class traits> |
| 214 |
constexpr bool operator!=(basic_string_view<charT, traits> lhs, |
| 215 |
basic_string_view<charT, traits> rhs) noexcept { |
| 216 |
return !(lhs == rhs); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* \brief Comparison operator. |
| 221 |
* \returns <tt>lhs.compare(rhs) < 0</tt>. |
| 222 |
*/ |
| 223 |
template <class charT, class traits> |
| 224 |
constexpr bool operator<(basic_string_view<charT, traits> lhs, |
| 225 |
basic_string_view<charT, traits> rhs) noexcept { |
| 226 |
return lhs.compare(rhs) < 0; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* \brief Comparison operator. |
| 231 |
* \returns <tt>rhs < lhs</tt>. |
| 232 |
*/ |
| 233 |
template <class charT, class traits> |
| 234 |
constexpr bool operator>(basic_string_view<charT, traits> lhs, |
| 235 |
basic_string_view<charT, traits> rhs) noexcept { |
| 236 |
return rhs < lhs; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* \brief Comparison operator. |
| 241 |
* \returns <tt>!(lhs > rhs)</tt>. |
| 242 |
*/ |
| 243 |
template <class charT, class traits> |
| 244 |
constexpr bool operator<=(basic_string_view<charT, traits> lhs, |
| 245 |
basic_string_view<charT, traits> rhs) noexcept { |
| 246 |
return !(lhs > rhs); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* \brief Comparison operator. |
| 251 |
* \returns <tt>!(lhs < rhs)</tt>. |
| 252 |
*/ |
| 253 |
template <class charT, class traits> |
| 254 |
constexpr bool operator>=(basic_string_view<charT, traits> lhs, |
| 255 |
basic_string_view<charT, traits> rhs) noexcept { |
| 256 |
return !(lhs < rhs); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* \brief Output stream operator. |
| 261 |
*/ |
| 262 |
template <class charT, class traits> |
| 263 |
std::basic_ostream<charT, traits> &operator<<( |
| 264 |
std::basic_ostream<charT, traits> &os, |
| 265 |
basic_string_view<charT, traits> str) { |
| 266 |
return os << str.to_string(); |
| 267 |
} |
| 268 |
|
| 269 |
typedef basic_string_view<char> string_view; |
| 270 |
} // namespace network |
| 271 |
|
| 272 |
#endif // NETWORK_STRING_VIEW_INC |