| 1 |
/* |
| 2 |
Copyright (c) 2017 Arun Muralidharan |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
of this software and associated documentation files (the "Software"), to deal |
| 6 |
in the Software without restriction, including without limitation the rights |
| 7 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
copies of the Software, and to permit persons to whom the Software is |
| 9 |
furnished to do so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in all |
| 12 |
copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef JWT_STRING_VIEW_HPP |
| 24 |
#define JWT_STRING_VIEW_HPP |
| 25 |
|
| 26 |
#if defined(__cpp_lib_string_view) |
| 27 |
|
| 28 |
#include <string_view> |
| 29 |
|
| 30 |
namespace jwt { |
| 31 |
using string_view = std::string_view; |
| 32 |
} |
| 33 |
|
| 34 |
#else // defined(__cpp_lib_string_view) |
| 35 |
|
| 36 |
#include <limits> |
| 37 |
#include <string> |
| 38 |
#include <cassert> |
| 39 |
|
| 40 |
namespace jwt { |
| 41 |
|
| 42 |
/* |
| 43 |
* Implements c++17 string_view. |
| 44 |
* Could have used boost::string_ref, but wanted to |
| 45 |
* keep boost dependency off from this library. |
| 46 |
*/ |
| 47 |
|
| 48 |
template < |
| 49 |
typename CharT, |
| 50 |
typename Traits = std::char_traits<CharT> |
| 51 |
> |
| 52 |
class basic_string_view |
| 53 |
{ |
| 54 |
public: // Member Types |
| 55 |
using traits_type = std::char_traits<CharT>; |
| 56 |
using value_type = CharT; |
| 57 |
using pointer = const CharT*; |
| 58 |
using const_pointer = const CharT*; |
| 59 |
using reference = const CharT&; |
| 60 |
using const_reference = const CharT&; |
| 61 |
using iterator = const CharT*; |
| 62 |
using const_iterator = const CharT*; |
| 63 |
using reverse_iterator = std::reverse_iterator<iterator>; |
| 64 |
using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
| 65 |
using size_type = size_t; |
| 66 |
using difference_type = std::ptrdiff_t; |
| 67 |
|
| 68 |
static constexpr size_type npos = size_type(-1); |
| 69 |
|
| 70 |
public: // 'tors |
| 71 |
/// The default constructor; |
| 72 |
basic_string_view() = default; |
| 73 |
|
| 74 |
/// Construct from string literal |
| 75 |
basic_string_view(const CharT* str) noexcept |
| 76 |
: data_(str) |
| 77 |
, len_(str ? traits_type::length(str) : 0) |
| 78 |
{ |
| 79 |
} |
| 80 |
|
| 81 |
/// Construct from CharT pointer and provided length |
| 82 |
basic_string_view(const CharT* p, size_type len) noexcept |
| 83 |
: data_(p) |
| 84 |
, len_(len) |
| 85 |
{ |
| 86 |
} |
| 87 |
|
| 88 |
/// Construct from std::string |
| 89 |
template <typename Allocator> |
| 90 |
basic_string_view( |
| 91 |
const std::basic_string<CharT, Traits, Allocator>& str) noexcept |
| 92 |
: data_(str.data()) |
| 93 |
, len_(str.length()) |
| 94 |
{ |
| 95 |
} |
| 96 |
|
| 97 |
/// Copy constructor |
| 98 |
basic_string_view(const basic_string_view&) = default; |
| 99 |
|
| 100 |
/// Assignment operator |
| 101 |
basic_string_view& operator=(const basic_string_view&) = default; |
| 102 |
|
| 103 |
/// Destructor |
| 104 |
~basic_string_view() |
| 105 |
{ |
| 106 |
data_ = nullptr; |
| 107 |
len_ = 0; |
| 108 |
} |
| 109 |
|
| 110 |
public: // Exposed APIs |
| 111 |
/// Iterator Member Functions |
| 112 |
|
| 113 |
iterator begin() const noexcept { return data_; } |
| 114 |
iterator end() const noexcept { return data_ + len_; } |
| 115 |
|
| 116 |
iterator rbegin() const noexcept { return reverse_iterator(end()); } |
| 117 |
iterator rend() const noexcept { return reverse_iterator(begin()); } |
| 118 |
|
| 119 |
const_iterator cbegin() const noexcept { return begin(); } |
| 120 |
const_iterator cend() const noexcept { return end(); } |
| 121 |
|
| 122 |
const_iterator crbegin() const noexcept { return rbegin(); } |
| 123 |
const_iterator crend() const noexcept { return rend(); } |
| 124 |
|
| 125 |
/// Capacity Member Functions |
| 126 |
|
| 127 |
size_type length() const noexcept { return len_; } |
| 128 |
size_type size() const noexcept { return len_; } |
| 129 |
|
| 130 |
size_type max_size() const noexcept |
| 131 |
{ |
| 132 |
return (npos - sizeof(size_type) - sizeof(void*)) |
| 133 |
/ sizeof(value_type) / 4; |
| 134 |
} |
| 135 |
|
| 136 |
bool empty() const noexcept { return len_ == 0; } |
| 137 |
|
| 138 |
/// Element Access Member Functions |
| 139 |
const_reference operator[](size_type idx) const noexcept |
| 140 |
{ |
| 141 |
assert(idx < len_ && "string_view subscript out of range"); |
| 142 |
return data_[idx]; |
| 143 |
} |
| 144 |
|
| 145 |
// NOTE: 'at' not supported |
| 146 |
//CharT at(size_type idx) const; |
| 147 |
|
| 148 |
const_reference front() const noexcept |
| 149 |
{ |
| 150 |
return data_[0]; |
| 151 |
} |
| 152 |
|
| 153 |
const_reference back() const noexcept |
| 154 |
{ |
| 155 |
return data_[len_ - 1]; |
| 156 |
} |
| 157 |
|
| 158 |
const_pointer data() const noexcept |
| 159 |
{ |
| 160 |
return data_; |
| 161 |
} |
| 162 |
|
| 163 |
/// Modifier Member Functions |
| 164 |
void remove_prefix(size_type n) noexcept |
| 165 |
{ |
| 166 |
assert (n < len_ && "Data would point out of bounds"); |
| 167 |
data_ += n; |
| 168 |
len_ -= n; |
| 169 |
} |
| 170 |
|
| 171 |
void remove_suffix(size_type n) noexcept |
| 172 |
{ |
| 173 |
assert (n < len_ && "Suffix length more than data length"); |
| 174 |
len_ -= n; |
| 175 |
} |
| 176 |
|
| 177 |
void swap(basic_string_view& other) |
| 178 |
{ |
| 179 |
std::swap(data_, other.data_); |
| 180 |
std::swap(len_, other.len_); |
| 181 |
} |
| 182 |
|
| 183 |
/// String Operation Member Functions |
| 184 |
|
| 185 |
template <typename Allocator> |
| 186 |
explicit operator std::basic_string<CharT, Traits, Allocator>() const |
| 187 |
{ |
| 188 |
return {data_, len_}; |
| 189 |
} |
| 190 |
|
| 191 |
// NOTE: Does not throw |
| 192 |
size_type copy(CharT* dest, size_type n, size_type pos = 0) const noexcept |
| 193 |
{ |
| 194 |
assert (pos < len_ && n < len_); |
| 195 |
size_type to_copy = std::min(n, len_ - pos); |
| 196 |
|
| 197 |
for (size_type i = 0; i < to_copy; i++) { |
| 198 |
dest[i] = data_[i + pos]; |
| 199 |
} |
| 200 |
|
| 201 |
return to_copy; |
| 202 |
} |
| 203 |
|
| 204 |
// NOTE: Does not throw |
| 205 |
basic_string_view substr(size_type pos, size_type n = npos) const noexcept |
| 206 |
{ |
| 207 |
assert (pos < len_ && "Start position should be less than length of the view"); |
| 208 |
assert (n == npos ? 1 : (n - pos) < len_ && |
| 209 |
"Substring length asked for is more than the view length"); |
| 210 |
|
| 211 |
if (n == npos) n = len_; |
| 212 |
|
| 213 |
return basic_string_view{data_ + pos, n}; |
| 214 |
} |
| 215 |
|
| 216 |
/// Comparison Member Functions |
| 217 |
int compare(const basic_string_view& other) const noexcept |
| 218 |
{ |
| 219 |
int ret = traits_type::compare(data_, other.data_, std::min(len_, other.len_)); |
| 220 |
if (ret == 0) { |
| 221 |
ret = compare_length(len_, other.len_); |
| 222 |
} |
| 223 |
return ret; |
| 224 |
} |
| 225 |
|
| 226 |
int compare(size_type pos, size_type n, basic_string_view other) const noexcept |
| 227 |
{ |
| 228 |
return substr(pos, n).compare(other); |
| 229 |
} |
| 230 |
|
| 231 |
int compare(const CharT* str) const noexcept |
| 232 |
{ |
| 233 |
return compare(basic_string_view{str}); |
| 234 |
} |
| 235 |
|
| 236 |
int compare(size_type pos, size_type n, const CharT* str) const noexcept |
| 237 |
{ |
| 238 |
return compare(pos, n, basic_string_view{str}); |
| 239 |
} |
| 240 |
|
| 241 |
int compare(size_type pos, size_type n1, const CharT* str, size_type n2) const noexcept |
| 242 |
{ |
| 243 |
return compare(pos, n1, basic_string_view{str, n2}); |
| 244 |
} |
| 245 |
|
| 246 |
/// Find operations |
| 247 |
size_type find(const CharT* str, size_type pos, size_type n) const noexcept; |
| 248 |
|
| 249 |
size_type find(const CharT ch, size_type pos) const noexcept; |
| 250 |
|
| 251 |
size_type find(basic_string_view sv, size_type pos = 0) const noexcept |
| 252 |
{ |
| 253 |
return find(sv.data(), pos, sv.length()); |
| 254 |
} |
| 255 |
|
| 256 |
size_type find(const CharT* str, size_type pos = 0) const noexcept |
| 257 |
{ |
| 258 |
return find(str, pos, traits_type::length(str)); |
| 259 |
} |
| 260 |
|
| 261 |
size_type rfind(const CharT* str, size_type pos, size_type n) const noexcept; |
| 262 |
|
| 263 |
size_type rfind(const CharT ch, size_type pos) const noexcept; |
| 264 |
|
| 265 |
size_type rfind(basic_string_view sv, size_type pos = 0) const noexcept |
| 266 |
{ |
| 267 |
return rfind(sv.data(), pos, sv.length()); |
| 268 |
} |
| 269 |
|
| 270 |
size_type rfind(const CharT* str, size_type pos = 0) const noexcept |
| 271 |
{ |
| 272 |
return rfind(str, pos, traits_type::length(str)); |
| 273 |
} |
| 274 |
|
| 275 |
size_type find_first_of(const CharT* str, size_type pos, size_type count) const noexcept; |
| 276 |
|
| 277 |
size_type find_first_of(basic_string_view str, size_type pos = 0) const noexcept |
| 278 |
{ |
| 279 |
return find_first_of(str.data(), pos, str.length()); |
| 280 |
} |
| 281 |
|
| 282 |
size_type find_first_of(CharT ch, size_type pos = 0) const noexcept |
| 283 |
{ |
| 284 |
return find(ch, pos); |
| 285 |
} |
| 286 |
|
| 287 |
size_type find_first_of(const CharT* str, size_type pos = 0) const noexcept |
| 288 |
{ |
| 289 |
return find_first_of(str, pos, traits_type::length(str)); |
| 290 |
} |
| 291 |
|
| 292 |
size_type find_last_of(const CharT* str, size_type pos, size_type count) const noexcept; |
| 293 |
|
| 294 |
size_type find_last_of(basic_string_view str, size_type pos = npos) const noexcept |
| 295 |
{ |
| 296 |
return find_last_of(str.data(), (pos == npos ? len_ - 1 : pos), str.length()); |
| 297 |
} |
| 298 |
|
| 299 |
size_type find_last_of(CharT ch, size_type pos = npos) const noexcept |
| 300 |
{ |
| 301 |
return rfind(ch, pos == npos ? len_ - 1 : pos); |
| 302 |
} |
| 303 |
|
| 304 |
size_type find_last_of(const CharT* str, size_type pos = npos) const noexcept |
| 305 |
{ |
| 306 |
return find_last_of(str, (pos == npos ? len_ - 1 : pos), traits_type::length(str)); |
| 307 |
} |
| 308 |
|
| 309 |
size_type find_first_not_of(const CharT* str, size_type pos, size_type n) const noexcept; |
| 310 |
|
| 311 |
size_type find_first_not_of(CharT ch, size_type pos) const noexcept; |
| 312 |
|
| 313 |
size_type find_first_not_of(basic_string_view str, size_type pos = 0) const noexcept |
| 314 |
{ |
| 315 |
return find_first_not_of(str.data(), pos, str.length()); |
| 316 |
} |
| 317 |
|
| 318 |
size_type find_first_not_of(const CharT* str, size_type pos = 0) const noexcept |
| 319 |
{ |
| 320 |
return find_first_not_of(str, pos, traits_type::length(str)); |
| 321 |
} |
| 322 |
|
| 323 |
size_type find_last_not_of(const CharT* str, size_type pos, size_type n) const noexcept; |
| 324 |
|
| 325 |
size_type find_last_not_of(CharT ch, size_type pos) const noexcept; |
| 326 |
|
| 327 |
size_type find_last_not_of(basic_string_view str, size_type pos = npos) const noexcept |
| 328 |
{ |
| 329 |
return find_last_not_of(str.data(), (pos == npos ? len_ - 1 : pos), str.length()); |
| 330 |
} |
| 331 |
|
| 332 |
size_type find_last_not_of(const CharT* str, size_type pos = npos) const noexcept |
| 333 |
{ |
| 334 |
return find_last_not_of(str, (pos == npos ? len_ - 1 : pos), traits_type::length(str)); |
| 335 |
} |
| 336 |
|
| 337 |
/// Comparison operators Member Functions |
| 338 |
/* |
| 339 |
friend bool operator== (basic_string_view a, basic_string_view b) noexcept; |
| 340 |
|
| 341 |
friend bool operator!= (basic_string_view a, basic_string_view b) noexcept; |
| 342 |
|
| 343 |
friend bool operator< (basic_string_view a, basic_string_view b) noexcept; |
| 344 |
|
| 345 |
friend bool operator> (basic_string_view a, basic_string_view b) noexcept; |
| 346 |
|
| 347 |
friend bool operator<= (basic_string_view a, basic_string_view b) noexcept; |
| 348 |
|
| 349 |
friend bool operator>= (basic_string_view a, basic_string_view b) noexcept; |
| 350 |
*/ |
| 351 |
|
| 352 |
private: // private implementations |
| 353 |
|
| 354 |
static constexpr int compare_length(size_type n1, size_type n2) noexcept |
| 355 |
{ |
| 356 |
return static_cast<difference_type>(n1 - n2) > std::numeric_limits<int>::max() |
| 357 |
? std::numeric_limits<int>::max() |
| 358 |
: static_cast<difference_type>(n1 - n2) < std::numeric_limits<int>::min() |
| 359 |
? std::numeric_limits<int>::min() |
| 360 |
: static_cast<int>(n1 - n2) |
| 361 |
; |
| 362 |
} |
| 363 |
|
| 364 |
private: |
| 365 |
// This is what view is basically... |
| 366 |
const char* data_ = nullptr; |
| 367 |
size_type len_ = 0; |
| 368 |
}; |
| 369 |
|
| 370 |
|
| 371 |
/// Helper typedef |
| 372 |
using string_view = basic_string_view<char>; |
| 373 |
|
| 374 |
|
| 375 |
} // END namespace jwt |
| 376 |
|
| 377 |
#include "jwt/impl/string_view.ipp" |
| 378 |
|
| 379 |
#endif // defined(__cpp_lib_string_view) |
| 380 |
|
| 381 |
#endif |