Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
Louis Dionne | 9bd9388 | 2021-11-17 16:25:01 -0500 | [diff] [blame] | 2 | //===----------------------------------------------------------------------===// |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 3 | // |
Chandler Carruth | 7642bb1 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_STRING_VIEW |
| 11 | #define _LIBCPP_STRING_VIEW |
| 12 | |
| 13 | /* |
| 14 | string_view synopsis |
| 15 | |
| 16 | namespace std { |
| 17 | |
| 18 | // 7.2, Class template basic_string_view |
| 19 | template<class charT, class traits = char_traits<charT>> |
| 20 | class basic_string_view; |
| 21 | |
Mark de Wever | af59b2d | 2020-11-24 18:08:02 +0100 | [diff] [blame] | 22 | template<class charT, class traits> |
Christopher Di Bella | 92072c2 | 2021-05-14 06:20:07 +0000 | [diff] [blame] | 23 | inline constexpr bool ranges::enable_view<basic_string_view<charT, traits>> = true; |
| 24 | |
| 25 | template<class charT, class traits> |
Mark de Wever | af59b2d | 2020-11-24 18:08:02 +0100 | [diff] [blame] | 26 | inline constexpr bool ranges::enable_borrowed_range<basic_string_view<charT, traits>> = true; // C++20 |
| 27 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 28 | // 7.9, basic_string_view non-member comparison functions |
| 29 | template<class charT, class traits> |
| 30 | constexpr bool operator==(basic_string_view<charT, traits> x, |
| 31 | basic_string_view<charT, traits> y) noexcept; |
| 32 | template<class charT, class traits> |
| 33 | constexpr bool operator!=(basic_string_view<charT, traits> x, |
| 34 | basic_string_view<charT, traits> y) noexcept; |
| 35 | template<class charT, class traits> |
| 36 | constexpr bool operator< (basic_string_view<charT, traits> x, |
| 37 | basic_string_view<charT, traits> y) noexcept; |
| 38 | template<class charT, class traits> |
| 39 | constexpr bool operator> (basic_string_view<charT, traits> x, |
| 40 | basic_string_view<charT, traits> y) noexcept; |
| 41 | template<class charT, class traits> |
| 42 | constexpr bool operator<=(basic_string_view<charT, traits> x, |
| 43 | basic_string_view<charT, traits> y) noexcept; |
| 44 | template<class charT, class traits> |
| 45 | constexpr bool operator>=(basic_string_view<charT, traits> x, |
| 46 | basic_string_view<charT, traits> y) noexcept; |
| 47 | // see below, sufficient additional overloads of comparison functions |
| 48 | |
| 49 | // 7.10, Inserters and extractors |
| 50 | template<class charT, class traits> |
| 51 | basic_ostream<charT, traits>& |
| 52 | operator<<(basic_ostream<charT, traits>& os, |
| 53 | basic_string_view<charT, traits> str); |
| 54 | |
| 55 | // basic_string_view typedef names |
| 56 | typedef basic_string_view<char> string_view; |
Marek Kurdej | e3ac4e2 | 2021-03-23 17:15:07 +0100 | [diff] [blame] | 57 | typedef basic_string_view<char8_t> u8string_view; // C++20 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 58 | typedef basic_string_view<char16_t> u16string_view; |
| 59 | typedef basic_string_view<char32_t> u32string_view; |
| 60 | typedef basic_string_view<wchar_t> wstring_view; |
| 61 | |
| 62 | template<class charT, class traits = char_traits<charT>> |
| 63 | class basic_string_view { |
| 64 | public: |
| 65 | // types |
| 66 | typedef traits traits_type; |
| 67 | typedef charT value_type; |
| 68 | typedef charT* pointer; |
| 69 | typedef const charT* const_pointer; |
| 70 | typedef charT& reference; |
| 71 | typedef const charT& const_reference; |
| 72 | typedef implementation-defined const_iterator; |
| 73 | typedef const_iterator iterator; |
| 74 | typedef reverse_iterator<const_iterator> const_reverse_iterator; |
| 75 | typedef const_reverse_iterator reverse_iterator; |
| 76 | typedef size_t size_type; |
| 77 | typedef ptrdiff_t difference_type; |
| 78 | static constexpr size_type npos = size_type(-1); |
| 79 | |
| 80 | // 7.3, basic_string_view constructors and assignment operators |
| 81 | constexpr basic_string_view() noexcept; |
| 82 | constexpr basic_string_view(const basic_string_view&) noexcept = default; |
| 83 | basic_string_view& operator=(const basic_string_view&) noexcept = default; |
| 84 | template<class Allocator> |
| 85 | constexpr basic_string_view(const charT* str); |
Marek Kurdej | 90d7971 | 2021-07-27 16:16:21 +0200 | [diff] [blame] | 86 | basic_string_view(nullptr_t) = delete; // C++2b |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 87 | constexpr basic_string_view(const charT* str, size_type len); |
Joe Loser | 28dcceb | 2021-10-06 14:17:02 -0400 | [diff] [blame] | 88 | template <class It, class End> |
| 89 | constexpr basic_string_view(It begin, End end); // C++20 |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 90 | template <class Range> |
| 91 | constexpr basic_string_view(Range&& r); // C++23 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 92 | |
| 93 | // 7.4, basic_string_view iterator support |
| 94 | constexpr const_iterator begin() const noexcept; |
| 95 | constexpr const_iterator end() const noexcept; |
| 96 | constexpr const_iterator cbegin() const noexcept; |
| 97 | constexpr const_iterator cend() const noexcept; |
| 98 | const_reverse_iterator rbegin() const noexcept; |
| 99 | const_reverse_iterator rend() const noexcept; |
| 100 | const_reverse_iterator crbegin() const noexcept; |
| 101 | const_reverse_iterator crend() const noexcept; |
| 102 | |
| 103 | // 7.5, basic_string_view capacity |
| 104 | constexpr size_type size() const noexcept; |
| 105 | constexpr size_type length() const noexcept; |
| 106 | constexpr size_type max_size() const noexcept; |
| 107 | constexpr bool empty() const noexcept; |
| 108 | |
| 109 | // 7.6, basic_string_view element access |
| 110 | constexpr const_reference operator[](size_type pos) const; |
| 111 | constexpr const_reference at(size_type pos) const; |
| 112 | constexpr const_reference front() const; |
| 113 | constexpr const_reference back() const; |
| 114 | constexpr const_pointer data() const noexcept; |
| 115 | |
| 116 | // 7.7, basic_string_view modifiers |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 117 | constexpr void remove_prefix(size_type n); |
| 118 | constexpr void remove_suffix(size_type n); |
| 119 | constexpr void swap(basic_string_view& s) noexcept; |
| 120 | |
Arthur O'Dwyer | 20638cc | 2021-02-09 19:12:16 -0500 | [diff] [blame] | 121 | size_type copy(charT* s, size_type n, size_type pos = 0) const; // constexpr in C++20 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 122 | |
| 123 | constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const; |
| 124 | constexpr int compare(basic_string_view s) const noexcept; |
| 125 | constexpr int compare(size_type pos1, size_type n1, basic_string_view s) const; |
| 126 | constexpr int compare(size_type pos1, size_type n1, |
| 127 | basic_string_view s, size_type pos2, size_type n2) const; |
| 128 | constexpr int compare(const charT* s) const; |
| 129 | constexpr int compare(size_type pos1, size_type n1, const charT* s) const; |
| 130 | constexpr int compare(size_type pos1, size_type n1, |
| 131 | const charT* s, size_type n2) const; |
| 132 | constexpr size_type find(basic_string_view s, size_type pos = 0) const noexcept; |
| 133 | constexpr size_type find(charT c, size_type pos = 0) const noexcept; |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 134 | constexpr size_type find(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension |
| 135 | constexpr size_type find(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 136 | constexpr size_type rfind(basic_string_view s, size_type pos = npos) const noexcept; |
| 137 | constexpr size_type rfind(charT c, size_type pos = npos) const noexcept; |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 138 | constexpr size_type rfind(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension |
| 139 | constexpr size_type rfind(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 140 | constexpr size_type find_first_of(basic_string_view s, size_type pos = 0) const noexcept; |
| 141 | constexpr size_type find_first_of(charT c, size_type pos = 0) const noexcept; |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 142 | constexpr size_type find_first_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension |
| 143 | constexpr size_type find_first_of(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 144 | constexpr size_type find_last_of(basic_string_view s, size_type pos = npos) const noexcept; |
| 145 | constexpr size_type find_last_of(charT c, size_type pos = npos) const noexcept; |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 146 | constexpr size_type find_last_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension |
| 147 | constexpr size_type find_last_of(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 148 | constexpr size_type find_first_not_of(basic_string_view s, size_type pos = 0) const noexcept; |
| 149 | constexpr size_type find_first_not_of(charT c, size_type pos = 0) const noexcept; |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 150 | constexpr size_type find_first_not_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension |
| 151 | constexpr size_type find_first_not_of(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 152 | constexpr size_type find_last_not_of(basic_string_view s, size_type pos = npos) const noexcept; |
| 153 | constexpr size_type find_last_not_of(charT c, size_type pos = npos) const noexcept; |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 154 | constexpr size_type find_last_not_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension |
| 155 | constexpr size_type find_last_not_of(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 156 | |
Marek Kurdej | 24b4c51 | 2021-01-07 12:29:04 +0100 | [diff] [blame] | 157 | constexpr bool starts_with(basic_string_view s) const noexcept; // C++20 |
| 158 | constexpr bool starts_with(charT c) const noexcept; // C++20 |
| 159 | constexpr bool starts_with(const charT* s) const; // C++20 |
| 160 | constexpr bool ends_with(basic_string_view s) const noexcept; // C++20 |
| 161 | constexpr bool ends_with(charT c) const noexcept; // C++20 |
| 162 | constexpr bool ends_with(const charT* s) const; // C++20 |
Marshall Clow | 18c293b | 2017-12-04 20:11:38 +0000 | [diff] [blame] | 163 | |
Wim Leflere | 023c354 | 2021-01-19 14:33:30 -0500 | [diff] [blame] | 164 | constexpr bool contains(basic_string_view s) const noexcept; // C++2b |
| 165 | constexpr bool contains(charT c) const noexcept; // C++2b |
| 166 | constexpr bool contains(const charT* s) const; // C++2b |
| 167 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 168 | private: |
| 169 | const_pointer data_; // exposition only |
| 170 | size_type size_; // exposition only |
| 171 | }; |
| 172 | |
Joe Loser | 28dcceb | 2021-10-06 14:17:02 -0400 | [diff] [blame] | 173 | // basic_string_view deduction guides |
| 174 | template<class It, class End> |
| 175 | basic_string_view(It, End) -> basic_string_view<iter_value_t<It>>; // C++20 |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 176 | template<class Range> |
| 177 | basic_string_view(Range&&) -> basic_string_view<ranges::range_value_t<Range>>; // C++23 |
Joe Loser | 28dcceb | 2021-10-06 14:17:02 -0400 | [diff] [blame] | 178 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 179 | // 7.11, Hash support |
| 180 | template <class T> struct hash; |
| 181 | template <> struct hash<string_view>; |
Marek Kurdej | e3ac4e2 | 2021-03-23 17:15:07 +0100 | [diff] [blame] | 182 | template <> struct hash<u8string_view>; // C++20 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 183 | template <> struct hash<u16string_view>; |
| 184 | template <> struct hash<u32string_view>; |
| 185 | template <> struct hash<wstring_view>; |
| 186 | |
Marshall Clow | abe6daf | 2017-10-24 14:06:00 +0000 | [diff] [blame] | 187 | constexpr basic_string_view<char> operator "" sv( const char *str, size_t len ) noexcept; |
| 188 | constexpr basic_string_view<wchar_t> operator "" sv( const wchar_t *str, size_t len ) noexcept; |
Marek Kurdej | e3ac4e2 | 2021-03-23 17:15:07 +0100 | [diff] [blame] | 189 | constexpr basic_string_view<char8_t> operator "" sv( const char8_t *str, size_t len ) noexcept; // C++20 |
Marshall Clow | abe6daf | 2017-10-24 14:06:00 +0000 | [diff] [blame] | 190 | constexpr basic_string_view<char16_t> operator "" sv( const char16_t *str, size_t len ) noexcept; |
| 191 | constexpr basic_string_view<char32_t> operator "" sv( const char32_t *str, size_t len ) noexcept; |
Marshall Clow | 27c8a4c | 2017-01-09 18:07:34 +0000 | [diff] [blame] | 192 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 193 | } // namespace std |
| 194 | |
| 195 | |
| 196 | */ |
| 197 | |
| 198 | #include <__config> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame] | 199 | #include <__debug> |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 200 | #include <__ranges/concepts.h> |
| 201 | #include <__ranges/data.h> |
Mark de Wever | af59b2d | 2020-11-24 18:08:02 +0100 | [diff] [blame] | 202 | #include <__ranges/enable_borrowed_range.h> |
Christopher Di Bella | 92072c2 | 2021-05-14 06:20:07 +0000 | [diff] [blame] | 203 | #include <__ranges/enable_view.h> |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 204 | #include <__ranges/size.h> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 205 | #include <__string> |
Eric Fiselier | 2d357f7 | 2016-12-05 23:53:23 +0000 | [diff] [blame] | 206 | #include <algorithm> |
Arthur O'Dwyer | 7deec12 | 2021-03-24 18:19:12 -0400 | [diff] [blame] | 207 | #include <compare> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame] | 208 | #include <iosfwd> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 209 | #include <iterator> |
Eric Fiselier | 2d357f7 | 2016-12-05 23:53:23 +0000 | [diff] [blame] | 210 | #include <limits> |
| 211 | #include <stdexcept> |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 212 | #include <type_traits> |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 213 | #include <version> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 214 | |
| 215 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 216 | #pragma GCC system_header |
| 217 | #endif |
| 218 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 219 | _LIBCPP_PUSH_MACROS |
| 220 | #include <__undef_macros> |
| 221 | |
| 222 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 223 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 224 | |
| 225 | template<class _CharT, class _Traits = char_traits<_CharT> > |
Richard Smith | 256954d | 2020-11-11 17:12:18 -0800 | [diff] [blame] | 226 | class _LIBCPP_TEMPLATE_VIS basic_string_view; |
| 227 | |
| 228 | typedef basic_string_view<char> string_view; |
Arthur O'Dwyer | afa5d5f | 2021-04-18 21:47:08 -0400 | [diff] [blame] | 229 | #ifndef _LIBCPP_HAS_NO_CHAR8_T |
Richard Smith | 256954d | 2020-11-11 17:12:18 -0800 | [diff] [blame] | 230 | typedef basic_string_view<char8_t> u8string_view; |
| 231 | #endif |
| 232 | typedef basic_string_view<char16_t> u16string_view; |
| 233 | typedef basic_string_view<char32_t> u32string_view; |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 234 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Richard Smith | 256954d | 2020-11-11 17:12:18 -0800 | [diff] [blame] | 235 | typedef basic_string_view<wchar_t> wstring_view; |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 236 | #endif |
Richard Smith | 256954d | 2020-11-11 17:12:18 -0800 | [diff] [blame] | 237 | |
| 238 | template<class _CharT, class _Traits> |
| 239 | class |
| 240 | _LIBCPP_PREFERRED_NAME(string_view) |
Arthur O'Dwyer | afa5d5f | 2021-04-18 21:47:08 -0400 | [diff] [blame] | 241 | #ifndef _LIBCPP_HAS_NO_CHAR8_T |
Richard Smith | 256954d | 2020-11-11 17:12:18 -0800 | [diff] [blame] | 242 | _LIBCPP_PREFERRED_NAME(u8string_view) |
| 243 | #endif |
| 244 | _LIBCPP_PREFERRED_NAME(u16string_view) |
| 245 | _LIBCPP_PREFERRED_NAME(u32string_view) |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 246 | _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wstring_view)) |
Richard Smith | 256954d | 2020-11-11 17:12:18 -0800 | [diff] [blame] | 247 | basic_string_view { |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 248 | public: |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 249 | // types |
| 250 | typedef _Traits traits_type; |
| 251 | typedef _CharT value_type; |
Marshall Clow | 7d2c518 | 2017-12-20 16:31:40 +0000 | [diff] [blame] | 252 | typedef _CharT* pointer; |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 253 | typedef const _CharT* const_pointer; |
Marshall Clow | 7d2c518 | 2017-12-20 16:31:40 +0000 | [diff] [blame] | 254 | typedef _CharT& reference; |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 255 | typedef const _CharT& const_reference; |
| 256 | typedef const_pointer const_iterator; // See [string.view.iterators] |
| 257 | typedef const_iterator iterator; |
| 258 | typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator; |
| 259 | typedef const_reverse_iterator reverse_iterator; |
| 260 | typedef size_t size_type; |
| 261 | typedef ptrdiff_t difference_type; |
| 262 | static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1); |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 263 | |
Marshall Clow | 79f3354 | 2018-03-21 00:36:05 +0000 | [diff] [blame] | 264 | static_assert((!is_array<value_type>::value), "Character type of basic_string_view must not be an array"); |
| 265 | static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string_view must be standard-layout"); |
| 266 | static_assert(( is_trivial<value_type>::value), "Character type of basic_string_view must be trivial"); |
Marshall Clow | a3a74e0 | 2017-03-15 18:41:11 +0000 | [diff] [blame] | 267 | static_assert((is_same<_CharT, typename traits_type::char_type>::value), |
| 268 | "traits_type::char_type must be the same type as CharT"); |
| 269 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 270 | // [string.view.cons], construct/copy |
| 271 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 272 | basic_string_view() _NOEXCEPT : __data (nullptr), __size(0) {} |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 273 | |
Arthur O'Dwyer | a0cb1e8 | 2021-09-28 12:19:35 -0400 | [diff] [blame] | 274 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 275 | basic_string_view(const basic_string_view&) _NOEXCEPT = default; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 276 | |
Arthur O'Dwyer | a0cb1e8 | 2021-09-28 12:19:35 -0400 | [diff] [blame] | 277 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 278 | basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 279 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 280 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 281 | basic_string_view(const _CharT* __s, size_type __len) _NOEXCEPT |
| 282 | : __data(__s), __size(__len) |
| 283 | { |
Marshall Clow | ca1bcdb | 2019-06-04 02:07:11 +0000 | [diff] [blame] | 284 | #if _LIBCPP_STD_VER > 11 |
| 285 | _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr"); |
| 286 | #endif |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 287 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 288 | |
Joe Loser | 28dcceb | 2021-10-06 14:17:02 -0400 | [diff] [blame] | 289 | #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_RANGES) |
| 290 | template <contiguous_iterator _It, sized_sentinel_for<_It> _End> |
Joe Loser | f9dfe43 | 2021-11-24 15:35:15 -0500 | [diff] [blame] | 291 | requires (is_same_v<iter_value_t<_It>, _CharT> && !is_convertible_v<_End, size_type>) |
Joe Loser | 28dcceb | 2021-10-06 14:17:02 -0400 | [diff] [blame] | 292 | constexpr _LIBCPP_HIDE_FROM_ABI basic_string_view(_It __begin, _End __end) |
| 293 | : __data(_VSTD::to_address(__begin)), __size(__end - __begin) |
| 294 | { |
| 295 | _LIBCPP_ASSERT((__end - __begin) >= 0, "std::string_view::string_view(iterator, sentinel) received invalid range"); |
| 296 | } |
| 297 | #endif |
| 298 | |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 299 | #if _LIBCPP_STD_VER > 20 && !defined(_LIBCPP_HAS_NO_RANGES) |
| 300 | template <class _Range> |
| 301 | requires ( |
| 302 | !is_same_v<remove_cvref_t<_Range>, basic_string_view> && |
| 303 | ranges::contiguous_range<_Range> && |
| 304 | ranges::sized_range<_Range> && |
| 305 | is_same_v<ranges::range_value_t<_Range>, _CharT> && |
| 306 | !is_convertible_v<_Range, const _CharT*> && |
| 307 | (!requires(remove_cvref_t<_Range>& d) { |
| 308 | d.operator _VSTD::basic_string_view<_CharT, _Traits>(); |
| 309 | }) && |
| 310 | (!requires { |
| 311 | typename remove_reference_t<_Range>::traits_type; |
| 312 | } || is_same_v<typename remove_reference_t<_Range>::traits_type, _Traits>) |
| 313 | ) |
| 314 | constexpr _LIBCPP_HIDE_FROM_ABI |
| 315 | basic_string_view(_Range&& __r) : __data(ranges::data(__r)), __size(ranges::size(__r)) {} |
| 316 | #endif |
| 317 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 318 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 319 | basic_string_view(const _CharT* __s) |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 320 | : __data(__s), __size(_VSTD::__char_traits_length_checked<_Traits>(__s)) {} |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 321 | |
Marek Kurdej | 90d7971 | 2021-07-27 16:16:21 +0200 | [diff] [blame] | 322 | #if _LIBCPP_STD_VER > 20 |
| 323 | basic_string_view(nullptr_t) = delete; |
| 324 | #endif |
| 325 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 326 | // [string.view.iterators], iterators |
| 327 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 328 | const_iterator begin() const _NOEXCEPT { return cbegin(); } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 329 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 330 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 331 | const_iterator end() const _NOEXCEPT { return cend(); } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 332 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 333 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 334 | const_iterator cbegin() const _NOEXCEPT { return __data; } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 335 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 336 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 337 | const_iterator cend() const _NOEXCEPT { return __data + __size; } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 338 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 339 | _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY |
| 340 | const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 341 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 342 | _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY |
| 343 | const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 344 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 345 | _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY |
| 346 | const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 347 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 348 | _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY |
| 349 | const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 350 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 351 | // [string.view.capacity], capacity |
| 352 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 353 | size_type size() const _NOEXCEPT { return __size; } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 354 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 355 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 356 | size_type length() const _NOEXCEPT { return __size; } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 357 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 358 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 8542a4c | 2021-11-22 16:44:50 -0500 | [diff] [blame] | 359 | size_type max_size() const _NOEXCEPT { return numeric_limits<size_type>::max() / sizeof(value_type); } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 360 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 361 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 362 | bool empty() const _NOEXCEPT { return __size == 0; } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 363 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 364 | // [string.view.access], element access |
| 365 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
Chris Palmer | 1b114a5 | 2020-10-06 13:01:50 -0400 | [diff] [blame] | 366 | const_reference operator[](size_type __pos) const _NOEXCEPT { |
| 367 | return _LIBCPP_ASSERT(__pos < size(), "string_view[] index out of bounds"), __data[__pos]; |
| 368 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 369 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 370 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 371 | const_reference at(size_type __pos) const |
| 372 | { |
| 373 | return __pos >= size() |
| 374 | ? (__throw_out_of_range("string_view::at"), __data[0]) |
| 375 | : __data[__pos]; |
| 376 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 377 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 378 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 05cf669 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 379 | const_reference front() const _NOEXCEPT |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 380 | { |
| 381 | return _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty"), __data[0]; |
| 382 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 383 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 384 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 05cf669 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 385 | const_reference back() const _NOEXCEPT |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 386 | { |
| 387 | return _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty"), __data[__size-1]; |
| 388 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 389 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 390 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 391 | const_pointer data() const _NOEXCEPT { return __data; } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 392 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 393 | // [string.view.modifiers], modifiers: |
| 394 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 395 | void remove_prefix(size_type __n) _NOEXCEPT |
| 396 | { |
| 397 | _LIBCPP_ASSERT(__n <= size(), "remove_prefix() can't remove more than size()"); |
| 398 | __data += __n; |
| 399 | __size -= __n; |
| 400 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 401 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 402 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 403 | void remove_suffix(size_type __n) _NOEXCEPT |
| 404 | { |
| 405 | _LIBCPP_ASSERT(__n <= size(), "remove_suffix() can't remove more than size()"); |
| 406 | __size -= __n; |
| 407 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 408 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 409 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 410 | void swap(basic_string_view& __other) _NOEXCEPT |
| 411 | { |
| 412 | const value_type *__p = __data; |
| 413 | __data = __other.__data; |
| 414 | __other.__data = __p; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 415 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 416 | size_type __sz = __size; |
| 417 | __size = __other.__size; |
| 418 | __other.__size = __sz; |
| 419 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 420 | |
Arthur O'Dwyer | 20638cc | 2021-02-09 19:12:16 -0500 | [diff] [blame] | 421 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 422 | size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const |
| 423 | { |
| 424 | if (__pos > size()) |
| 425 | __throw_out_of_range("string_view::copy"); |
| 426 | size_type __rlen = _VSTD::min(__n, size() - __pos); |
| 427 | _Traits::copy(__s, data() + __pos, __rlen); |
| 428 | return __rlen; |
| 429 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 430 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 431 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 432 | basic_string_view substr(size_type __pos = 0, size_type __n = npos) const |
| 433 | { |
| 434 | return __pos > size() |
| 435 | ? (__throw_out_of_range("string_view::substr"), basic_string_view()) |
| 436 | : basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos)); |
| 437 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 438 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 439 | _LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT |
| 440 | { |
| 441 | size_type __rlen = _VSTD::min( size(), __sv.size()); |
| 442 | int __retval = _Traits::compare(data(), __sv.data(), __rlen); |
| 443 | if ( __retval == 0 ) // first __rlen chars matched |
| 444 | __retval = size() == __sv.size() ? 0 : ( size() < __sv.size() ? -1 : 1 ); |
| 445 | return __retval; |
| 446 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 447 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 448 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 449 | int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const |
| 450 | { |
| 451 | return substr(__pos1, __n1).compare(__sv); |
| 452 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 453 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 454 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 455 | int compare( size_type __pos1, size_type __n1, |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 456 | basic_string_view __sv, size_type __pos2, size_type __n2) const |
| 457 | { |
| 458 | return substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2)); |
| 459 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 460 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 461 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 462 | int compare(const _CharT* __s) const _NOEXCEPT |
| 463 | { |
| 464 | return compare(basic_string_view(__s)); |
| 465 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 466 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 467 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 468 | int compare(size_type __pos1, size_type __n1, const _CharT* __s) const |
| 469 | { |
| 470 | return substr(__pos1, __n1).compare(basic_string_view(__s)); |
| 471 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 472 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 473 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 474 | int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const |
| 475 | { |
| 476 | return substr(__pos1, __n1).compare(basic_string_view(__s, __n2)); |
| 477 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 478 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 479 | // find |
| 480 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 481 | size_type find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT |
| 482 | { |
| 483 | _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr"); |
| 484 | return __str_find<value_type, size_type, traits_type, npos> |
| 485 | (data(), size(), __s.data(), __pos, __s.size()); |
| 486 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 487 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 488 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 489 | size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT |
| 490 | { |
| 491 | return __str_find<value_type, size_type, traits_type, npos> |
| 492 | (data(), size(), __c, __pos); |
| 493 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 494 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 495 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 496 | size_type find(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 497 | { |
| 498 | _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find(): received nullptr"); |
| 499 | return __str_find<value_type, size_type, traits_type, npos> |
| 500 | (data(), size(), __s, __pos, __n); |
| 501 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 502 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 503 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 504 | size_type find(const _CharT* __s, size_type __pos = 0) const _NOEXCEPT |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 505 | { |
| 506 | _LIBCPP_ASSERT(__s != nullptr, "string_view::find(): received nullptr"); |
| 507 | return __str_find<value_type, size_type, traits_type, npos> |
| 508 | (data(), size(), __s, __pos, traits_type::length(__s)); |
| 509 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 510 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 511 | // rfind |
| 512 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 513 | size_type rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT |
| 514 | { |
| 515 | _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr"); |
| 516 | return __str_rfind<value_type, size_type, traits_type, npos> |
| 517 | (data(), size(), __s.data(), __pos, __s.size()); |
| 518 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 519 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 520 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 521 | size_type rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT |
| 522 | { |
| 523 | return __str_rfind<value_type, size_type, traits_type, npos> |
| 524 | (data(), size(), __c, __pos); |
| 525 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 526 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 527 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 528 | size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 529 | { |
| 530 | _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr"); |
| 531 | return __str_rfind<value_type, size_type, traits_type, npos> |
| 532 | (data(), size(), __s, __pos, __n); |
| 533 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 534 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 535 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 536 | size_type rfind(const _CharT* __s, size_type __pos=npos) const _NOEXCEPT |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 537 | { |
| 538 | _LIBCPP_ASSERT(__s != nullptr, "string_view::rfind(): received nullptr"); |
| 539 | return __str_rfind<value_type, size_type, traits_type, npos> |
| 540 | (data(), size(), __s, __pos, traits_type::length(__s)); |
| 541 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 542 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 543 | // find_first_of |
| 544 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 545 | size_type find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT |
| 546 | { |
| 547 | _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr"); |
| 548 | return __str_find_first_of<value_type, size_type, traits_type, npos> |
| 549 | (data(), size(), __s.data(), __pos, __s.size()); |
| 550 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 551 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 552 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 553 | size_type find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT |
| 554 | { return find(__c, __pos); } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 555 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 556 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 557 | size_type find_first_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 558 | { |
| 559 | _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_of(): received nullptr"); |
| 560 | return __str_find_first_of<value_type, size_type, traits_type, npos> |
| 561 | (data(), size(), __s, __pos, __n); |
| 562 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 563 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 564 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 565 | size_type find_first_of(const _CharT* __s, size_type __pos=0) const _NOEXCEPT |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 566 | { |
| 567 | _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_of(): received nullptr"); |
| 568 | return __str_find_first_of<value_type, size_type, traits_type, npos> |
| 569 | (data(), size(), __s, __pos, traits_type::length(__s)); |
| 570 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 571 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 572 | // find_last_of |
| 573 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 574 | size_type find_last_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT |
| 575 | { |
| 576 | _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr"); |
| 577 | return __str_find_last_of<value_type, size_type, traits_type, npos> |
| 578 | (data(), size(), __s.data(), __pos, __s.size()); |
| 579 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 580 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 581 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 582 | size_type find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT |
| 583 | { return rfind(__c, __pos); } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 584 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 585 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 586 | size_type find_last_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 587 | { |
| 588 | _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_of(): received nullptr"); |
| 589 | return __str_find_last_of<value_type, size_type, traits_type, npos> |
| 590 | (data(), size(), __s, __pos, __n); |
| 591 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 592 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 593 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 594 | size_type find_last_of(const _CharT* __s, size_type __pos=npos) const _NOEXCEPT |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 595 | { |
| 596 | _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_of(): received nullptr"); |
| 597 | return __str_find_last_of<value_type, size_type, traits_type, npos> |
| 598 | (data(), size(), __s, __pos, traits_type::length(__s)); |
| 599 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 600 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 601 | // find_first_not_of |
| 602 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 603 | size_type find_first_not_of(basic_string_view __s, size_type __pos=0) const _NOEXCEPT |
| 604 | { |
| 605 | _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr"); |
| 606 | return __str_find_first_not_of<value_type, size_type, traits_type, npos> |
| 607 | (data(), size(), __s.data(), __pos, __s.size()); |
| 608 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 609 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 610 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 611 | size_type find_first_not_of(_CharT __c, size_type __pos=0) const _NOEXCEPT |
| 612 | { |
| 613 | return __str_find_first_not_of<value_type, size_type, traits_type, npos> |
| 614 | (data(), size(), __c, __pos); |
| 615 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 616 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 617 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 618 | size_type find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 619 | { |
| 620 | _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr"); |
| 621 | return __str_find_first_not_of<value_type, size_type, traits_type, npos> |
| 622 | (data(), size(), __s, __pos, __n); |
| 623 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 624 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 625 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 626 | size_type find_first_not_of(const _CharT* __s, size_type __pos=0) const _NOEXCEPT |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 627 | { |
| 628 | _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_not_of(): received nullptr"); |
| 629 | return __str_find_first_not_of<value_type, size_type, traits_type, npos> |
| 630 | (data(), size(), __s, __pos, traits_type::length(__s)); |
| 631 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 632 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 633 | // find_last_not_of |
| 634 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 635 | size_type find_last_not_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT |
| 636 | { |
| 637 | _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr"); |
| 638 | return __str_find_last_not_of<value_type, size_type, traits_type, npos> |
| 639 | (data(), size(), __s.data(), __pos, __s.size()); |
| 640 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 641 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 642 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 643 | size_type find_last_not_of(_CharT __c, size_type __pos=npos) const _NOEXCEPT |
| 644 | { |
| 645 | return __str_find_last_not_of<value_type, size_type, traits_type, npos> |
| 646 | (data(), size(), __c, __pos); |
| 647 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 648 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 649 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 650 | size_type find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 651 | { |
| 652 | _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr"); |
| 653 | return __str_find_last_not_of<value_type, size_type, traits_type, npos> |
| 654 | (data(), size(), __s, __pos, __n); |
| 655 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 656 | |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 657 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 658 | size_type find_last_not_of(const _CharT* __s, size_type __pos=npos) const _NOEXCEPT |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 659 | { |
| 660 | _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_not_of(): received nullptr"); |
| 661 | return __str_find_last_not_of<value_type, size_type, traits_type, npos> |
| 662 | (data(), size(), __s, __pos, traits_type::length(__s)); |
| 663 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 664 | |
Marshall Clow | 18c293b | 2017-12-04 20:11:38 +0000 | [diff] [blame] | 665 | #if _LIBCPP_STD_VER > 17 |
Arthur O'Dwyer | a0cb1e8 | 2021-09-28 12:19:35 -0400 | [diff] [blame] | 666 | constexpr _LIBCPP_INLINE_VISIBILITY |
| 667 | bool starts_with(basic_string_view __s) const noexcept |
Marshall Clow | 18c293b | 2017-12-04 20:11:38 +0000 | [diff] [blame] | 668 | { return size() >= __s.size() && compare(0, __s.size(), __s) == 0; } |
| 669 | |
Arthur O'Dwyer | a0cb1e8 | 2021-09-28 12:19:35 -0400 | [diff] [blame] | 670 | constexpr _LIBCPP_INLINE_VISIBILITY |
| 671 | bool starts_with(value_type __c) const noexcept |
Marshall Clow | 18c293b | 2017-12-04 20:11:38 +0000 | [diff] [blame] | 672 | { return !empty() && _Traits::eq(front(), __c); } |
| 673 | |
Arthur O'Dwyer | a0cb1e8 | 2021-09-28 12:19:35 -0400 | [diff] [blame] | 674 | constexpr _LIBCPP_INLINE_VISIBILITY |
| 675 | bool starts_with(const value_type* __s) const noexcept |
Marshall Clow | 18c293b | 2017-12-04 20:11:38 +0000 | [diff] [blame] | 676 | { return starts_with(basic_string_view(__s)); } |
| 677 | |
Arthur O'Dwyer | a0cb1e8 | 2021-09-28 12:19:35 -0400 | [diff] [blame] | 678 | constexpr _LIBCPP_INLINE_VISIBILITY |
| 679 | bool ends_with(basic_string_view __s) const noexcept |
Marshall Clow | 18c293b | 2017-12-04 20:11:38 +0000 | [diff] [blame] | 680 | { return size() >= __s.size() && compare(size() - __s.size(), npos, __s) == 0; } |
| 681 | |
Arthur O'Dwyer | a0cb1e8 | 2021-09-28 12:19:35 -0400 | [diff] [blame] | 682 | constexpr _LIBCPP_INLINE_VISIBILITY |
| 683 | bool ends_with(value_type __c) const noexcept |
Marshall Clow | 18c293b | 2017-12-04 20:11:38 +0000 | [diff] [blame] | 684 | { return !empty() && _Traits::eq(back(), __c); } |
| 685 | |
Arthur O'Dwyer | a0cb1e8 | 2021-09-28 12:19:35 -0400 | [diff] [blame] | 686 | constexpr _LIBCPP_INLINE_VISIBILITY |
| 687 | bool ends_with(const value_type* __s) const noexcept |
Marshall Clow | 18c293b | 2017-12-04 20:11:38 +0000 | [diff] [blame] | 688 | { return ends_with(basic_string_view(__s)); } |
| 689 | #endif |
| 690 | |
Wim Leflere | 023c354 | 2021-01-19 14:33:30 -0500 | [diff] [blame] | 691 | #if _LIBCPP_STD_VER > 20 |
| 692 | constexpr _LIBCPP_INLINE_VISIBILITY |
| 693 | bool contains(basic_string_view __sv) const noexcept |
| 694 | { return find(__sv) != npos; } |
| 695 | |
| 696 | constexpr _LIBCPP_INLINE_VISIBILITY |
| 697 | bool contains(value_type __c) const noexcept |
| 698 | { return find(__c) != npos; } |
| 699 | |
| 700 | constexpr _LIBCPP_INLINE_VISIBILITY |
| 701 | bool contains(const value_type* __s) const |
| 702 | { return find(__s) != npos; } |
| 703 | #endif |
| 704 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 705 | private: |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 706 | const value_type* __data; |
| 707 | size_type __size; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 708 | }; |
| 709 | |
Mark de Wever | af59b2d | 2020-11-24 18:08:02 +0100 | [diff] [blame] | 710 | #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_RANGES) |
| 711 | template <class _CharT, class _Traits> |
Christopher Di Bella | 92072c2 | 2021-05-14 06:20:07 +0000 | [diff] [blame] | 712 | inline constexpr bool ranges::enable_view<basic_string_view<_CharT, _Traits>> = true; |
| 713 | |
| 714 | template <class _CharT, class _Traits> |
Mark de Wever | af59b2d | 2020-11-24 18:08:02 +0100 | [diff] [blame] | 715 | inline constexpr bool ranges::enable_borrowed_range<basic_string_view<_CharT, _Traits> > = true; |
| 716 | #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_RANGES) |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 717 | |
Joe Loser | 28dcceb | 2021-10-06 14:17:02 -0400 | [diff] [blame] | 718 | // [string.view.deduct] |
| 719 | |
| 720 | #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_RANGES) |
| 721 | template <contiguous_iterator _It, sized_sentinel_for<_It> _End> |
| 722 | basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>; |
| 723 | #endif |
| 724 | |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 725 | |
| 726 | #if _LIBCPP_STD_VER > 20 && !defined(_LIBCPP_HAS_NO_RANGES) |
| 727 | template <ranges::contiguous_range _Range> |
| 728 | basic_string_view(_Range) -> basic_string_view<ranges::range_value_t<_Range>>; |
| 729 | #endif |
| 730 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 731 | // [string.view.comparison] |
| 732 | // operator == |
| 733 | template<class _CharT, class _Traits> |
| 734 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 735 | bool operator==(basic_string_view<_CharT, _Traits> __lhs, |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 736 | basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 737 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 738 | if ( __lhs.size() != __rhs.size()) return false; |
| 739 | return __lhs.compare(__rhs) == 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 742 | // The dummy default template parameters are used to work around a MSVC issue with mangling, see VSO-409326 for details. |
| 743 | // This applies to the other sufficient overloads below for the other comparison operators. |
| 744 | template<class _CharT, class _Traits, int = 1> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 745 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 746 | bool operator==(basic_string_view<_CharT, _Traits> __lhs, |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 747 | typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 748 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 749 | if ( __lhs.size() != __rhs.size()) return false; |
| 750 | return __lhs.compare(__rhs) == 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 753 | template<class _CharT, class _Traits, int = 2> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 754 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 755 | bool operator==(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs, |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 756 | basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 757 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 758 | if ( __lhs.size() != __rhs.size()) return false; |
| 759 | return __lhs.compare(__rhs) == 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | |
| 763 | // operator != |
| 764 | template<class _CharT, class _Traits> |
| 765 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 766 | bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 767 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 768 | if ( __lhs.size() != __rhs.size()) |
| 769 | return true; |
| 770 | return __lhs.compare(__rhs) != 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 773 | template<class _CharT, class _Traits, int = 1> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 774 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 775 | bool operator!=(basic_string_view<_CharT, _Traits> __lhs, |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 776 | typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 777 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 778 | if ( __lhs.size() != __rhs.size()) |
| 779 | return true; |
| 780 | return __lhs.compare(__rhs) != 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 783 | template<class _CharT, class _Traits, int = 2> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 784 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 785 | bool operator!=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs, |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 786 | basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 787 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 788 | if ( __lhs.size() != __rhs.size()) |
| 789 | return true; |
| 790 | return __lhs.compare(__rhs) != 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | |
| 794 | // operator < |
| 795 | template<class _CharT, class _Traits> |
| 796 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 797 | bool operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 798 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 799 | return __lhs.compare(__rhs) < 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 802 | template<class _CharT, class _Traits, int = 1> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 803 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 804 | bool operator<(basic_string_view<_CharT, _Traits> __lhs, |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 805 | typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 806 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 807 | return __lhs.compare(__rhs) < 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 810 | template<class _CharT, class _Traits, int = 2> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 811 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 812 | bool operator<(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs, |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 813 | basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 814 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 815 | return __lhs.compare(__rhs) < 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | |
| 819 | // operator > |
| 820 | template<class _CharT, class _Traits> |
| 821 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 822 | bool operator> (basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 823 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 824 | return __lhs.compare(__rhs) > 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 827 | template<class _CharT, class _Traits, int = 1> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 828 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 829 | bool operator>(basic_string_view<_CharT, _Traits> __lhs, |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 830 | typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 831 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 832 | return __lhs.compare(__rhs) > 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 835 | template<class _CharT, class _Traits, int = 2> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 836 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 837 | bool operator>(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs, |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 838 | basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 839 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 840 | return __lhs.compare(__rhs) > 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | |
| 844 | // operator <= |
| 845 | template<class _CharT, class _Traits> |
| 846 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 847 | bool operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 848 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 849 | return __lhs.compare(__rhs) <= 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 852 | template<class _CharT, class _Traits, int = 1> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 853 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 854 | bool operator<=(basic_string_view<_CharT, _Traits> __lhs, |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 855 | typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 856 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 857 | return __lhs.compare(__rhs) <= 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 860 | template<class _CharT, class _Traits, int = 2> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 861 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 862 | bool operator<=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs, |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 863 | basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 864 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 865 | return __lhs.compare(__rhs) <= 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | |
| 869 | // operator >= |
| 870 | template<class _CharT, class _Traits> |
| 871 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 872 | bool operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
| 873 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 874 | return __lhs.compare(__rhs) >= 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 878 | template<class _CharT, class _Traits, int = 1> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 879 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 880 | bool operator>=(basic_string_view<_CharT, _Traits> __lhs, |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 881 | typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 882 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 883 | return __lhs.compare(__rhs) >= 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Joe Loser | 1cf8cae | 2021-11-03 18:45:04 -0400 | [diff] [blame] | 886 | template<class _CharT, class _Traits, int = 2> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 887 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 888 | bool operator>=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs, |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 889 | basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 890 | { |
Marshall Clow | b7db497 | 2017-11-15 20:02:27 +0000 | [diff] [blame] | 891 | return __lhs.compare(__rhs) >= 0; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Eric Fiselier | 1411692 | 2019-09-25 18:56:54 +0000 | [diff] [blame] | 894 | |
| 895 | template<class _CharT, class _Traits> |
| 896 | basic_ostream<_CharT, _Traits>& |
| 897 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 898 | basic_string_view<_CharT, _Traits> __str); |
| 899 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 900 | // [string.view.hash] |
Marshall Clow | ab313df | 2019-06-27 14:18:32 +0000 | [diff] [blame] | 901 | template<class _CharT> |
| 902 | struct _LIBCPP_TEMPLATE_VIS hash<basic_string_view<_CharT, char_traits<_CharT> > > |
| 903 | : public unary_function<basic_string_view<_CharT, char_traits<_CharT> >, size_t> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 904 | { |
Louis Dionne | d83f2e6 | 2018-11-28 15:22:30 +0000 | [diff] [blame] | 905 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | ab313df | 2019-06-27 14:18:32 +0000 | [diff] [blame] | 906 | size_t operator()(const basic_string_view<_CharT, char_traits<_CharT> > __val) const _NOEXCEPT { |
Louis Dionne | d83f2e6 | 2018-11-28 15:22:30 +0000 | [diff] [blame] | 907 | return __do_string_hash(__val.data(), __val.data() + __val.size()); |
| 908 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 909 | }; |
| 910 | |
Marshall Clow | 27c8a4c | 2017-01-09 18:07:34 +0000 | [diff] [blame] | 911 | |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 912 | #if _LIBCPP_STD_VER > 11 |
Marshall Clow | 27c8a4c | 2017-01-09 18:07:34 +0000 | [diff] [blame] | 913 | inline namespace literals |
| 914 | { |
| 915 | inline namespace string_view_literals |
| 916 | { |
| 917 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Marshall Clow | abe6daf | 2017-10-24 14:06:00 +0000 | [diff] [blame] | 918 | basic_string_view<char> operator "" sv(const char *__str, size_t __len) _NOEXCEPT |
Marshall Clow | 27c8a4c | 2017-01-09 18:07:34 +0000 | [diff] [blame] | 919 | { |
| 920 | return basic_string_view<char> (__str, __len); |
| 921 | } |
| 922 | |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 923 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Marshall Clow | 27c8a4c | 2017-01-09 18:07:34 +0000 | [diff] [blame] | 924 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Marshall Clow | abe6daf | 2017-10-24 14:06:00 +0000 | [diff] [blame] | 925 | basic_string_view<wchar_t> operator "" sv(const wchar_t *__str, size_t __len) _NOEXCEPT |
Marshall Clow | 27c8a4c | 2017-01-09 18:07:34 +0000 | [diff] [blame] | 926 | { |
| 927 | return basic_string_view<wchar_t> (__str, __len); |
| 928 | } |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 929 | #endif |
Marshall Clow | 27c8a4c | 2017-01-09 18:07:34 +0000 | [diff] [blame] | 930 | |
Arthur O'Dwyer | afa5d5f | 2021-04-18 21:47:08 -0400 | [diff] [blame] | 931 | #ifndef _LIBCPP_HAS_NO_CHAR8_T |
Marshall Clow | 8732fed | 2018-12-11 04:35:44 +0000 | [diff] [blame] | 932 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 933 | basic_string_view<char8_t> operator "" sv(const char8_t *__str, size_t __len) _NOEXCEPT |
| 934 | { |
| 935 | return basic_string_view<char8_t> (__str, __len); |
| 936 | } |
| 937 | #endif |
| 938 | |
Marshall Clow | 27c8a4c | 2017-01-09 18:07:34 +0000 | [diff] [blame] | 939 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Marshall Clow | abe6daf | 2017-10-24 14:06:00 +0000 | [diff] [blame] | 940 | basic_string_view<char16_t> operator "" sv(const char16_t *__str, size_t __len) _NOEXCEPT |
Marshall Clow | 27c8a4c | 2017-01-09 18:07:34 +0000 | [diff] [blame] | 941 | { |
| 942 | return basic_string_view<char16_t> (__str, __len); |
| 943 | } |
| 944 | |
| 945 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Marshall Clow | abe6daf | 2017-10-24 14:06:00 +0000 | [diff] [blame] | 946 | basic_string_view<char32_t> operator "" sv(const char32_t *__str, size_t __len) _NOEXCEPT |
Marshall Clow | 27c8a4c | 2017-01-09 18:07:34 +0000 | [diff] [blame] | 947 | { |
| 948 | return basic_string_view<char32_t> (__str, __len); |
| 949 | } |
Nikolas Klauser | d26407a | 2021-12-02 14:12:51 +0100 | [diff] [blame] | 950 | } // namespace string_view_literals |
| 951 | } // namespace literals |
Marshall Clow | 27c8a4c | 2017-01-09 18:07:34 +0000 | [diff] [blame] | 952 | #endif |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 953 | _LIBCPP_END_NAMESPACE_STD |
| 954 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 955 | _LIBCPP_POP_MACROS |
| 956 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 957 | #endif // _LIBCPP_STRING_VIEW |