| 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_IPP |
| 24 |
#define JWT_STRING_VIEW_IPP |
| 25 |
|
| 26 |
namespace jwt { |
| 27 |
|
| 28 |
template <typename CharT, typename Traits> |
| 29 |
auto basic_string_view<CharT, Traits>::find( |
| 30 |
const CharT* str, |
| 31 |
size_type pos, |
| 32 |
size_type n) const noexcept -> size_type |
| 33 |
{ |
| 34 |
assert (str); |
| 35 |
assert (n < (len_ - pos) && "Comparison size out of bounds"); |
| 36 |
|
| 37 |
if (n == 0) { |
| 38 |
return pos <= len_ ? pos : npos; |
| 39 |
} |
| 40 |
if (n <= len_) { |
| 41 |
for (; pos <= (len_ - n); ++pos) { |
| 42 |
if (traits_type::eq(data_[pos], str[0]) && |
| 43 |
traits_type::compare(data_ + pos + 1, str + 1, n - 1) == 0) { |
| 44 |
return pos; |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return npos; |
| 50 |
} |
| 51 |
|
| 52 |
template <typename CharT, typename Traits> |
| 53 |
auto basic_string_view<CharT, Traits>::rfind( |
| 54 |
const CharT* str, |
| 55 |
size_type pos, |
| 56 |
size_type n) const noexcept -> size_type |
| 57 |
{ |
| 58 |
assert (str); |
| 59 |
assert (pos < len_ && "Position out of bounds"); |
| 60 |
|
| 61 |
if (n <= len_) { |
| 62 |
pos = std::min(len_ - n, pos); |
| 63 |
do { |
| 64 |
if (traits_type::eq(data_[pos], str[0]) && |
| 65 |
traits_type::compare(data_ + pos + 1, str + 1, n - 1) == 0) { |
| 66 |
return pos; |
| 67 |
} |
| 68 |
} while (pos-- != 0); |
| 69 |
} |
| 70 |
|
| 71 |
return npos; |
| 72 |
} |
| 73 |
|
| 74 |
template <typename CharT, typename Traits> |
| 75 |
auto basic_string_view<CharT, Traits>::find( |
| 76 |
const CharT ch, |
| 77 |
size_type pos) const noexcept -> size_type |
| 78 |
{ |
| 79 |
if (pos < len_) { |
| 80 |
for (size_type i = pos; i < len_; ++i) { |
| 81 |
if (traits_type::eq(data_[i], ch)) return i; |
| 82 |
} |
| 83 |
} |
| 84 |
return npos; |
| 85 |
} |
| 86 |
|
| 87 |
template <typename CharT, typename Traits> |
| 88 |
auto basic_string_view<CharT, Traits>::rfind( |
| 89 |
const CharT ch, |
| 90 |
size_type pos) const noexcept -> size_type |
| 91 |
{ |
| 92 |
if (pos < len_) { |
| 93 |
do { |
| 94 |
if (traits_type::eq(data_[pos], ch)) { |
| 95 |
return pos; |
| 96 |
} |
| 97 |
} while (pos-- != 0); |
| 98 |
} |
| 99 |
|
| 100 |
return npos; |
| 101 |
} |
| 102 |
|
| 103 |
template <typename CharT, typename Traits> |
| 104 |
auto basic_string_view<CharT, Traits>::find_first_of( |
| 105 |
const CharT* str, |
| 106 |
size_type pos, |
| 107 |
size_type count) const noexcept -> size_type |
| 108 |
{ |
| 109 |
assert (str); |
| 110 |
|
| 111 |
for (size_type i = pos; i < len_; ++i) { |
| 112 |
auto p = traits_type::find(str, count, data_[i]); |
| 113 |
if (p) { |
| 114 |
return i; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return npos; |
| 119 |
} |
| 120 |
|
| 121 |
template <typename CharT, typename Traits> |
| 122 |
auto basic_string_view<CharT, Traits>::find_last_of( |
| 123 |
const CharT* str, |
| 124 |
size_type pos, |
| 125 |
size_type count) const noexcept -> size_type |
| 126 |
{ |
| 127 |
assert (str); |
| 128 |
assert (pos < len_ && "Position must be within the bounds of the view"); |
| 129 |
size_type siz = len_; |
| 130 |
|
| 131 |
if (siz && count) { |
| 132 |
siz = std::min(pos, siz); |
| 133 |
|
| 134 |
do { |
| 135 |
auto p = traits_type::find(str, count, data_[siz]); |
| 136 |
if (p) { |
| 137 |
return siz; |
| 138 |
} |
| 139 |
} while (siz-- != 0); |
| 140 |
} |
| 141 |
|
| 142 |
return npos; |
| 143 |
} |
| 144 |
|
| 145 |
template <typename CharT, typename Traits> |
| 146 |
auto basic_string_view<CharT, Traits>::find_first_not_of( |
| 147 |
const CharT* str, |
| 148 |
size_type pos, |
| 149 |
size_type n) const noexcept -> size_type |
| 150 |
{ |
| 151 |
assert (str); |
| 152 |
assert (pos < len_&& "Position must be within the bounds of the view"); |
| 153 |
|
| 154 |
for (size_type i = pos; i < len_; ++i) |
| 155 |
{ |
| 156 |
auto p = traits_type::find(str, n, data_[i]); |
| 157 |
if (!p) return i; |
| 158 |
} |
| 159 |
|
| 160 |
return npos; |
| 161 |
} |
| 162 |
|
| 163 |
template <typename CharT, typename Traits> |
| 164 |
auto basic_string_view<CharT, Traits>::find_last_not_of( |
| 165 |
const CharT* str, |
| 166 |
size_type pos, |
| 167 |
size_type n) const noexcept -> size_type |
| 168 |
{ |
| 169 |
assert (str); |
| 170 |
assert (pos < len_ && "Position must be within the bounds of the view"); |
| 171 |
|
| 172 |
do { |
| 173 |
for (size_type i = 0; i < n; ++i) { |
| 174 |
if (!traits_type::eq(data_[pos], str[i])) return pos; |
| 175 |
} |
| 176 |
} while (pos-- != 0); |
| 177 |
|
| 178 |
return npos; |
| 179 |
} |
| 180 |
|
| 181 |
template <typename CharT, typename Traits> |
| 182 |
auto basic_string_view<CharT, Traits>::find_first_not_of( |
| 183 |
CharT ch, |
| 184 |
size_type pos) const noexcept -> size_type |
| 185 |
{ |
| 186 |
assert (pos < len_&& "Position must be within the bounds of the view"); |
| 187 |
|
| 188 |
for (size_type i = pos; i < len_; ++i) { |
| 189 |
if (!traits_type::eq(data_[i], ch)) return i; |
| 190 |
} |
| 191 |
|
| 192 |
return npos; |
| 193 |
} |
| 194 |
|
| 195 |
template <typename CharT, typename Traits> |
| 196 |
auto basic_string_view<CharT, Traits>::find_last_not_of( |
| 197 |
CharT ch, |
| 198 |
size_type pos) const noexcept -> size_type |
| 199 |
{ |
| 200 |
assert (pos < len_ && "Position must be within the bounds of the view"); |
| 201 |
|
| 202 |
do { |
| 203 |
if (!traits_type::eq(data_[pos], ch)) return pos; |
| 204 |
} while (pos-- != 0); |
| 205 |
|
| 206 |
return npos; |
| 207 |
} |
| 208 |
|
| 209 |
// Comparison Operators |
| 210 |
|
| 211 |
template <typename CharT, typename Traits> |
| 212 |
bool operator== (basic_string_view<CharT, Traits> a, |
| 213 |
basic_string_view<CharT, Traits> b) noexcept |
| 214 |
{ |
| 215 |
if (a.length() != b.length()) return false; |
| 216 |
using traits_type = typename basic_string_view<CharT, Traits>::traits_type; |
| 217 |
using size_type = typename basic_string_view<CharT, Traits>::size_type; |
| 218 |
|
| 219 |
for (size_type i = 0; i < a.length(); ++i) { |
| 220 |
if (!traits_type::eq(a[i], b[i])) return false; |
| 221 |
} |
| 222 |
|
| 223 |
return true; |
| 224 |
} |
| 225 |
|
| 226 |
template <typename CharT, typename Traits> |
| 227 |
bool operator!= (basic_string_view<CharT, Traits> a, |
| 228 |
basic_string_view<CharT, Traits> b) noexcept |
| 229 |
{ |
| 230 |
return !( a == b ); |
| 231 |
} |
| 232 |
|
| 233 |
template <typename CharT, typename Traits> |
| 234 |
bool operator< (basic_string_view<CharT, Traits> a, |
| 235 |
basic_string_view<CharT, Traits> b) noexcept |
| 236 |
{ |
| 237 |
return a.compare(b) < 0; |
| 238 |
} |
| 239 |
|
| 240 |
template <typename CharT, typename Traits> |
| 241 |
bool operator> (basic_string_view<CharT, Traits> a, |
| 242 |
basic_string_view<CharT, Traits> b) noexcept |
| 243 |
{ |
| 244 |
return a.compare(b) > 0; |
| 245 |
} |
| 246 |
|
| 247 |
template <typename CharT, typename Traits> |
| 248 |
bool operator<= (basic_string_view<CharT, Traits> a, |
| 249 |
basic_string_view<CharT, Traits> b) noexcept |
| 250 |
{ |
| 251 |
return a.compare(b) <= 0; |
| 252 |
} |
| 253 |
|
| 254 |
template <typename CharT, typename Traits> |
| 255 |
bool operator>= (basic_string_view<CharT, Traits> a, |
| 256 |
basic_string_view<CharT, Traits> b) noexcept |
| 257 |
{ |
| 258 |
return a.compare(b) >= 0; |
| 259 |
} |
| 260 |
|
| 261 |
template <typename CharT, typename Traits> |
| 262 |
std::ostream& operator<< (std::ostream& os, basic_string_view<CharT, Traits> sv) |
| 263 |
{ |
| 264 |
os.write(sv.data(), sv.length()); |
| 265 |
return os; |
| 266 |
} |
| 267 |
|
| 268 |
namespace { |
| 269 |
|
| 270 |
/* |
| 271 |
* Copy of gcc implementation of murmurhash |
| 272 |
* hash_bytes.cc |
| 273 |
*/ |
| 274 |
|
| 275 |
inline size_t |
| 276 |
unaligned_load(const char* p) noexcept |
| 277 |
{ |
| 278 |
std::size_t result; |
| 279 |
std::memcpy(&result, p, sizeof(result)); |
| 280 |
return result; |
| 281 |
} |
| 282 |
|
| 283 |
inline size_t |
| 284 |
hash_bytes(const void* ptr, size_t len, size_t seed) noexcept |
| 285 |
{ |
| 286 |
const size_t m = 0x5bd1e995; |
| 287 |
size_t hash = seed ^ len; |
| 288 |
const char* buf = static_cast<const char*>(ptr); |
| 289 |
|
| 290 |
// Mix 4 bytes at a time into the hash. |
| 291 |
while(len >= 4) |
| 292 |
{ |
| 293 |
size_t k = unaligned_load(buf); |
| 294 |
k *= m; |
| 295 |
k ^= k >> 24; |
| 296 |
k *= m; |
| 297 |
hash *= m; |
| 298 |
hash ^= k; |
| 299 |
buf += 4; |
| 300 |
len -= 4; |
| 301 |
} |
| 302 |
|
| 303 |
// Handle the last few bytes of the input array. |
| 304 |
switch(len) |
| 305 |
{ |
| 306 |
case 3: |
| 307 |
hash ^= static_cast<unsigned char>(buf[2]) << 16; |
| 308 |
//FALLTHROUGH |
| 309 |
case 2: |
| 310 |
hash ^= static_cast<unsigned char>(buf[1]) << 8; |
| 311 |
//FALLTHROUGH |
| 312 |
case 1: |
| 313 |
hash ^= static_cast<unsigned char>(buf[0]); |
| 314 |
hash *= m; |
| 315 |
}; |
| 316 |
|
| 317 |
// Do a few final mixes of the hash. |
| 318 |
hash ^= hash >> 13; |
| 319 |
hash *= m; |
| 320 |
hash ^= hash >> 15; |
| 321 |
return hash; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
} // END namespace jwt |
| 326 |
|
| 327 |
/// Provide a hash specialization |
| 328 |
namespace std { |
| 329 |
template <> |
| 330 |
struct hash<jwt::string_view> |
| 331 |
{ |
| 332 |
size_t operator()(const jwt::string_view& sv) const noexcept |
| 333 |
{ |
| 334 |
return jwt::hash_bytes((void*)sv.data(), sv.length(), static_cast<size_t>(0xc70f6907UL)); |
| 335 |
} |
| 336 |
}; |
| 337 |
} |
| 338 |
|
| 339 |
#endif |