Marshall Clow | 1e00d6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===------------------------ string_view ---------------------------------===// |
| 3 | // |
| 4 | // The LLVM Compiler Infrastructure |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_STRING_VIEW |
| 12 | #define _LIBCPP_STRING_VIEW |
| 13 | |
| 14 | /* |
| 15 | string_view synopsis |
| 16 | |
| 17 | namespace std { |
| 18 | |
| 19 | // 7.2, Class template basic_string_view |
| 20 | template<class charT, class traits = char_traits<charT>> |
| 21 | class basic_string_view; |
| 22 | |
| 23 | // 7.9, basic_string_view non-member comparison functions |
| 24 | template<class charT, class traits> |
| 25 | constexpr bool operator==(basic_string_view<charT, traits> x, |
| 26 | basic_string_view<charT, traits> y) noexcept; |
| 27 | template<class charT, class traits> |
| 28 | constexpr bool operator!=(basic_string_view<charT, traits> x, |
| 29 | basic_string_view<charT, traits> y) noexcept; |
| 30 | template<class charT, class traits> |
| 31 | constexpr bool operator< (basic_string_view<charT, traits> x, |
| 32 | basic_string_view<charT, traits> y) noexcept; |
| 33 | template<class charT, class traits> |
| 34 | constexpr bool operator> (basic_string_view<charT, traits> x, |
| 35 | basic_string_view<charT, traits> y) noexcept; |
| 36 | template<class charT, class traits> |
| 37 | constexpr bool operator<=(basic_string_view<charT, traits> x, |
| 38 | basic_string_view<charT, traits> y) noexcept; |
| 39 | template<class charT, class traits> |
| 40 | constexpr bool operator>=(basic_string_view<charT, traits> x, |
| 41 | basic_string_view<charT, traits> y) noexcept; |
| 42 | // see below, sufficient additional overloads of comparison functions |
| 43 | |
| 44 | // 7.10, Inserters and extractors |
| 45 | template<class charT, class traits> |
| 46 | basic_ostream<charT, traits>& |
| 47 | operator<<(basic_ostream<charT, traits>& os, |
| 48 | basic_string_view<charT, traits> str); |
| 49 | |
| 50 | // basic_string_view typedef names |
| 51 | typedef basic_string_view<char> string_view; |
| 52 | typedef basic_string_view<char16_t> u16string_view; |
| 53 | typedef basic_string_view<char32_t> u32string_view; |
| 54 | typedef basic_string_view<wchar_t> wstring_view; |
| 55 | |
| 56 | template<class charT, class traits = char_traits<charT>> |
| 57 | class basic_string_view { |
| 58 | public: |
| 59 | // types |
| 60 | typedef traits traits_type; |
| 61 | typedef charT value_type; |
| 62 | typedef charT* pointer; |
| 63 | typedef const charT* const_pointer; |
| 64 | typedef charT& reference; |
| 65 | typedef const charT& const_reference; |
| 66 | typedef implementation-defined const_iterator; |
| 67 | typedef const_iterator iterator; |
| 68 | typedef reverse_iterator<const_iterator> const_reverse_iterator; |
| 69 | typedef const_reverse_iterator reverse_iterator; |
| 70 | typedef size_t size_type; |
| 71 | typedef ptrdiff_t difference_type; |
| 72 | static constexpr size_type npos = size_type(-1); |
| 73 | |
| 74 | // 7.3, basic_string_view constructors and assignment operators |
| 75 | constexpr basic_string_view() noexcept; |
| 76 | constexpr basic_string_view(const basic_string_view&) noexcept = default; |
| 77 | basic_string_view& operator=(const basic_string_view&) noexcept = default; |
| 78 | template<class Allocator> |
| 79 | constexpr basic_string_view(const charT* str); |
| 80 | constexpr basic_string_view(const charT* str, size_type len); |
| 81 | |
| 82 | // 7.4, basic_string_view iterator support |
| 83 | constexpr const_iterator begin() const noexcept; |
| 84 | constexpr const_iterator end() const noexcept; |
| 85 | constexpr const_iterator cbegin() const noexcept; |
| 86 | constexpr const_iterator cend() const noexcept; |
| 87 | const_reverse_iterator rbegin() const noexcept; |
| 88 | const_reverse_iterator rend() const noexcept; |
| 89 | const_reverse_iterator crbegin() const noexcept; |
| 90 | const_reverse_iterator crend() const noexcept; |
| 91 | |
| 92 | // 7.5, basic_string_view capacity |
| 93 | constexpr size_type size() const noexcept; |
| 94 | constexpr size_type length() const noexcept; |
| 95 | constexpr size_type max_size() const noexcept; |
| 96 | constexpr bool empty() const noexcept; |
| 97 | |
| 98 | // 7.6, basic_string_view element access |
| 99 | constexpr const_reference operator[](size_type pos) const; |
| 100 | constexpr const_reference at(size_type pos) const; |
| 101 | constexpr const_reference front() const; |
| 102 | constexpr const_reference back() const; |
| 103 | constexpr const_pointer data() const noexcept; |
| 104 | |
| 105 | // 7.7, basic_string_view modifiers |
| 106 | constexpr void clear() noexcept; |
| 107 | constexpr void remove_prefix(size_type n); |
| 108 | constexpr void remove_suffix(size_type n); |
| 109 | constexpr void swap(basic_string_view& s) noexcept; |
| 110 | |
| 111 | size_type copy(charT* s, size_type n, size_type pos = 0) const; |
| 112 | |
| 113 | constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const; |
| 114 | constexpr int compare(basic_string_view s) const noexcept; |
| 115 | constexpr int compare(size_type pos1, size_type n1, basic_string_view s) const; |
| 116 | constexpr int compare(size_type pos1, size_type n1, |
| 117 | basic_string_view s, size_type pos2, size_type n2) const; |
| 118 | constexpr int compare(const charT* s) const; |
| 119 | constexpr int compare(size_type pos1, size_type n1, const charT* s) const; |
| 120 | constexpr int compare(size_type pos1, size_type n1, |
| 121 | const charT* s, size_type n2) const; |
| 122 | constexpr size_type find(basic_string_view s, size_type pos = 0) const noexcept; |
| 123 | constexpr size_type find(charT c, size_type pos = 0) const noexcept; |
| 124 | constexpr size_type find(const charT* s, size_type pos, size_type n) const; |
| 125 | constexpr size_type find(const charT* s, size_type pos = 0) const; |
| 126 | constexpr size_type rfind(basic_string_view s, size_type pos = npos) const noexcept; |
| 127 | constexpr size_type rfind(charT c, size_type pos = npos) const noexcept; |
| 128 | constexpr size_type rfind(const charT* s, size_type pos, size_type n) const; |
| 129 | constexpr size_type rfind(const charT* s, size_type pos = npos) const; |
| 130 | constexpr size_type find_first_of(basic_string_view s, size_type pos = 0) const noexcept; |
| 131 | constexpr size_type find_first_of(charT c, size_type pos = 0) const noexcept; |
| 132 | constexpr size_type find_first_of(const charT* s, size_type pos, size_type n) const; |
| 133 | constexpr size_type find_first_of(const charT* s, size_type pos = 0) const; |
| 134 | constexpr size_type find_last_of(basic_string_view s, size_type pos = npos) const noexcept; |
| 135 | constexpr size_type find_last_of(charT c, size_type pos = npos) const noexcept; |
| 136 | constexpr size_type find_last_of(const charT* s, size_type pos, size_type n) const; |
| 137 | constexpr size_type find_last_of(const charT* s, size_type pos = npos) const; |
| 138 | constexpr size_type find_first_not_of(basic_string_view s, size_type pos = 0) const noexcept; |
| 139 | constexpr size_type find_first_not_of(charT c, size_type pos = 0) const noexcept; |
| 140 | constexpr size_type find_first_not_of(const charT* s, size_type pos, size_type n) const; |
| 141 | constexpr size_type find_first_not_of(const charT* s, size_type pos = 0) const; |
| 142 | constexpr size_type find_last_not_of(basic_string_view s, size_type pos = npos) const noexcept; |
| 143 | constexpr size_type find_last_not_of(charT c, size_type pos = npos) const noexcept; |
| 144 | constexpr size_type find_last_not_of(const charT* s, size_type pos, size_type n) const; |
| 145 | constexpr size_type find_last_not_of(const charT* s, size_type pos = npos) const; |
| 146 | |
| 147 | private: |
| 148 | const_pointer data_; // exposition only |
| 149 | size_type size_; // exposition only |
| 150 | }; |
| 151 | |
| 152 | // 7.11, Hash support |
| 153 | template <class T> struct hash; |
| 154 | template <> struct hash<string_view>; |
| 155 | template <> struct hash<u16string_view>; |
| 156 | template <> struct hash<u32string_view>; |
| 157 | template <> struct hash<wstring_view>; |
| 158 | |
| 159 | } // namespace std |
| 160 | |
| 161 | |
| 162 | */ |
| 163 | |
| 164 | #include <__config> |
| 165 | |
| 166 | #include <__string> |
| 167 | #include <algorithm> |
| 168 | #include <iterator> |
| 169 | #include <__debug> |
| 170 | |
| 171 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 172 | #pragma GCC system_header |
| 173 | #endif |
| 174 | |
| 175 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 176 | |
| 177 | template<class _CharT, class _Traits = char_traits<_CharT> > |
| 178 | class _LIBCPP_TYPE_VIS_ONLY basic_string_view { |
| 179 | public: |
| 180 | // types |
| 181 | typedef _Traits traits_type; |
| 182 | typedef _CharT value_type; |
| 183 | typedef const _CharT* pointer; |
| 184 | typedef const _CharT* const_pointer; |
| 185 | typedef const _CharT& reference; |
| 186 | typedef const _CharT& const_reference; |
| 187 | typedef const_pointer const_iterator; // See [string.view.iterators] |
| 188 | typedef const_iterator iterator; |
| 189 | typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator; |
| 190 | typedef const_reverse_iterator reverse_iterator; |
| 191 | typedef size_t size_type; |
| 192 | typedef ptrdiff_t difference_type; |
| 193 | static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1); |
| 194 | |
| 195 | // [string.view.cons], construct/copy |
| 196 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 197 | basic_string_view() _NOEXCEPT : __data (nullptr), __size(0) {} |
| 198 | |
| 199 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 200 | basic_string_view(const basic_string_view&) _NOEXCEPT = default; |
| 201 | |
| 202 | _LIBCPP_INLINE_VISIBILITY |
| 203 | basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default; |
| 204 | |
| 205 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 206 | basic_string_view(const _CharT* __s, size_type __len) |
| 207 | : __data(__s), __size(__len) |
| 208 | { |
Marshall Clow | 1536233 | 2016-07-21 06:24:04 +0000 | [diff] [blame] | 209 | // #if _LIBCPP_STD_VER > 11 |
| 210 | // _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr"); |
| 211 | // #endif |
Marshall Clow | 1e00d6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 215 | basic_string_view(const _CharT* __s) |
| 216 | : __data(__s), __size(_Traits::length(__s)) {} |
| 217 | |
| 218 | // [string.view.iterators], iterators |
| 219 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 220 | const_iterator begin() const _NOEXCEPT { return cbegin(); } |
| 221 | |
| 222 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 223 | const_iterator end() const _NOEXCEPT { return cend(); } |
| 224 | |
| 225 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 226 | const_iterator cbegin() const _NOEXCEPT { return __data; } |
| 227 | |
| 228 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 229 | const_iterator cend() const _NOEXCEPT { return __data + __size; } |
| 230 | |
| 231 | _LIBCPP_INLINE_VISIBILITY |
| 232 | const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); } |
| 233 | |
| 234 | _LIBCPP_INLINE_VISIBILITY |
| 235 | const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); } |
| 236 | |
| 237 | _LIBCPP_INLINE_VISIBILITY |
| 238 | const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); } |
| 239 | |
| 240 | _LIBCPP_INLINE_VISIBILITY |
| 241 | const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); } |
| 242 | |
| 243 | // [string.view.capacity], capacity |
| 244 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 245 | size_type size() const _NOEXCEPT { return __size; } |
| 246 | |
| 247 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 248 | size_type length() const _NOEXCEPT { return __size; } |
| 249 | |
| 250 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 251 | size_type max_size() const _NOEXCEPT { return numeric_limits<size_type>::max(); } |
| 252 | |
| 253 | _LIBCPP_CONSTEXPR bool _LIBCPP_INLINE_VISIBILITY |
| 254 | empty() const _NOEXCEPT { return __size == 0; } |
| 255 | |
| 256 | // [string.view.access], element access |
| 257 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 258 | const_reference operator[](size_type __pos) const _NOEXCEPT { return __data[__pos]; } |
| 259 | |
| 260 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 261 | const_reference at(size_type __pos) const |
| 262 | { |
| 263 | return __pos >= size() |
Marshall Clow | e7acb0e | 2016-08-25 17:47:09 +0000 | [diff] [blame] | 264 | ? (__throw_out_of_range("string_view::at"), __data[0]) |
Marshall Clow | 1e00d6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 265 | : __data[__pos]; |
| 266 | } |
| 267 | |
| 268 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 269 | const_reference front() const |
| 270 | { |
| 271 | return _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty"), __data[0]; |
| 272 | } |
| 273 | |
| 274 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 275 | const_reference back() const |
| 276 | { |
| 277 | return _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty"), __data[__size-1]; |
| 278 | } |
| 279 | |
| 280 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 281 | const_pointer data() const _NOEXCEPT { return __data; } |
| 282 | |
| 283 | // [string.view.modifiers], modifiers: |
| 284 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 285 | void clear() _NOEXCEPT |
| 286 | { |
| 287 | __data = nullptr; |
| 288 | __size = 0; |
| 289 | } |
| 290 | |
| 291 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 292 | void remove_prefix(size_type __n) _NOEXCEPT |
| 293 | { |
| 294 | _LIBCPP_ASSERT(__n <= size(), "remove_prefix() can't remove more than size()"); |
| 295 | __data += __n; |
| 296 | __size -= __n; |
| 297 | } |
| 298 | |
| 299 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 300 | void remove_suffix(size_type __n) _NOEXCEPT |
| 301 | { |
| 302 | _LIBCPP_ASSERT(__n <= size(), "remove_suffix() can't remove more than size()"); |
| 303 | __size -= __n; |
| 304 | } |
| 305 | |
| 306 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 307 | void swap(basic_string_view& __other) _NOEXCEPT |
| 308 | { |
| 309 | const value_type *__p = __data; |
| 310 | __data = __other.__data; |
| 311 | __other.__data = __p; |
| 312 | |
| 313 | size_type __sz = __size; |
| 314 | __size = __other.__size; |
| 315 | __other.__size = __sz; |
| 316 | } |
| 317 | |
| 318 | _LIBCPP_INLINE_VISIBILITY |
| 319 | size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const |
| 320 | { |
| 321 | if (__pos > size()) |
Marshall Clow | e7acb0e | 2016-08-25 17:47:09 +0000 | [diff] [blame] | 322 | __throw_out_of_range("string_view::copy"); |
Marshall Clow | 1e00d6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 323 | size_type __rlen = _VSTD::min( __n, size() - __pos ); |
| 324 | copy_n(begin() + __pos, __rlen, __s ); |
| 325 | return __rlen; |
| 326 | } |
| 327 | |
| 328 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 329 | basic_string_view substr(size_type __pos = 0, size_type __n = npos) const |
| 330 | { |
| 331 | return __pos > size() |
Marshall Clow | e7acb0e | 2016-08-25 17:47:09 +0000 | [diff] [blame] | 332 | ? (__throw_out_of_range("string_view::substr"), basic_string_view()) |
Marshall Clow | 1e00d6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 333 | : basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos)); |
| 334 | } |
| 335 | |
| 336 | _LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT |
| 337 | { |
| 338 | size_type __rlen = _VSTD::min( size(), __sv.size()); |
| 339 | int __retval = _Traits::compare(data(), __sv.data(), __rlen); |
| 340 | if ( __retval == 0 ) // first __rlen chars matched |
| 341 | __retval = size() == __sv.size() ? 0 : ( size() < __sv.size() ? -1 : 1 ); |
| 342 | return __retval; |
| 343 | } |
| 344 | |
| 345 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 346 | int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const |
| 347 | { |
| 348 | return substr(__pos1, __n1).compare(__sv); |
| 349 | } |
| 350 | |
| 351 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 352 | int compare( size_type __pos1, size_type __n1, |
| 353 | basic_string_view _sv, size_type __pos2, size_type __n2) const |
| 354 | { |
| 355 | return substr(__pos1, __n1).compare(_sv.substr(__pos2, __n2)); |
| 356 | } |
| 357 | |
| 358 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | e6521d6 | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 359 | int compare(const _CharT* __s) const _NOEXCEPT |
Marshall Clow | 1e00d6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 360 | { |
| 361 | return compare(basic_string_view(__s)); |
| 362 | } |
| 363 | |
| 364 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 365 | int compare(size_type __pos1, size_type __n1, const _CharT* __s) const |
| 366 | { |
| 367 | return substr(__pos1, __n1).compare(basic_string_view(__s)); |
| 368 | } |
| 369 | |
| 370 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 371 | int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const |
| 372 | { |
| 373 | return substr(__pos1, __n1).compare(basic_string_view(__s, __n2)); |
| 374 | } |
| 375 | |
| 376 | // find |
| 377 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 378 | size_type find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT |
| 379 | { |
| 380 | _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr"); |
| 381 | return __str_find<value_type, size_type, traits_type, npos> |
| 382 | (data(), size(), __s.data(), __pos, __s.size()); |
| 383 | } |
| 384 | |
| 385 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 386 | size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT |
| 387 | { |
| 388 | return __str_find<value_type, size_type, traits_type, npos> |
| 389 | (data(), size(), __c, __pos); |
| 390 | } |
| 391 | |
| 392 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 393 | size_type find(const _CharT* __s, size_type __pos, size_type __n) const |
| 394 | { |
| 395 | _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find(): received nullptr"); |
| 396 | return __str_find<value_type, size_type, traits_type, npos> |
| 397 | (data(), size(), __s, __pos, __n); |
| 398 | } |
| 399 | |
| 400 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 401 | size_type find(const _CharT* __s, size_type __pos = 0) const |
| 402 | { |
| 403 | _LIBCPP_ASSERT(__s != nullptr, "string_view::find(): received nullptr"); |
| 404 | return __str_find<value_type, size_type, traits_type, npos> |
| 405 | (data(), size(), __s, __pos, traits_type::length(__s)); |
| 406 | } |
| 407 | |
| 408 | // rfind |
| 409 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 410 | size_type rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT |
| 411 | { |
| 412 | _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr"); |
| 413 | return __str_rfind<value_type, size_type, traits_type, npos> |
| 414 | (data(), size(), __s.data(), __pos, __s.size()); |
| 415 | } |
| 416 | |
| 417 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 418 | size_type rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT |
| 419 | { |
| 420 | return __str_rfind<value_type, size_type, traits_type, npos> |
| 421 | (data(), size(), __c, __pos); |
| 422 | } |
| 423 | |
| 424 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 425 | size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const |
| 426 | { |
| 427 | _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr"); |
| 428 | return __str_rfind<value_type, size_type, traits_type, npos> |
| 429 | (data(), size(), __s, __pos, __n); |
| 430 | } |
| 431 | |
| 432 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 433 | size_type rfind(const _CharT* __s, size_type __pos=npos) const |
| 434 | { |
| 435 | _LIBCPP_ASSERT(__s != nullptr, "string_view::rfind(): received nullptr"); |
| 436 | return __str_rfind<value_type, size_type, traits_type, npos> |
| 437 | (data(), size(), __s, __pos, traits_type::length(__s)); |
| 438 | } |
| 439 | |
| 440 | // find_first_of |
| 441 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 442 | size_type find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT |
| 443 | { |
| 444 | _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr"); |
| 445 | return __str_find_first_of<value_type, size_type, traits_type, npos> |
| 446 | (data(), size(), __s.data(), __pos, __s.size()); |
| 447 | } |
| 448 | |
| 449 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 450 | size_type find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT |
| 451 | { return find(__c, __pos); } |
| 452 | |
| 453 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 454 | size_type find_first_of(const _CharT* __s, size_type __pos, size_type __n) const |
| 455 | { |
| 456 | _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_of(): received nullptr"); |
| 457 | return __str_find_first_of<value_type, size_type, traits_type, npos> |
| 458 | (data(), size(), __s, __pos, __n); |
| 459 | } |
| 460 | |
| 461 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 462 | size_type find_first_of(const _CharT* __s, size_type __pos=0) const |
| 463 | { |
| 464 | _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_of(): received nullptr"); |
| 465 | return __str_find_first_of<value_type, size_type, traits_type, npos> |
| 466 | (data(), size(), __s, __pos, traits_type::length(__s)); |
| 467 | } |
| 468 | |
| 469 | // find_last_of |
| 470 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 471 | size_type find_last_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT |
| 472 | { |
| 473 | _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr"); |
| 474 | return __str_find_last_of<value_type, size_type, traits_type, npos> |
| 475 | (data(), size(), __s.data(), __pos, __s.size()); |
| 476 | } |
| 477 | |
| 478 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 479 | size_type find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT |
| 480 | { return rfind(__c, __pos); } |
| 481 | |
| 482 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 483 | size_type find_last_of(const _CharT* __s, size_type __pos, size_type __n) const |
| 484 | { |
| 485 | _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_of(): received nullptr"); |
| 486 | return __str_find_last_of<value_type, size_type, traits_type, npos> |
| 487 | (data(), size(), __s, __pos, __n); |
| 488 | } |
| 489 | |
| 490 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 491 | size_type find_last_of(const _CharT* __s, size_type __pos=npos) const |
| 492 | { |
| 493 | _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_of(): received nullptr"); |
| 494 | return __str_find_last_of<value_type, size_type, traits_type, npos> |
| 495 | (data(), size(), __s, __pos, traits_type::length(__s)); |
| 496 | } |
| 497 | |
| 498 | // find_first_not_of |
| 499 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 500 | size_type find_first_not_of(basic_string_view __s, size_type __pos=0) const _NOEXCEPT |
| 501 | { |
| 502 | _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr"); |
| 503 | return __str_find_first_not_of<value_type, size_type, traits_type, npos> |
| 504 | (data(), size(), __s.data(), __pos, __s.size()); |
| 505 | } |
| 506 | |
| 507 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 508 | size_type find_first_not_of(_CharT __c, size_type __pos=0) const _NOEXCEPT |
| 509 | { |
| 510 | return __str_find_first_not_of<value_type, size_type, traits_type, npos> |
| 511 | (data(), size(), __c, __pos); |
| 512 | } |
| 513 | |
| 514 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 515 | size_type find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const |
| 516 | { |
| 517 | _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr"); |
| 518 | return __str_find_first_not_of<value_type, size_type, traits_type, npos> |
| 519 | (data(), size(), __s, __pos, __n); |
| 520 | } |
| 521 | |
| 522 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 523 | size_type find_first_not_of(const _CharT* __s, size_type __pos=0) const |
| 524 | { |
| 525 | _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_not_of(): received nullptr"); |
| 526 | return __str_find_first_not_of<value_type, size_type, traits_type, npos> |
| 527 | (data(), size(), __s, __pos, traits_type::length(__s)); |
| 528 | } |
| 529 | |
| 530 | // find_last_not_of |
| 531 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 532 | size_type find_last_not_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT |
| 533 | { |
| 534 | _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr"); |
| 535 | return __str_find_last_not_of<value_type, size_type, traits_type, npos> |
| 536 | (data(), size(), __s.data(), __pos, __s.size()); |
| 537 | } |
| 538 | |
| 539 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 540 | size_type find_last_not_of(_CharT __c, size_type __pos=npos) const _NOEXCEPT |
| 541 | { |
| 542 | return __str_find_last_not_of<value_type, size_type, traits_type, npos> |
| 543 | (data(), size(), __c, __pos); |
| 544 | } |
| 545 | |
| 546 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 547 | size_type find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const |
| 548 | { |
| 549 | _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr"); |
| 550 | return __str_find_last_not_of<value_type, size_type, traits_type, npos> |
| 551 | (data(), size(), __s, __pos, __n); |
| 552 | } |
| 553 | |
| 554 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 555 | size_type find_last_not_of(const _CharT* __s, size_type __pos=npos) const |
| 556 | { |
| 557 | _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_not_of(): received nullptr"); |
| 558 | return __str_find_last_not_of<value_type, size_type, traits_type, npos> |
| 559 | (data(), size(), __s, __pos, traits_type::length(__s)); |
| 560 | } |
| 561 | |
| 562 | private: |
| 563 | const value_type* __data; |
| 564 | size_type __size; |
| 565 | }; |
| 566 | |
| 567 | |
| 568 | // [string.view.comparison] |
| 569 | // operator == |
| 570 | template<class _CharT, class _Traits> |
| 571 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 572 | bool operator==(basic_string_view<_CharT, _Traits> __lhs, |
| 573 | basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 574 | { |
| 575 | if ( __lhs.size() != __rhs.size()) return false; |
| 576 | return __lhs.compare(__rhs) == 0; |
| 577 | } |
| 578 | |
| 579 | template<class _CharT, class _Traits> |
| 580 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 581 | bool operator==(basic_string_view<_CharT, _Traits> __lhs, |
| 582 | typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT |
| 583 | { |
| 584 | if ( __lhs.size() != __rhs.size()) return false; |
| 585 | return __lhs.compare(__rhs) == 0; |
| 586 | } |
| 587 | |
| 588 | template<class _CharT, class _Traits> |
| 589 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 590 | bool operator==(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs, |
| 591 | basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 592 | { |
| 593 | if ( __lhs.size() != __rhs.size()) return false; |
| 594 | return __lhs.compare(__rhs) == 0; |
| 595 | } |
| 596 | |
| 597 | |
| 598 | // operator != |
| 599 | template<class _CharT, class _Traits> |
| 600 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 601 | bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 602 | { |
| 603 | if ( __lhs.size() != __rhs.size()) |
| 604 | return true; |
| 605 | return __lhs.compare(__rhs) != 0; |
| 606 | } |
| 607 | |
| 608 | template<class _CharT, class _Traits> |
| 609 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 610 | bool operator!=(basic_string_view<_CharT, _Traits> __lhs, |
| 611 | typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT |
| 612 | { |
| 613 | if ( __lhs.size() != __rhs.size()) |
| 614 | return true; |
| 615 | return __lhs.compare(__rhs) != 0; |
| 616 | } |
| 617 | |
| 618 | template<class _CharT, class _Traits> |
| 619 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 620 | bool operator!=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs, |
| 621 | basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 622 | { |
| 623 | if ( __lhs.size() != __rhs.size()) |
| 624 | return true; |
| 625 | return __lhs.compare(__rhs) != 0; |
| 626 | } |
| 627 | |
| 628 | |
| 629 | // operator < |
| 630 | template<class _CharT, class _Traits> |
| 631 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 632 | bool operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 633 | { |
| 634 | return __lhs.compare(__rhs) < 0; |
| 635 | } |
| 636 | |
| 637 | template<class _CharT, class _Traits> |
| 638 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 639 | bool operator<(basic_string_view<_CharT, _Traits> __lhs, |
| 640 | typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT |
| 641 | { |
| 642 | return __lhs.compare(__rhs) < 0; |
| 643 | } |
| 644 | |
| 645 | template<class _CharT, class _Traits> |
| 646 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 647 | bool operator<(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs, |
| 648 | basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 649 | { |
| 650 | return __lhs.compare(__rhs) < 0; |
| 651 | } |
| 652 | |
| 653 | |
| 654 | // operator > |
| 655 | template<class _CharT, class _Traits> |
| 656 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 657 | bool operator> (basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 658 | { |
| 659 | return __lhs.compare(__rhs) > 0; |
| 660 | } |
| 661 | |
| 662 | template<class _CharT, class _Traits> |
| 663 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 664 | bool operator>(basic_string_view<_CharT, _Traits> __lhs, |
| 665 | typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT |
| 666 | { |
| 667 | return __lhs.compare(__rhs) > 0; |
| 668 | } |
| 669 | |
| 670 | template<class _CharT, class _Traits> |
| 671 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 672 | bool operator>(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs, |
| 673 | basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 674 | { |
| 675 | return __lhs.compare(__rhs) > 0; |
| 676 | } |
| 677 | |
| 678 | |
| 679 | // operator <= |
| 680 | template<class _CharT, class _Traits> |
| 681 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 682 | bool operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 683 | { |
| 684 | return __lhs.compare(__rhs) <= 0; |
| 685 | } |
| 686 | |
| 687 | template<class _CharT, class _Traits> |
| 688 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 689 | bool operator<=(basic_string_view<_CharT, _Traits> __lhs, |
| 690 | typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT |
| 691 | { |
| 692 | return __lhs.compare(__rhs) <= 0; |
| 693 | } |
| 694 | |
| 695 | template<class _CharT, class _Traits> |
| 696 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 697 | bool operator<=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs, |
| 698 | basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 699 | { |
| 700 | return __lhs.compare(__rhs) <= 0; |
| 701 | } |
| 702 | |
| 703 | |
| 704 | // operator >= |
| 705 | template<class _CharT, class _Traits> |
| 706 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 707 | bool operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 708 | { |
| 709 | return __lhs.compare(__rhs) >= 0; |
| 710 | } |
| 711 | |
| 712 | |
| 713 | template<class _CharT, class _Traits> |
| 714 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 715 | bool operator>=(basic_string_view<_CharT, _Traits> __lhs, |
| 716 | typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT |
| 717 | { |
| 718 | return __lhs.compare(__rhs) >= 0; |
| 719 | } |
| 720 | |
| 721 | template<class _CharT, class _Traits> |
| 722 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 723 | bool operator>=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs, |
| 724 | basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 725 | { |
| 726 | return __lhs.compare(__rhs) >= 0; |
| 727 | } |
| 728 | |
| 729 | typedef basic_string_view<char> string_view; |
| 730 | typedef basic_string_view<char16_t> u16string_view; |
| 731 | typedef basic_string_view<char32_t> u32string_view; |
| 732 | typedef basic_string_view<wchar_t> wstring_view; |
| 733 | |
| 734 | // [string.view.hash] |
| 735 | template<class _CharT, class _Traits> |
| 736 | struct _LIBCPP_TYPE_VIS_ONLY hash<basic_string_view<_CharT, _Traits> > |
| 737 | : public unary_function<basic_string_view<_CharT, _Traits>, size_t> |
| 738 | { |
| 739 | size_t operator()(const basic_string_view<_CharT, _Traits> __val) const _NOEXCEPT; |
| 740 | }; |
| 741 | |
| 742 | template<class _CharT, class _Traits> |
| 743 | size_t |
| 744 | hash<basic_string_view<_CharT, _Traits> >::operator()( |
| 745 | const basic_string_view<_CharT, _Traits> __val) const _NOEXCEPT |
| 746 | { |
| 747 | return __do_string_hash(__val.data(), __val.data() + __val.size()); |
| 748 | } |
| 749 | |
Marshall Clow | 1e00d6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 750 | _LIBCPP_END_NAMESPACE_STD |
| 751 | |
| 752 | #endif // _LIBCPP_STRING_VIEW |