blob: 2630799c06032099166960188f69cb0e45652795 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- string -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
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
12#define _LIBCPP_STRING
13
14/*
15 string synopsis
16
17namespace std
18{
19
20template <class stateT>
21class fpos
22{
23private:
24 stateT st;
25public:
26 fpos(streamoff = streamoff());
27
28 operator streamoff() const;
29
30 stateT state() const;
31 void state(stateT);
32
33 fpos& operator+=(streamoff);
34 fpos operator+ (streamoff) const;
35 fpos& operator-=(streamoff);
36 fpos operator- (streamoff) const;
37};
38
39template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
40
41template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
42template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
43
44template <class charT>
45struct char_traits
46{
47 typedef charT char_type;
48 typedef ... int_type;
49 typedef streamoff off_type;
50 typedef streampos pos_type;
51 typedef mbstate_t state_type;
52
Howard Hinnanta6119a82011-05-29 19:57:12 +000053 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant03d71812012-07-20 19:09:12 +000054 static constexpr bool eq(char_type c1, char_type c2) noexcept;
55 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056
57 static int compare(const char_type* s1, const char_type* s2, size_t n);
58 static size_t length(const char_type* s);
59 static const char_type* find(const char_type* s, size_t n, const char_type& a);
60 static char_type* move(char_type* s1, const char_type* s2, size_t n);
61 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
62 static char_type* assign(char_type* s, size_t n, char_type a);
63
Howard Hinnant03d71812012-07-20 19:09:12 +000064 static constexpr int_type not_eof(int_type c) noexcept;
65 static constexpr char_type to_char_type(int_type c) noexcept;
66 static constexpr int_type to_int_type(char_type c) noexcept;
67 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
68 static constexpr int_type eof() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069};
70
71template <> struct char_traits<char>;
72template <> struct char_traits<wchar_t>;
73
Howard Hinnant324bb032010-08-22 00:02:43 +000074template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075class basic_string
76{
Howard Hinnant324bb032010-08-22 00:02:43 +000077public:
78// types:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000079 typedef traits traits_type;
80 typedef typename traits_type::char_type value_type;
81 typedef Allocator allocator_type;
82 typedef typename allocator_type::size_type size_type;
83 typedef typename allocator_type::difference_type difference_type;
84 typedef typename allocator_type::reference reference;
85 typedef typename allocator_type::const_reference const_reference;
86 typedef typename allocator_type::pointer pointer;
87 typedef typename allocator_type::const_pointer const_pointer;
88 typedef implementation-defined iterator;
89 typedef implementation-defined const_iterator;
90 typedef std::reverse_iterator<iterator> reverse_iterator;
91 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
92
93 static const size_type npos = -1;
94
Howard Hinnant53f7d4c2011-06-03 18:40:47 +000095 basic_string()
96 noexcept(is_nothrow_default_constructible<allocator_type>::value);
97 explicit basic_string(const allocator_type& a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098 basic_string(const basic_string& str);
Howard Hinnant53f7d4c2011-06-03 18:40:47 +000099 basic_string(basic_string&& str)
100 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000101 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000102 const allocator_type& a = allocator_type());
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000104 const Allocator& a = Allocator());
Marshall Clowdb7fa112016-11-14 18:22:19 +0000105 template<class T>
106 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000107 explicit basic_string(const basic_string_view<charT, traits> sv, const Allocator& a = Allocator());
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000108 basic_string(const value_type* s, const allocator_type& a = allocator_type());
109 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000110 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
111 template<class InputIterator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000112 basic_string(InputIterator begin, InputIterator end,
113 const allocator_type& a = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000114 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
115 basic_string(const basic_string&, const Allocator&);
116 basic_string(basic_string&&, const Allocator&);
117
118 ~basic_string();
119
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000120 operator basic_string_view<charT, traits>() const noexcept;
121
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000122 basic_string& operator=(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000123 basic_string& operator=(basic_string_view<charT, traits> sv);
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000124 basic_string& operator=(basic_string&& str)
125 noexcept(
Marshall Clowaf961ed2015-08-18 18:57:00 +0000126 allocator_type::propagate_on_container_move_assignment::value ||
127 allocator_type::is_always_equal::value ); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000128 basic_string& operator=(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129 basic_string& operator=(value_type c);
130 basic_string& operator=(initializer_list<value_type>);
131
Howard Hinnanta6119a82011-05-29 19:57:12 +0000132 iterator begin() noexcept;
133 const_iterator begin() const noexcept;
134 iterator end() noexcept;
135 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136
Howard Hinnanta6119a82011-05-29 19:57:12 +0000137 reverse_iterator rbegin() noexcept;
138 const_reverse_iterator rbegin() const noexcept;
139 reverse_iterator rend() noexcept;
140 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141
Howard Hinnanta6119a82011-05-29 19:57:12 +0000142 const_iterator cbegin() const noexcept;
143 const_iterator cend() const noexcept;
144 const_reverse_iterator crbegin() const noexcept;
145 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000146
Howard Hinnanta6119a82011-05-29 19:57:12 +0000147 size_type size() const noexcept;
148 size_type length() const noexcept;
149 size_type max_size() const noexcept;
150 size_type capacity() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000151
152 void resize(size_type n, value_type c);
153 void resize(size_type n);
154
155 void reserve(size_type res_arg = 0);
156 void shrink_to_fit();
Howard Hinnanta6119a82011-05-29 19:57:12 +0000157 void clear() noexcept;
158 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159
160 const_reference operator[](size_type pos) const;
161 reference operator[](size_type pos);
162
163 const_reference at(size_type n) const;
164 reference at(size_type n);
165
166 basic_string& operator+=(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000167 basic_string& operator+=(basic_string_view<charT, traits> sv);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000168 basic_string& operator+=(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000169 basic_string& operator+=(value_type c);
170 basic_string& operator+=(initializer_list<value_type>);
171
172 basic_string& append(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000173 basic_string& append(basic_string_view<charT, traits> sv);
Marshall Clowa93b5e22014-03-04 19:17:19 +0000174 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000175 template <class T>
176 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000177 basic_string& append(const value_type* s, size_type n);
178 basic_string& append(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179 basic_string& append(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000180 template<class InputIterator>
181 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182 basic_string& append(initializer_list<value_type>);
183
184 void push_back(value_type c);
185 void pop_back();
186 reference front();
187 const_reference front() const;
188 reference back();
189 const_reference back() const;
190
191 basic_string& assign(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000192 basic_string& assign(basic_string_view<charT, traits> sv);
Howard Hinnanta6119a82011-05-29 19:57:12 +0000193 basic_string& assign(basic_string&& str);
Marshall Clowa93b5e22014-03-04 19:17:19 +0000194 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000195 template <class T>
196 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000197 basic_string& assign(const value_type* s, size_type n);
198 basic_string& assign(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199 basic_string& assign(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000200 template<class InputIterator>
201 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202 basic_string& assign(initializer_list<value_type>);
203
204 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000205 basic_string& insert(size_type pos1, basic_string_view<charT, traits> sv);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000206 basic_string& insert(size_type pos1, const basic_string& str,
207 size_type pos2, size_type n);
Marshall Clow6ac8de02016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clowa93b5e22014-03-04 19:17:19 +0000210 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000211 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000212 basic_string& insert(size_type pos, size_type n, value_type c);
213 iterator insert(const_iterator p, value_type c);
214 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000215 template<class InputIterator>
216 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217 iterator insert(const_iterator p, initializer_list<value_type>);
218
219 basic_string& erase(size_type pos = 0, size_type n = npos);
220 iterator erase(const_iterator position);
221 iterator erase(const_iterator first, const_iterator last);
222
223 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000224 basic_string& replace(size_type pos1, size_type n1, basic_string_view<charT, traits> sv);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000225 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clowa93b5e22014-03-04 19:17:19 +0000226 size_type pos2, size_type n2=npos); // C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000227 template <class T>
228 basic_string& replace(size_type pos1, size_type n1, const T& t,
229 size_type pos2, size_type n); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000230 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
231 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000232 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000233 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000234 basic_string& replace(const_iterator i1, const_iterator i2, basic_string_view<charT, traits> sv);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000235 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
236 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000237 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000238 template<class InputIterator>
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000239 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
240 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000241
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000242 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243 basic_string substr(size_type pos = 0, size_type n = npos) const;
244
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000245 void swap(basic_string& str)
Marshall Clow7d914d12015-07-13 20:04:56 +0000246 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
247 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000248
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000249 const value_type* c_str() const noexcept;
250 const value_type* data() const noexcept;
Marshall Clowf532a702016-03-08 15:44:30 +0000251 value_type* data() noexcept; // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252
Howard Hinnanta6119a82011-05-29 19:57:12 +0000253 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000254
Howard Hinnanta6119a82011-05-29 19:57:12 +0000255 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000256 size_type find(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000257 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
258 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000259 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260
Howard Hinnanta6119a82011-05-29 19:57:12 +0000261 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clow0b9db072017-09-07 04:19:32 +0000262 size_type rfind(basic_string_view<charT, traits> sv, size_type pos = npos) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000263 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
264 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000265 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266
Howard Hinnanta6119a82011-05-29 19:57:12 +0000267 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000268 size_type find_first_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000269 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
270 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000271 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000272
Howard Hinnanta6119a82011-05-29 19:57:12 +0000273 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clow0b9db072017-09-07 04:19:32 +0000274 size_type find_last_of(basic_string_view<charT, traits> sv, size_type pos = npos) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000275 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
276 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000277 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278
Howard Hinnanta6119a82011-05-29 19:57:12 +0000279 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000280 size_type find_first_not_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000281 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000283 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284
Howard Hinnanta6119a82011-05-29 19:57:12 +0000285 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clow0b9db072017-09-07 04:19:32 +0000286 size_type find_last_not_of(basic_string_view<charT, traits> sv, size_type pos = npos) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000287 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
288 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000289 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290
Howard Hinnanta6119a82011-05-29 19:57:12 +0000291 int compare(const basic_string& str) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000292 int compare(basic_string_view<charT, traits> sv) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000293 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000294 int compare(size_type pos1, size_type n1, basic_string_view<charT, traits> sv) const;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000295 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clowa93b5e22014-03-04 19:17:19 +0000296 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000297 template <class T>
298 int compare(size_type pos1, size_type n1, const T& t,
299 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000300 int compare(const value_type* s) const noexcept;
301 int compare(size_type pos1, size_type n1, const value_type* s) const;
302 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000303
Marshall Clow46b4ad52017-12-04 20:11:38 +0000304 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++2a
305 bool starts_with(charT c) const noexcept; // C++2a
306 bool starts_with(const charT* s) const; // C++2a
307 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++2a
308 bool ends_with(charT c) const noexcept; // C++2a
309 bool ends_with(const charT* s) const; // C++2a
310
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311 bool __invariants() const;
312};
313
Marshall Clow5b1e87e2018-02-08 06:34:03 +0000314template<class InputIterator,
315 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
316basic_string(InputIterator, InputIterator, Allocator = Allocator())
317 -> basic_string<typename iterator_traits<InputIterator>::value_type,
318 char_traits<typename iterator_traits<InputIterator>::value_type>,
319 Allocator>; // C++17
320
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321template<class charT, class traits, class Allocator>
322basic_string<charT, traits, Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000323operator+(const basic_string<charT, traits, Allocator>& lhs,
324 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000325
326template<class charT, class traits, class Allocator>
327basic_string<charT, traits, Allocator>
328operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
329
330template<class charT, class traits, class Allocator>
331basic_string<charT, traits, Allocator>
332operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
333
334template<class charT, class traits, class Allocator>
335basic_string<charT, traits, Allocator>
336operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
337
338template<class charT, class traits, class Allocator>
339basic_string<charT, traits, Allocator>
340operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
341
342template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000343bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000344 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345
346template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000347bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000348
349template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000350bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351
Howard Hinnant324bb032010-08-22 00:02:43 +0000352template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000353bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000354 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000355
356template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000357bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000358
359template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000360bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361
362template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000363bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000364 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000365
366template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000367bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368
369template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000370bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371
372template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000373bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000374 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375
376template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000377bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000378
379template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000380bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381
382template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000383bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000384 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385
386template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000387bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388
389template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000390bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391
392template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000393bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000394 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395
396template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000397bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000398
399template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000400bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401
402template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000403void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000404 basic_string<charT, traits, Allocator>& rhs)
405 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
408basic_istream<charT, traits>&
409operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
410
411template<class charT, class traits, class Allocator>
412basic_ostream<charT, traits>&
413operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
414
415template<class charT, class traits, class Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000416basic_istream<charT, traits>&
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000417getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
418 charT delim);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
421basic_istream<charT, traits>&
422getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
423
424typedef basic_string<char> string;
425typedef basic_string<wchar_t> wstring;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000426typedef basic_string<char16_t> u16string;
427typedef basic_string<char32_t> u32string;
428
429int stoi (const string& str, size_t* idx = 0, int base = 10);
430long stol (const string& str, size_t* idx = 0, int base = 10);
431unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
432long long stoll (const string& str, size_t* idx = 0, int base = 10);
433unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10);
434
435float stof (const string& str, size_t* idx = 0);
436double stod (const string& str, size_t* idx = 0);
437long double stold(const string& str, size_t* idx = 0);
438
439string to_string(int val);
440string to_string(unsigned val);
441string to_string(long val);
442string to_string(unsigned long val);
443string to_string(long long val);
444string to_string(unsigned long long val);
445string to_string(float val);
446string to_string(double val);
447string to_string(long double val);
448
449int stoi (const wstring& str, size_t* idx = 0, int base = 10);
450long stol (const wstring& str, size_t* idx = 0, int base = 10);
451unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
452long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
453unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10);
454
455float stof (const wstring& str, size_t* idx = 0);
456double stod (const wstring& str, size_t* idx = 0);
457long double stold(const wstring& str, size_t* idx = 0);
458
459wstring to_wstring(int val);
460wstring to_wstring(unsigned val);
461wstring to_wstring(long val);
462wstring to_wstring(unsigned long val);
463wstring to_wstring(long long val);
464wstring to_wstring(unsigned long long val);
465wstring to_wstring(float val);
466wstring to_wstring(double val);
467wstring to_wstring(long double val);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000468
469template <> struct hash<string>;
470template <> struct hash<u16string>;
471template <> struct hash<u32string>;
472template <> struct hash<wstring>;
473
Marshall Clow15234322013-07-23 17:05:24 +0000474basic_string<char> operator "" s( const char *str, size_t len ); // C++14
475basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
476basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
477basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
478
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000479} // std
480
481*/
482
483#include <__config>
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000484#include <string_view>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485#include <iosfwd>
486#include <cstring>
Howard Hinnantadff4892010-05-24 17:49:41 +0000487#include <cstdio> // For EOF.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000488#include <cwchar>
489#include <algorithm>
490#include <iterator>
491#include <utility>
492#include <memory>
493#include <stdexcept>
494#include <type_traits>
495#include <initializer_list>
496#include <__functional_base>
497#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
498#include <cstdint>
499#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000500
Eric Fiselierb9536102014-08-10 23:53:08 +0000501#include <__debug>
502
Howard Hinnant08e17472011-10-17 20:05:10 +0000503#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000504#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000505#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000506
Eric Fiselier018a3d52017-05-31 22:07:49 +0000507_LIBCPP_PUSH_MACROS
508#include <__undef_macros>
509
510
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000511_LIBCPP_BEGIN_NAMESPACE_STD
512
513// fpos
514
515template <class _StateT>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000516class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000517{
518private:
519 _StateT __st_;
520 streamoff __off_;
521public:
522 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
523
524 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
525
526 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
527 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
528
529 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
530 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
531 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
532 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
533};
534
535template <class _StateT>
536inline _LIBCPP_INLINE_VISIBILITY
537streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
538 {return streamoff(__x) - streamoff(__y);}
539
540template <class _StateT>
541inline _LIBCPP_INLINE_VISIBILITY
542bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
543 {return streamoff(__x) == streamoff(__y);}
544
545template <class _StateT>
546inline _LIBCPP_INLINE_VISIBILITY
547bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
548 {return streamoff(__x) != streamoff(__y);}
549
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000550// basic_string
551
552template<class _CharT, class _Traits, class _Allocator>
553basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000554operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
555 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556
557template<class _CharT, class _Traits, class _Allocator>
558basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000559operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560
561template<class _CharT, class _Traits, class _Allocator>
562basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000563operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000564
565template<class _CharT, class _Traits, class _Allocator>
566basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000567operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000568
569template<class _CharT, class _Traits, class _Allocator>
570basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000571operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572
Shoaib Meenai487562f2017-07-29 02:54:41 +0000573_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
574
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000575template <bool>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000576class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000577{
578protected:
Marshall Clow14c09a22016-08-25 15:09:01 +0000579 _LIBCPP_NORETURN void __throw_length_error() const;
580 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000581};
582
583template <bool __b>
584void
585__basic_string_common<__b>::__throw_length_error() const
586{
Marshall Clow14c09a22016-08-25 15:09:01 +0000587 _VSTD::__throw_length_error("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000588}
589
590template <bool __b>
591void
592__basic_string_common<__b>::__throw_out_of_range() const
593{
Marshall Clow14c09a22016-08-25 15:09:01 +0000594 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595}
596
Eric Fiselier833d6442016-09-15 22:27:07 +0000597_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598
Marshall Clowdf9db312016-01-13 21:54:34 +0000599#ifdef _LIBCPP_NO_EXCEPTIONS
600template <class _Iter>
601struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
602#elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
603template <class _Iter>
604struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
605#else
606template <class _Iter, bool = __is_forward_iterator<_Iter>::value>
607struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
608 noexcept(++(declval<_Iter&>())) &&
609 is_nothrow_assignable<_Iter&, _Iter>::value &&
610 noexcept(declval<_Iter>() == declval<_Iter>()) &&
611 noexcept(*declval<_Iter>())
612)) {};
613
614template <class _Iter>
615struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
616#endif
617
618
619template <class _Iter>
620struct __libcpp_string_gets_noexcept_iterator
621 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
622
Marshall Clow6ac8de02016-09-24 22:45:42 +0000623template <class _CharT, class _Traits, class _Tp>
624struct __can_be_converted_to_string_view : public _LIBCPP_BOOL_CONSTANT(
Marshall Clowf1729d92017-11-15 20:02:27 +0000625 ( is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
Marshall Clow6ac8de02016-09-24 22:45:42 +0000626 !is_convertible<const _Tp&, const _CharT*>::value)) {};
627
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000628#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000629
630template <class _CharT, size_t = sizeof(_CharT)>
631struct __padding
632{
633 unsigned char __xx[sizeof(_CharT)-1];
634};
635
636template <class _CharT>
637struct __padding<_CharT, 1>
638{
639};
640
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000641#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000642
Howard Hinnant324bb032010-08-22 00:02:43 +0000643template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000644class _LIBCPP_TEMPLATE_VIS basic_string
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000645 : private __basic_string_common<true>
646{
647public:
648 typedef basic_string __self;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000649 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000650 typedef _Traits traits_type;
Marshall Clow2d4c3fa2017-03-15 18:41:11 +0000651 typedef _CharT value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000652 typedef _Allocator allocator_type;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000653 typedef allocator_traits<allocator_type> __alloc_traits;
654 typedef typename __alloc_traits::size_type size_type;
655 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000656 typedef value_type& reference;
657 typedef const value_type& const_reference;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000658 typedef typename __alloc_traits::pointer pointer;
659 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660
Marshall Clow46ea17e2018-01-22 00:17:48 +0000661 static_assert(is_trivial<value_type>::value, "Character type of basic_string must be trivial");
Marshall Clow2d4c3fa2017-03-15 18:41:11 +0000662 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant499cea12013-08-23 17:37:05 +0000663 "traits_type::char_type must be the same type as CharT");
664 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
665 "Allocator::value_type must be same type as value_type");
666#if defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667 typedef pointer iterator;
668 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000669#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000670 typedef __wrap_iter<pointer> iterator;
671 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000672#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000673 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
674 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000675
676private:
Howard Hinnant15467182013-04-30 21:44:48 +0000677
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000678#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000679
680 struct __long
681 {
682 pointer __data_;
683 size_type __size_;
684 size_type __cap_;
685 };
686
Eric Fiselier5ccf0432017-10-17 13:16:01 +0000687#ifdef _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000688 static const size_type __short_mask = 0x01;
689 static const size_type __long_mask = 0x1ul;
Howard Hinnant15467182013-04-30 21:44:48 +0000690#else // _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000691 static const size_type __short_mask = 0x80;
692 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant15467182013-04-30 21:44:48 +0000693#endif // _LIBCPP_BIG_ENDIAN
694
695 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
696 (sizeof(__long) - 1)/sizeof(value_type) : 2};
697
698 struct __short
699 {
700 value_type __data_[__min_cap];
701 struct
702 : __padding<value_type>
703 {
704 unsigned char __size_;
705 };
706 };
707
708#else
709
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710 struct __long
711 {
712 size_type __cap_;
713 size_type __size_;
714 pointer __data_;
715 };
716
Eric Fiselier5ccf0432017-10-17 13:16:01 +0000717#ifdef _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000718 static const size_type __short_mask = 0x80;
719 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant324bb032010-08-22 00:02:43 +0000720#else // _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000721 static const size_type __short_mask = 0x01;
722 static const size_type __long_mask = 0x1ul;
Howard Hinnant324bb032010-08-22 00:02:43 +0000723#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000724
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
726 (sizeof(__long) - 1)/sizeof(value_type) : 2};
727
728 struct __short
729 {
730 union
731 {
732 unsigned char __size_;
Howard Hinnant9c0df142012-10-30 19:06:59 +0000733 value_type __lx;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000734 };
735 value_type __data_[__min_cap];
736 };
737
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000738#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000739
Howard Hinnant499cea12013-08-23 17:37:05 +0000740 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000741
Howard Hinnant499cea12013-08-23 17:37:05 +0000742 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743
744 struct __raw
745 {
746 size_type __words[__n_words];
747 };
748
749 struct __rep
750 {
751 union
752 {
753 __long __l;
754 __short __s;
755 __raw __r;
756 };
757 };
758
759 __compressed_pair<__rep, allocator_type> __r_;
760
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000761public:
762 static const size_type npos = -1;
763
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000764 _LIBCPP_INLINE_VISIBILITY basic_string()
765 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000766
767 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
768#if _LIBCPP_STD_VER <= 14
769 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
770#else
771 _NOEXCEPT;
772#endif
773
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000774 basic_string(const basic_string& __str);
775 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clow7b193f72015-06-03 19:56:43 +0000776
Eric Fiselier3e928972017-04-19 00:28:44 +0000777#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant9f193f22011-01-26 00:06:59 +0000778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000779 basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +0000780#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000781 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000782#else
783 _NOEXCEPT;
784#endif
785
Howard Hinnant9f193f22011-01-26 00:06:59 +0000786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000787 basic_string(basic_string&& __str, const allocator_type& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000788#endif // _LIBCPP_CXX03_LANG
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000789 _LIBCPP_INLINE_VISIBILITY basic_string(const _CharT* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000790 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000791 basic_string(const _CharT* __s, const _Allocator& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000792 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000793 basic_string(const _CharT* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000794 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000795 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000796 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000797 basic_string(size_type __n, _CharT __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000798 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000799 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000800 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000801 const _Allocator& __a = _Allocator());
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000802 _LIBCPP_INLINE_VISIBILITY
803 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000804 const _Allocator& __a = _Allocator());
Marshall Clowdb7fa112016-11-14 18:22:19 +0000805 template<class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000806 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000807 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Marshall Clowdb7fa112016-11-14 18:22:19 +0000808 const allocator_type& __a = allocator_type(),
809 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type* = 0);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000810 _LIBCPP_INLINE_VISIBILITY explicit
811 basic_string(__self_view __sv);
812 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000813 basic_string(__self_view __sv, const _Allocator& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000814 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000816 basic_string(_InputIterator __first, _InputIterator __last);
817 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000820#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000821 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000822 basic_string(initializer_list<_CharT> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000823 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000824 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000825#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000826
Eric Fiselier51eb1d52016-10-31 03:42:50 +0000827 inline ~basic_string();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000828
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000829 _LIBCPP_INLINE_VISIBILITY
830 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
831
Howard Hinnante32b5e22010-11-17 17:55:08 +0000832 basic_string& operator=(const basic_string& __str);
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000833
834#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier37b2be92017-01-17 22:10:32 +0000835 template <class = void>
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000836#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000837 _LIBCPP_INLINE_VISIBILITY
838 basic_string& operator=(__self_view __sv) {return assign(__sv);}
Eric Fiselier3e928972017-04-19 00:28:44 +0000839#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000841 basic_string& operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +0000842 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselier3e928972017-04-19 00:28:44 +0000843 _LIBCPP_INLINE_VISIBILITY
844 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000845#endif
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000846 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000847 basic_string& operator=(value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000848
Howard Hinnant499cea12013-08-23 17:37:05 +0000849#if _LIBCPP_DEBUG_LEVEL >= 2
850 _LIBCPP_INLINE_VISIBILITY
851 iterator begin() _NOEXCEPT
852 {return iterator(this, __get_pointer());}
853 _LIBCPP_INLINE_VISIBILITY
854 const_iterator begin() const _NOEXCEPT
855 {return const_iterator(this, __get_pointer());}
856 _LIBCPP_INLINE_VISIBILITY
857 iterator end() _NOEXCEPT
858 {return iterator(this, __get_pointer() + size());}
859 _LIBCPP_INLINE_VISIBILITY
860 const_iterator end() const _NOEXCEPT
861 {return const_iterator(this, __get_pointer() + size());}
862#else
Howard Hinnanta6119a82011-05-29 19:57:12 +0000863 _LIBCPP_INLINE_VISIBILITY
864 iterator begin() _NOEXCEPT
865 {return iterator(__get_pointer());}
866 _LIBCPP_INLINE_VISIBILITY
867 const_iterator begin() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000868 {return const_iterator(__get_pointer());}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000869 _LIBCPP_INLINE_VISIBILITY
870 iterator end() _NOEXCEPT
871 {return iterator(__get_pointer() + size());}
872 _LIBCPP_INLINE_VISIBILITY
873 const_iterator end() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000874 {return const_iterator(__get_pointer() + size());}
Howard Hinnant499cea12013-08-23 17:37:05 +0000875#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnanta6119a82011-05-29 19:57:12 +0000876 _LIBCPP_INLINE_VISIBILITY
877 reverse_iterator rbegin() _NOEXCEPT
878 {return reverse_iterator(end());}
879 _LIBCPP_INLINE_VISIBILITY
880 const_reverse_iterator rbegin() const _NOEXCEPT
881 {return const_reverse_iterator(end());}
882 _LIBCPP_INLINE_VISIBILITY
883 reverse_iterator rend() _NOEXCEPT
884 {return reverse_iterator(begin());}
885 _LIBCPP_INLINE_VISIBILITY
886 const_reverse_iterator rend() const _NOEXCEPT
887 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000888
Howard Hinnanta6119a82011-05-29 19:57:12 +0000889 _LIBCPP_INLINE_VISIBILITY
890 const_iterator cbegin() const _NOEXCEPT
891 {return begin();}
892 _LIBCPP_INLINE_VISIBILITY
893 const_iterator cend() const _NOEXCEPT
894 {return end();}
895 _LIBCPP_INLINE_VISIBILITY
896 const_reverse_iterator crbegin() const _NOEXCEPT
897 {return rbegin();}
898 _LIBCPP_INLINE_VISIBILITY
899 const_reverse_iterator crend() const _NOEXCEPT
900 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000901
Howard Hinnanta6119a82011-05-29 19:57:12 +0000902 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000903 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000904 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
905 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
906 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +0000907 {return (__is_long() ? __get_long_cap()
908 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909
910 void resize(size_type __n, value_type __c);
911 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
912
Eric Fiselier59e24fe2017-06-01 02:29:37 +0000913 void reserve(size_type __res_arg = 0);
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000915 void shrink_to_fit() _NOEXCEPT {reserve();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +0000917 void clear() _NOEXCEPT;
Marshall Clowf1729d92017-11-15 20:02:27 +0000918 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
919 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000920
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000921 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
922 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000923
924 const_reference at(size_type __n) const;
925 reference at(size_type __n);
926
927 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000928 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(__self_view __sv) {return append(__sv);}
929 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000930 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselier3e928972017-04-19 00:28:44 +0000931#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000932 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Eric Fiselier3e928972017-04-19 00:28:44 +0000933#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000935 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936 basic_string& append(const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000937 _LIBCPP_INLINE_VISIBILITY
938 basic_string& append(__self_view __sv) { return append(__sv.data(), __sv.size()); }
Marshall Clowa93b5e22014-03-04 19:17:19 +0000939 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +0000940 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000941 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
942 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +0000943 <
944 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
945 basic_string&
946 >::type
947 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000948 basic_string& append(const value_type* __s, size_type __n);
949 basic_string& append(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000950 basic_string& append(size_type __n, value_type __c);
Eric Fiselier026d38e2016-10-31 02:46:25 +0000951 template <class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000952 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
953 basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000954 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000955 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
956 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000957 <
Marshall Clowdf9db312016-01-13 21:54:34 +0000958 __is_exactly_input_iterator<_InputIterator>::value
959 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000960 basic_string&
961 >::type
Eric Fiselier026d38e2016-10-31 02:46:25 +0000962 _LIBCPP_INLINE_VISIBILITY
963 append(_InputIterator __first, _InputIterator __last) {
964 const basic_string __temp (__first, __last, __alloc());
965 append(__temp.data(), __temp.size());
966 return *this;
967 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000969 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
970 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971 <
Marshall Clowdf9db312016-01-13 21:54:34 +0000972 __is_forward_iterator<_ForwardIterator>::value
973 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974 basic_string&
975 >::type
Eric Fiselier026d38e2016-10-31 02:46:25 +0000976 _LIBCPP_INLINE_VISIBILITY
977 append(_ForwardIterator __first, _ForwardIterator __last) {
978 return __append_forward_unsafe(__first, __last);
979 }
980
Eric Fiselier3e928972017-04-19 00:28:44 +0000981#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000983 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Eric Fiselier3e928972017-04-19 00:28:44 +0000984#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985
986 void push_back(value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988 void pop_back();
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000989 _LIBCPP_INLINE_VISIBILITY reference front();
990 _LIBCPP_INLINE_VISIBILITY const_reference front() const;
991 _LIBCPP_INLINE_VISIBILITY reference back();
992 _LIBCPP_INLINE_VISIBILITY const_reference back() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000993
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000994 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000995 basic_string& assign(__self_view __sv) { return assign(__sv.data(), __sv.size()); }
996 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf40ec902016-03-09 18:08:29 +0000997 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselier3e928972017-04-19 00:28:44 +0000998#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta6119a82011-05-29 19:57:12 +0000999 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier59e24fe2017-06-01 02:29:37 +00001000 basic_string& assign(basic_string&& __str)
Marshall Clow7ed093b2015-10-05 16:17:34 +00001001 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselier59e24fe2017-06-01 02:29:37 +00001002 {*this = _VSTD::move(__str); return *this;}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001003#endif
Marshall Clowa93b5e22014-03-04 19:17:19 +00001004 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +00001005 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001006 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1007 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001008 <
1009 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1010 basic_string&
1011 >::type
Eric Fiselier59e24fe2017-06-01 02:29:37 +00001012 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001013 basic_string& assign(const value_type* __s, size_type __n);
1014 basic_string& assign(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001015 basic_string& assign(size_type __n, value_type __c);
1016 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001017 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1018 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001020 __is_exactly_input_iterator<_InputIterator>::value
1021 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001022 basic_string&
1023 >::type
1024 assign(_InputIterator __first, _InputIterator __last);
1025 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001026 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1027 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001028 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001029 __is_forward_iterator<_ForwardIterator>::value
1030 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031 basic_string&
1032 >::type
1033 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselier3e928972017-04-19 00:28:44 +00001034#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001036 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001037#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001038
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001040 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001041 _LIBCPP_INLINE_VISIBILITY
1042 basic_string& insert(size_type __pos1, __self_view __sv) { return insert(__pos1, __sv.data(), __sv.size()); }
Marshall Clow6ac8de02016-09-24 22:45:42 +00001043 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001044 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1045 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001046 <
1047 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1048 basic_string&
1049 >::type
1050 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clowa93b5e22014-03-04 19:17:19 +00001051 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001052 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1053 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001054 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1055 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001056 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1058 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001059 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1060 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001061 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001062 __is_exactly_input_iterator<_InputIterator>::value
1063 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001064 iterator
1065 >::type
1066 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1067 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001068 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1069 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001070 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001071 __is_forward_iterator<_ForwardIterator>::value
1072 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001073 iterator
1074 >::type
1075 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselier3e928972017-04-19 00:28:44 +00001076#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001077 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001078 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1079 {return insert(__pos, __il.begin(), __il.end());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001080#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001081
1082 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001083 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001084 iterator erase(const_iterator __pos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001085 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001086 iterator erase(const_iterator __first, const_iterator __last);
1087
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001088 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001089 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001090 _LIBCPP_INLINE_VISIBILITY
1091 basic_string& replace(size_type __pos1, size_type __n1, __self_view __sv) { return replace(__pos1, __n1, __sv.data(), __sv.size()); }
Marshall Clowa93b5e22014-03-04 19:17:19 +00001092 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos);
Marshall Clow6ac8de02016-09-24 22:45:42 +00001093 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001094 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1095 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001096 <
1097 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1098 basic_string&
1099 >::type
1100 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001101 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1102 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001103 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001104 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001105 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001106 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001107 basic_string& replace(const_iterator __i1, const_iterator __i2, __self_view __sv) { return replace(__i1 - begin(), __i2 - __i1, __sv); }
1108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001109 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001110 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001111 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001113 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001114 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001115 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1116 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001117 <
1118 __is_input_iterator<_InputIterator>::value,
1119 basic_string&
1120 >::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001121 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselier3e928972017-04-19 00:28:44 +00001122#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001124 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001125 {return replace(__i1, __i2, __il.begin(), __il.end());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001126#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001127
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001128 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001130 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1131
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001133 void swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00001134#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00001135 _NOEXCEPT_DEBUG;
Marshall Clow7d914d12015-07-13 20:04:56 +00001136#else
Eric Fiselier47257c42016-12-28 05:53:01 +00001137 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00001138 __is_nothrow_swappable<allocator_type>::value);
1139#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001140
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001142 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001143 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001144 const value_type* data() const _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
Eric Fiselier10bebe22017-11-20 20:23:27 +00001145#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowf532a702016-03-08 15:44:30 +00001146 _LIBCPP_INLINE_VISIBILITY
1147 value_type* data() _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
1148#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001149
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001151 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001152
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001154 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001155 _LIBCPP_INLINE_VISIBILITY
1156 size_type find(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001157 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001159 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001160 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001161
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001163 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001164 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0b9db072017-09-07 04:19:32 +00001165 size_type rfind(__self_view __sv, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001166 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001168 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001169 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001170
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001172 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001173 _LIBCPP_INLINE_VISIBILITY
1174 size_type find_first_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001175 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001177 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001179 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001180
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001182 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001183 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0b9db072017-09-07 04:19:32 +00001184 size_type find_last_of(__self_view __sv, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001185 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001187 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001189 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001190
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001192 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001193 _LIBCPP_INLINE_VISIBILITY
1194 size_type find_first_not_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001195 size_type find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001197 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001198 _LIBCPP_INLINE_VISIBILITY
1199 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1200
1201 _LIBCPP_INLINE_VISIBILITY
1202 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001203 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0b9db072017-09-07 04:19:32 +00001204 size_type find_last_not_of(__self_view __sv, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001205 size_type find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001207 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001208 _LIBCPP_INLINE_VISIBILITY
1209 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1210
1211 _LIBCPP_INLINE_VISIBILITY
1212 int compare(const basic_string& __str) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001213 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001214 int compare(__self_view __sv) const _NOEXCEPT;
1215 _LIBCPP_INLINE_VISIBILITY
1216 int compare(size_type __pos1, size_type __n1, __self_view __sv) const;
1217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001218 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clowa93b5e22014-03-04 19:17:19 +00001219 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos) const;
Marshall Clow6ac8de02016-09-24 22:45:42 +00001220 template <class _Tp>
Eric Fiselier9dbc0532016-10-14 05:29:46 +00001221 inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow6ac8de02016-09-24 22:45:42 +00001222 typename enable_if
1223 <
1224 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1225 int
1226 >::type
1227 compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos) const;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001228 int compare(const value_type* __s) const _NOEXCEPT;
1229 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1230 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001231
Marshall Clow46b4ad52017-12-04 20:11:38 +00001232#if _LIBCPP_STD_VER > 17
1233 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1234 bool starts_with(__self_view __sv) const _NOEXCEPT
1235 { return __self_view(data(), size()).starts_with(__sv); }
1236
1237 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1238 bool starts_with(value_type __c) const _NOEXCEPT
1239 { return !empty() && _Traits::eq(front(), __c); }
1240
1241 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1242 bool starts_with(const value_type* __s) const _NOEXCEPT
1243 { return starts_with(__self_view(__s)); }
1244
1245 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1246 bool ends_with(__self_view __sv) const _NOEXCEPT
1247 { return __self_view(data(), size()).ends_with( __sv); }
1248
1249 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1250 bool ends_with(value_type __c) const _NOEXCEPT
1251 { return !empty() && _Traits::eq(back(), __c); }
1252
1253 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1254 bool ends_with(const value_type* __s) const _NOEXCEPT
1255 { return ends_with(__self_view(__s)); }
1256#endif
1257
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001258 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnant08dd2532013-04-22 23:55:13 +00001259
Vedant Kumar2b588cb2018-03-08 21:15:26 +00001260 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink();
1261
Howard Hinnant08dd2532013-04-22 23:55:13 +00001262 _LIBCPP_INLINE_VISIBILITY
1263 bool __is_long() const _NOEXCEPT
1264 {return bool(__r_.first().__s.__size_ & __short_mask);}
1265
Howard Hinnant499cea12013-08-23 17:37:05 +00001266#if _LIBCPP_DEBUG_LEVEL >= 2
1267
1268 bool __dereferenceable(const const_iterator* __i) const;
1269 bool __decrementable(const const_iterator* __i) const;
1270 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1271 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1272
1273#endif // _LIBCPP_DEBUG_LEVEL >= 2
1274
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001275private:
Howard Hinnanta6119a82011-05-29 19:57:12 +00001276 _LIBCPP_INLINE_VISIBILITY
1277 allocator_type& __alloc() _NOEXCEPT
1278 {return __r_.second();}
1279 _LIBCPP_INLINE_VISIBILITY
1280 const allocator_type& __alloc() const _NOEXCEPT
1281 {return __r_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001282
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001283#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001284
Howard Hinnanta6119a82011-05-29 19:57:12 +00001285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001286 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001287# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001288 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant15467182013-04-30 21:44:48 +00001289# else
1290 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1291# endif
1292
Howard Hinnanta6119a82011-05-29 19:57:12 +00001293 _LIBCPP_INLINE_VISIBILITY
1294 size_type __get_short_size() const _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001295# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001296 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant15467182013-04-30 21:44:48 +00001297# else
1298 {return __r_.first().__s.__size_;}
1299# endif
1300
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001301#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001302
1303 _LIBCPP_INLINE_VISIBILITY
1304 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001305# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant15467182013-04-30 21:44:48 +00001306 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1307# else
1308 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1309# endif
1310
1311 _LIBCPP_INLINE_VISIBILITY
1312 size_type __get_short_size() const _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001313# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant15467182013-04-30 21:44:48 +00001314 {return __r_.first().__s.__size_;}
1315# else
1316 {return __r_.first().__s.__size_ >> 1;}
1317# endif
1318
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001319#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001320
Howard Hinnanta6119a82011-05-29 19:57:12 +00001321 _LIBCPP_INLINE_VISIBILITY
1322 void __set_long_size(size_type __s) _NOEXCEPT
1323 {__r_.first().__l.__size_ = __s;}
1324 _LIBCPP_INLINE_VISIBILITY
1325 size_type __get_long_size() const _NOEXCEPT
1326 {return __r_.first().__l.__size_;}
1327 _LIBCPP_INLINE_VISIBILITY
1328 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1330
Howard Hinnanta6119a82011-05-29 19:57:12 +00001331 _LIBCPP_INLINE_VISIBILITY
1332 void __set_long_cap(size_type __s) _NOEXCEPT
1333 {__r_.first().__l.__cap_ = __long_mask | __s;}
1334 _LIBCPP_INLINE_VISIBILITY
1335 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnantec3773c2011-12-01 20:21:04 +00001336 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337
Howard Hinnanta6119a82011-05-29 19:57:12 +00001338 _LIBCPP_INLINE_VISIBILITY
1339 void __set_long_pointer(pointer __p) _NOEXCEPT
1340 {__r_.first().__l.__data_ = __p;}
1341 _LIBCPP_INLINE_VISIBILITY
1342 pointer __get_long_pointer() _NOEXCEPT
1343 {return __r_.first().__l.__data_;}
1344 _LIBCPP_INLINE_VISIBILITY
1345 const_pointer __get_long_pointer() const _NOEXCEPT
1346 {return __r_.first().__l.__data_;}
1347 _LIBCPP_INLINE_VISIBILITY
1348 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001349 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001350 _LIBCPP_INLINE_VISIBILITY
1351 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001352 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001353 _LIBCPP_INLINE_VISIBILITY
1354 pointer __get_pointer() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001355 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001356 _LIBCPP_INLINE_VISIBILITY
1357 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001358 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1359
Howard Hinnanta6119a82011-05-29 19:57:12 +00001360 _LIBCPP_INLINE_VISIBILITY
1361 void __zero() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001362 {
1363 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1364 for (unsigned __i = 0; __i < __n_words; ++__i)
1365 __a[__i] = 0;
1366 }
1367
1368 template <size_type __a> static
Howard Hinnanta6119a82011-05-29 19:57:12 +00001369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00001370 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +00001371 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001372 enum {__alignment = 16};
Howard Hinnanta6119a82011-05-29 19:57:12 +00001373 static _LIBCPP_INLINE_VISIBILITY
1374 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow088e6012018-02-07 21:30:17 +00001375 {
1376 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1377 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1378 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1379 if (__guess == __min_cap) ++__guess;
1380 return __guess;
1381 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001382
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001383 inline
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001384 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001385 inline
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001386 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001387 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388 void __init(size_type __n, value_type __c);
Howard Hinnant324bb032010-08-22 00:02:43 +00001389
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001390 template <class _InputIterator>
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001391 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001392 typename enable_if
1393 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001394 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001395 void
1396 >::type
1397 __init(_InputIterator __first, _InputIterator __last);
1398
1399 template <class _ForwardIterator>
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001400 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001401 typename enable_if
1402 <
1403 __is_forward_iterator<_ForwardIterator>::value,
1404 void
1405 >::type
1406 __init(_ForwardIterator __first, _ForwardIterator __last);
1407
1408 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant324bb032010-08-22 00:02:43 +00001409 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001410 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1411 size_type __n_copy, size_type __n_del,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001412 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001413
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001415 void __erase_to_end(size_type __pos);
1416
Howard Hinnante32b5e22010-11-17 17:55:08 +00001417 _LIBCPP_INLINE_VISIBILITY
1418 void __copy_assign_alloc(const basic_string& __str)
1419 {__copy_assign_alloc(__str, integral_constant<bool,
1420 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1421
1422 _LIBCPP_INLINE_VISIBILITY
1423 void __copy_assign_alloc(const basic_string& __str, true_type)
1424 {
Marshall Clow9247fd22017-01-31 03:40:52 +00001425 if (__alloc() == __str.__alloc())
1426 __alloc() = __str.__alloc();
1427 else
Howard Hinnante32b5e22010-11-17 17:55:08 +00001428 {
Marshall Clow9247fd22017-01-31 03:40:52 +00001429 if (!__str.__is_long())
1430 {
Vedant Kumar2b588cb2018-03-08 21:15:26 +00001431 __clear_and_shrink();
Marshall Clow9247fd22017-01-31 03:40:52 +00001432 __alloc() = __str.__alloc();
1433 }
1434 else
1435 {
1436 allocator_type __a = __str.__alloc();
1437 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar2b588cb2018-03-08 21:15:26 +00001438 __clear_and_shrink();
Marshall Clow9247fd22017-01-31 03:40:52 +00001439 __alloc() = _VSTD::move(__a);
1440 __set_long_pointer(__p);
1441 __set_long_cap(__str.__get_long_cap());
1442 __set_long_size(__str.size());
1443 }
Howard Hinnante32b5e22010-11-17 17:55:08 +00001444 }
Howard Hinnante32b5e22010-11-17 17:55:08 +00001445 }
1446
1447 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001448 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnante32b5e22010-11-17 17:55:08 +00001449 {}
1450
Eric Fiselier3e928972017-04-19 00:28:44 +00001451#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001452 _LIBCPP_INLINE_VISIBILITY
Marshall Clowaf961ed2015-08-18 18:57:00 +00001453 void __move_assign(basic_string& __str, false_type)
1454 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001456 void __move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001457#if _LIBCPP_STD_VER > 14
1458 _NOEXCEPT;
1459#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001460 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001461#endif
Marshall Clowaf961ed2015-08-18 18:57:00 +00001462#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00001463
1464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001465 void
Howard Hinnant9cbee432011-09-02 20:42:31 +00001466 __move_assign_alloc(basic_string& __str)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001467 _NOEXCEPT_(
1468 !__alloc_traits::propagate_on_container_move_assignment::value ||
1469 is_nothrow_move_assignable<allocator_type>::value)
1470 {__move_assign_alloc(__str, integral_constant<bool,
1471 __alloc_traits::propagate_on_container_move_assignment::value>());}
1472
1473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00001474 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001475 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1476 {
1477 __alloc() = _VSTD::move(__c.__alloc());
1478 }
1479
1480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001481 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001482 _NOEXCEPT
1483 {}
1484
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001485 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1486 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487
1488 friend basic_string operator+<>(const basic_string&, const basic_string&);
1489 friend basic_string operator+<>(const value_type*, const basic_string&);
1490 friend basic_string operator+<>(value_type, const basic_string&);
1491 friend basic_string operator+<>(const basic_string&, const value_type*);
1492 friend basic_string operator+<>(const basic_string&, value_type);
1493};
1494
Marshall Clow5b1e87e2018-02-08 06:34:03 +00001495#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1496template<class _InputIterator,
1497 class _CharT = typename iterator_traits<_InputIterator>::value_type,
1498 class _Allocator = allocator<_CharT>,
1499 class = typename enable_if<__is_input_iterator<_InputIterator>::value, void>::type,
1500 class = typename enable_if<__is_allocator<_Allocator>::value, void>::type
1501 >
1502basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1503 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
1504#endif
1505
1506
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001507template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001508inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001509void
1510basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1511{
Howard Hinnant499cea12013-08-23 17:37:05 +00001512#if _LIBCPP_DEBUG_LEVEL >= 2
1513 __get_db()->__invalidate_all(this);
1514#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515}
1516
1517template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001518inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519void
Howard Hinnantec3773c2011-12-01 20:21:04 +00001520basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Howard Hinnant499cea12013-08-23 17:37:05 +00001521#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantec3773c2011-12-01 20:21:04 +00001522 __pos
1523#endif
1524 )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001525{
Howard Hinnant499cea12013-08-23 17:37:05 +00001526#if _LIBCPP_DEBUG_LEVEL >= 2
1527 __c_node* __c = __get_db()->__find_c_and_lock(this);
1528 if (__c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001529 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001530 const_pointer __new_last = __get_pointer() + __pos;
1531 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001532 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001533 --__p;
1534 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1535 if (__i->base() > __new_last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001536 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001537 (*__p)->__c_ = nullptr;
1538 if (--__c->end_ != __p)
1539 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001540 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001541 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001542 __get_db()->unlock();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001544#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001545}
1546
1547template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001548inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001549basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowc912c0c2015-06-04 02:05:41 +00001550 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001551{
Howard Hinnant499cea12013-08-23 17:37:05 +00001552#if _LIBCPP_DEBUG_LEVEL >= 2
1553 __get_db()->__insert_c(this);
1554#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001555 __zero();
1556}
1557
1558template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001559inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiselier692177d2015-07-18 20:40:46 +00001561#if _LIBCPP_STD_VER <= 14
1562 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1563#else
1564 _NOEXCEPT
1565#endif
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001566: __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001567{
Howard Hinnant499cea12013-08-23 17:37:05 +00001568#if _LIBCPP_DEBUG_LEVEL >= 2
1569 __get_db()->__insert_c(this);
1570#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001571 __zero();
1572}
1573
1574template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier6dbed462016-09-16 00:00:48 +00001575void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1576 size_type __sz,
1577 size_type __reserve)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001578{
1579 if (__reserve > max_size())
1580 this->__throw_length_error();
1581 pointer __p;
1582 if (__reserve < __min_cap)
1583 {
1584 __set_short_size(__sz);
1585 __p = __get_short_pointer();
1586 }
1587 else
1588 {
1589 size_type __cap = __recommend(__reserve);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001590 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001591 __set_long_pointer(__p);
1592 __set_long_cap(__cap+1);
1593 __set_long_size(__sz);
1594 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001595 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001596 traits_type::assign(__p[__sz], value_type());
1597}
1598
1599template <class _CharT, class _Traits, class _Allocator>
1600void
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001601basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001602{
1603 if (__sz > max_size())
1604 this->__throw_length_error();
1605 pointer __p;
1606 if (__sz < __min_cap)
1607 {
1608 __set_short_size(__sz);
1609 __p = __get_short_pointer();
1610 }
1611 else
1612 {
1613 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001614 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615 __set_long_pointer(__p);
1616 __set_long_cap(__cap+1);
1617 __set_long_size(__sz);
1618 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001619 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620 traits_type::assign(__p[__sz], value_type());
1621}
1622
1623template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001624inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001625basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001626{
Howard Hinnant499cea12013-08-23 17:37:05 +00001627 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001628 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001629#if _LIBCPP_DEBUG_LEVEL >= 2
1630 __get_db()->__insert_c(this);
1631#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001632}
1633
1634template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001635inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001636basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001637 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001638{
Howard Hinnant499cea12013-08-23 17:37:05 +00001639 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001641#if _LIBCPP_DEBUG_LEVEL >= 2
1642 __get_db()->__insert_c(this);
1643#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001644}
1645
1646template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001647inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001648basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001649{
Howard Hinnant499cea12013-08-23 17:37:05 +00001650 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001651 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00001652#if _LIBCPP_DEBUG_LEVEL >= 2
1653 __get_db()->__insert_c(this);
1654#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655}
1656
1657template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001658inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001659basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001660 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661{
Howard Hinnant499cea12013-08-23 17:37:05 +00001662 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001663 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00001664#if _LIBCPP_DEBUG_LEVEL >= 2
1665 __get_db()->__insert_c(this);
1666#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667}
1668
1669template <class _CharT, class _Traits, class _Allocator>
1670basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001671 : __r_(__second_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001672{
1673 if (!__str.__is_long())
1674 __r_.first().__r = __str.__r_.first().__r;
1675 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001676 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001677#if _LIBCPP_DEBUG_LEVEL >= 2
1678 __get_db()->__insert_c(this);
1679#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001680}
1681
1682template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001683basic_string<_CharT, _Traits, _Allocator>::basic_string(
1684 const basic_string& __str, const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001685 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001686{
1687 if (!__str.__is_long())
1688 __r_.first().__r = __str.__r_.first().__r;
1689 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001690 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001691#if _LIBCPP_DEBUG_LEVEL >= 2
1692 __get_db()->__insert_c(this);
1693#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001694}
1695
Eric Fiselier3e928972017-04-19 00:28:44 +00001696#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001697
1698template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001699inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001700basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +00001701#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001702 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow7b193f72015-06-03 19:56:43 +00001703#else
1704 _NOEXCEPT
1705#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001706 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001707{
1708 __str.__zero();
Howard Hinnant499cea12013-08-23 17:37:05 +00001709#if _LIBCPP_DEBUG_LEVEL >= 2
1710 __get_db()->__insert_c(this);
1711 if (__is_long())
1712 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001713#endif
1714}
1715
1716template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001717inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001718basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001719 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720{
Marshall Clowd5549cc2014-07-17 15:32:20 +00001721 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001722 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd5549cc2014-07-17 15:32:20 +00001723 else
1724 {
1725 __r_.first().__r = __str.__r_.first().__r;
1726 __str.__zero();
1727 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001728#if _LIBCPP_DEBUG_LEVEL >= 2
1729 __get_db()->__insert_c(this);
1730 if (__is_long())
1731 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001732#endif
1733}
1734
Eric Fiselier3e928972017-04-19 00:28:44 +00001735#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001736
1737template <class _CharT, class _Traits, class _Allocator>
1738void
1739basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1740{
1741 if (__n > max_size())
1742 this->__throw_length_error();
1743 pointer __p;
1744 if (__n < __min_cap)
1745 {
1746 __set_short_size(__n);
1747 __p = __get_short_pointer();
1748 }
1749 else
1750 {
1751 size_type __cap = __recommend(__n);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001752 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001753 __set_long_pointer(__p);
1754 __set_long_cap(__cap+1);
1755 __set_long_size(__n);
1756 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001757 traits_type::assign(_VSTD::__to_raw_pointer(__p), __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001758 traits_type::assign(__p[__n], value_type());
1759}
1760
1761template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001762inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001763basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001764{
1765 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00001766#if _LIBCPP_DEBUG_LEVEL >= 2
1767 __get_db()->__insert_c(this);
1768#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001769}
1770
1771template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001772inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001773basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001774 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001775{
1776 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00001777#if _LIBCPP_DEBUG_LEVEL >= 2
1778 __get_db()->__insert_c(this);
1779#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001780}
1781
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001782template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001783basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
1784 size_type __pos, size_type __n,
1785 const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001786 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001787{
1788 size_type __str_sz = __str.size();
1789 if (__pos > __str_sz)
1790 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001791 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Howard Hinnant499cea12013-08-23 17:37:05 +00001792#if _LIBCPP_DEBUG_LEVEL >= 2
1793 __get_db()->__insert_c(this);
1794#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795}
1796
1797template <class _CharT, class _Traits, class _Allocator>
Marshall Clowf4f7d8f2016-04-07 18:13:41 +00001798inline _LIBCPP_INLINE_VISIBILITY
1799basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001800 const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001801 : __r_(__second_tag(), __a)
Marshall Clowf4f7d8f2016-04-07 18:13:41 +00001802{
1803 size_type __str_sz = __str.size();
1804 if (__pos > __str_sz)
1805 this->__throw_out_of_range();
1806 __init(__str.data() + __pos, __str_sz - __pos);
1807#if _LIBCPP_DEBUG_LEVEL >= 2
1808 __get_db()->__insert_c(this);
1809#endif
1810}
1811
1812template <class _CharT, class _Traits, class _Allocator>
Marshall Clowdb7fa112016-11-14 18:22:19 +00001813template <class _Tp>
1814basic_string<_CharT, _Traits, _Allocator>::basic_string(
1815 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a,
Marshall Clowf1729d92017-11-15 20:02:27 +00001816 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type *)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001817 : __r_(__second_tag(), __a)
Marshall Clowdb7fa112016-11-14 18:22:19 +00001818{
Marshall Clowf1729d92017-11-15 20:02:27 +00001819 __self_view __sv = __self_view(__t).substr(__pos, __n);
Marshall Clowdb7fa112016-11-14 18:22:19 +00001820 __init(__sv.data(), __sv.size());
1821#if _LIBCPP_DEBUG_LEVEL >= 2
1822 __get_db()->__insert_c(this);
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001823#endif
Marshall Clowdb7fa112016-11-14 18:22:19 +00001824}
1825
1826template <class _CharT, class _Traits, class _Allocator>
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001827inline _LIBCPP_INLINE_VISIBILITY
1828basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv)
1829{
1830 __init(__sv.data(), __sv.size());
1831#if _LIBCPP_DEBUG_LEVEL >= 2
1832 __get_db()->__insert_c(this);
1833#endif
1834}
1835
1836template <class _CharT, class _Traits, class _Allocator>
1837inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001838basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001839 : __r_(__second_tag(), __a)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001840{
1841 __init(__sv.data(), __sv.size());
1842#if _LIBCPP_DEBUG_LEVEL >= 2
1843 __get_db()->__insert_c(this);
1844#endif
1845}
1846
1847template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001848template <class _InputIterator>
1849typename enable_if
1850<
Marshall Clowdf9db312016-01-13 21:54:34 +00001851 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852 void
1853>::type
1854basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
1855{
1856 __zero();
1857#ifndef _LIBCPP_NO_EXCEPTIONS
1858 try
1859 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001860#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861 for (; __first != __last; ++__first)
1862 push_back(*__first);
1863#ifndef _LIBCPP_NO_EXCEPTIONS
1864 }
1865 catch (...)
1866 {
1867 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001868 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001869 throw;
1870 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001871#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001872}
1873
1874template <class _CharT, class _Traits, class _Allocator>
1875template <class _ForwardIterator>
1876typename enable_if
1877<
1878 __is_forward_iterator<_ForwardIterator>::value,
1879 void
1880>::type
1881basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
1882{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001883 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001884 if (__sz > max_size())
1885 this->__throw_length_error();
1886 pointer __p;
1887 if (__sz < __min_cap)
1888 {
1889 __set_short_size(__sz);
1890 __p = __get_short_pointer();
1891 }
1892 else
1893 {
1894 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001895 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896 __set_long_pointer(__p);
1897 __set_long_cap(__cap+1);
1898 __set_long_size(__sz);
1899 }
Eric Fiselierb9919752014-10-27 19:28:20 +00001900 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001901 traits_type::assign(*__p, *__first);
1902 traits_type::assign(*__p, value_type());
1903}
1904
1905template <class _CharT, class _Traits, class _Allocator>
1906template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001907inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001908basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
1909{
1910 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001911#if _LIBCPP_DEBUG_LEVEL >= 2
1912 __get_db()->__insert_c(this);
1913#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001914}
1915
1916template <class _CharT, class _Traits, class _Allocator>
1917template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001918inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001919basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
1920 const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001921 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001922{
1923 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001924#if _LIBCPP_DEBUG_LEVEL >= 2
1925 __get_db()->__insert_c(this);
1926#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927}
1928
Eric Fiselier3e928972017-04-19 00:28:44 +00001929#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00001930
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001932inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001933basic_string<_CharT, _Traits, _Allocator>::basic_string(
1934 initializer_list<_CharT> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001935{
1936 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001937#if _LIBCPP_DEBUG_LEVEL >= 2
1938 __get_db()->__insert_c(this);
1939#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001940}
1941
1942template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001943inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001944
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001945basic_string<_CharT, _Traits, _Allocator>::basic_string(
1946 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001947 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001948{
1949 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001950#if _LIBCPP_DEBUG_LEVEL >= 2
1951 __get_db()->__insert_c(this);
1952#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953}
1954
Eric Fiselier3e928972017-04-19 00:28:44 +00001955#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00001956
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001957template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001958basic_string<_CharT, _Traits, _Allocator>::~basic_string()
1959{
Howard Hinnant499cea12013-08-23 17:37:05 +00001960#if _LIBCPP_DEBUG_LEVEL >= 2
1961 __get_db()->__erase_c(this);
1962#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001963 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001964 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001965}
1966
1967template <class _CharT, class _Traits, class _Allocator>
1968void
1969basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
1970 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001971 size_type __n_copy, size_type __n_del, size_type __n_add, const value_type* __p_new_stuff)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001972{
1973 size_type __ms = max_size();
1974 if (__delta_cap > __ms - __old_cap - 1)
1975 this->__throw_length_error();
1976 pointer __old_p = __get_pointer();
1977 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00001978 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001980 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001981 __invalidate_all_iterators();
1982 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001983 traits_type::copy(_VSTD::__to_raw_pointer(__p),
1984 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001985 if (__n_add != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001986 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001987 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1988 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001989 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
1990 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001991 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001992 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001993 __set_long_pointer(__p);
1994 __set_long_cap(__cap+1);
1995 __old_sz = __n_copy + __n_add + __sec_cp_sz;
1996 __set_long_size(__old_sz);
1997 traits_type::assign(__p[__old_sz], value_type());
1998}
1999
2000template <class _CharT, class _Traits, class _Allocator>
2001void
2002basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2003 size_type __n_copy, size_type __n_del, size_type __n_add)
2004{
2005 size_type __ms = max_size();
Marshall Clowecc8d7b2013-11-06 14:24:38 +00002006 if (__delta_cap > __ms - __old_cap)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002007 this->__throw_length_error();
2008 pointer __old_p = __get_pointer();
2009 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00002010 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002011 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00002012 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002013 __invalidate_all_iterators();
2014 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002015 traits_type::copy(_VSTD::__to_raw_pointer(__p),
2016 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002017 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2018 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002019 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
2020 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del,
2021 __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002022 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002023 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024 __set_long_pointer(__p);
2025 __set_long_cap(__cap+1);
2026}
2027
2028// assign
2029
2030template <class _CharT, class _Traits, class _Allocator>
2031basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002032basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002033{
Alp Tokerec34c482014-05-15 11:27:39 +00002034 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035 size_type __cap = capacity();
2036 if (__cap >= __n)
2037 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002038 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002039 traits_type::move(__p, __s, __n);
2040 traits_type::assign(__p[__n], value_type());
2041 __set_size(__n);
2042 __invalidate_iterators_past(__n);
2043 }
2044 else
2045 {
2046 size_type __sz = size();
2047 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2048 }
2049 return *this;
2050}
2051
2052template <class _CharT, class _Traits, class _Allocator>
2053basic_string<_CharT, _Traits, _Allocator>&
2054basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2055{
2056 size_type __cap = capacity();
2057 if (__cap < __n)
2058 {
2059 size_type __sz = size();
2060 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2061 }
2062 else
2063 __invalidate_iterators_past(__n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002064 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002065 traits_type::assign(__p, __n, __c);
2066 traits_type::assign(__p[__n], value_type());
2067 __set_size(__n);
2068 return *this;
2069}
2070
2071template <class _CharT, class _Traits, class _Allocator>
2072basic_string<_CharT, _Traits, _Allocator>&
2073basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2074{
2075 pointer __p;
2076 if (__is_long())
2077 {
2078 __p = __get_long_pointer();
2079 __set_long_size(1);
2080 }
2081 else
2082 {
2083 __p = __get_short_pointer();
2084 __set_short_size(1);
2085 }
2086 traits_type::assign(*__p, __c);
2087 traits_type::assign(*++__p, value_type());
2088 __invalidate_iterators_past(1);
2089 return *this;
2090}
2091
2092template <class _CharT, class _Traits, class _Allocator>
Howard Hinnante32b5e22010-11-17 17:55:08 +00002093basic_string<_CharT, _Traits, _Allocator>&
2094basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2095{
2096 if (this != &__str)
2097 {
2098 __copy_assign_alloc(__str);
Marshall Clowf40ec902016-03-09 18:08:29 +00002099 assign(__str.data(), __str.size());
Howard Hinnante32b5e22010-11-17 17:55:08 +00002100 }
2101 return *this;
2102}
2103
Eric Fiselier3e928972017-04-19 00:28:44 +00002104#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante32b5e22010-11-17 17:55:08 +00002105
2106template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002107inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002108void
2109basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002110 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002111{
2112 if (__alloc() != __str.__alloc())
2113 assign(__str);
2114 else
2115 __move_assign(__str, true_type());
2116}
2117
2118template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002119inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002120void
2121basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002122#if _LIBCPP_STD_VER > 14
2123 _NOEXCEPT
2124#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00002125 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002126#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00002127{
Vedant Kumar2b588cb2018-03-08 21:15:26 +00002128 __clear_and_shrink();
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00002129 __r_.first() = __str.__r_.first();
2130 __move_assign_alloc(__str);
Howard Hinnante32b5e22010-11-17 17:55:08 +00002131 __str.__zero();
2132}
2133
2134template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002135inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002136basic_string<_CharT, _Traits, _Allocator>&
2137basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002138 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnante32b5e22010-11-17 17:55:08 +00002139{
2140 __move_assign(__str, integral_constant<bool,
2141 __alloc_traits::propagate_on_container_move_assignment::value>());
2142 return *this;
2143}
2144
2145#endif
2146
2147template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002148template<class _InputIterator>
2149typename enable_if
2150<
Marshall Clowdf9db312016-01-13 21:54:34 +00002151 __is_exactly_input_iterator <_InputIterator>::value
2152 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002153 basic_string<_CharT, _Traits, _Allocator>&
2154>::type
2155basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2156{
Marshall Clowd979eed2016-09-05 01:54:30 +00002157 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002158 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002159 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002160}
2161
2162template <class _CharT, class _Traits, class _Allocator>
2163template<class _ForwardIterator>
2164typename enable_if
2165<
Marshall Clowdf9db312016-01-13 21:54:34 +00002166 __is_forward_iterator<_ForwardIterator>::value
2167 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002168 basic_string<_CharT, _Traits, _Allocator>&
2169>::type
2170basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2171{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002172 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002173 size_type __cap = capacity();
2174 if (__cap < __n)
2175 {
2176 size_type __sz = size();
2177 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2178 }
2179 else
2180 __invalidate_iterators_past(__n);
2181 pointer __p = __get_pointer();
2182 for (; __first != __last; ++__first, ++__p)
2183 traits_type::assign(*__p, *__first);
2184 traits_type::assign(*__p, value_type());
2185 __set_size(__n);
2186 return *this;
2187}
2188
2189template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002190basic_string<_CharT, _Traits, _Allocator>&
2191basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2192{
2193 size_type __sz = __str.size();
2194 if (__pos > __sz)
2195 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002196 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002197}
2198
2199template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002200template <class _Tp>
2201typename enable_if
2202<
2203 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
Marshall Clowf1729d92017-11-15 20:02:27 +00002204 basic_string<_CharT, _Traits, _Allocator>&
Marshall Clow6ac8de02016-09-24 22:45:42 +00002205>::type
2206basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002207{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002208 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002209 size_type __sz = __sv.size();
2210 if (__pos > __sz)
2211 this->__throw_out_of_range();
2212 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2213}
2214
2215
2216template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002217basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002218basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219{
Alp Tokerec34c482014-05-15 11:27:39 +00002220 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002221 return assign(__s, traits_type::length(__s));
2222}
2223
2224// append
2225
2226template <class _CharT, class _Traits, class _Allocator>
2227basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002228basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002229{
Alp Tokerec34c482014-05-15 11:27:39 +00002230 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231 size_type __cap = capacity();
2232 size_type __sz = size();
2233 if (__cap - __sz >= __n)
2234 {
2235 if (__n)
2236 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002237 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002238 traits_type::copy(__p + __sz, __s, __n);
2239 __sz += __n;
2240 __set_size(__sz);
2241 traits_type::assign(__p[__sz], value_type());
2242 }
2243 }
2244 else
2245 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2246 return *this;
2247}
2248
2249template <class _CharT, class _Traits, class _Allocator>
2250basic_string<_CharT, _Traits, _Allocator>&
2251basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2252{
2253 if (__n)
2254 {
2255 size_type __cap = capacity();
2256 size_type __sz = size();
2257 if (__cap - __sz < __n)
2258 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2259 pointer __p = __get_pointer();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002260 traits_type::assign(_VSTD::__to_raw_pointer(__p) + __sz, __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002261 __sz += __n;
2262 __set_size(__sz);
2263 traits_type::assign(__p[__sz], value_type());
2264 }
2265 return *this;
2266}
2267
2268template <class _CharT, class _Traits, class _Allocator>
2269void
2270basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2271{
Howard Hinnant15467182013-04-30 21:44:48 +00002272 bool __is_short = !__is_long();
2273 size_type __cap;
2274 size_type __sz;
2275 if (__is_short)
2276 {
2277 __cap = __min_cap - 1;
2278 __sz = __get_short_size();
2279 }
2280 else
2281 {
2282 __cap = __get_long_cap() - 1;
2283 __sz = __get_long_size();
2284 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002285 if (__sz == __cap)
Howard Hinnant15467182013-04-30 21:44:48 +00002286 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002287 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant15467182013-04-30 21:44:48 +00002288 __is_short = !__is_long();
2289 }
2290 pointer __p;
2291 if (__is_short)
2292 {
2293 __p = __get_short_pointer() + __sz;
2294 __set_short_size(__sz+1);
2295 }
2296 else
2297 {
2298 __p = __get_long_pointer() + __sz;
2299 __set_long_size(__sz+1);
2300 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002301 traits_type::assign(*__p, __c);
2302 traits_type::assign(*++__p, value_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303}
2304
Marshall Clowac655ef2016-09-07 03:32:06 +00002305template <class _Tp>
Marshall Clowd979eed2016-09-05 01:54:30 +00002306bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2307{
2308 return __first <= __p && __p < __last;
2309}
2310
Marshall Clowac655ef2016-09-07 03:32:06 +00002311template <class _Tp1, class _Tp2>
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00002312bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clowac655ef2016-09-07 03:32:06 +00002313{
2314 return false;
2315}
2316
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002317template <class _CharT, class _Traits, class _Allocator>
2318template<class _ForwardIterator>
Eric Fiselier026d38e2016-10-31 02:46:25 +00002319basic_string<_CharT, _Traits, _Allocator>&
2320basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2321 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002322{
Eric Fiselier026d38e2016-10-31 02:46:25 +00002323 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2324 "function requires a ForwardIterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002325 size_type __sz = size();
2326 size_type __cap = capacity();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002327 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002328 if (__n)
2329 {
Eric Fiselier622c7d52017-04-15 06:49:02 +00002330 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2331 _CharRef __tmp_ref = *__first;
2332 if (__ptr_in_range(_VSTD::addressof(__tmp_ref), data(), data() + size()))
Marshall Clowd979eed2016-09-05 01:54:30 +00002333 {
2334 const basic_string __temp (__first, __last, __alloc());
2335 append(__temp.data(), __temp.size());
2336 }
2337 else
2338 {
2339 if (__cap - __sz < __n)
2340 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2341 pointer __p = __get_pointer() + __sz;
2342 for (; __first != __last; ++__p, ++__first)
2343 traits_type::assign(*__p, *__first);
2344 traits_type::assign(*__p, value_type());
2345 __set_size(__sz + __n);
2346 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002347 }
2348 return *this;
2349}
2350
2351template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002352inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002353basic_string<_CharT, _Traits, _Allocator>&
2354basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2355{
2356 return append(__str.data(), __str.size());
2357}
2358
2359template <class _CharT, class _Traits, class _Allocator>
2360basic_string<_CharT, _Traits, _Allocator>&
2361basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2362{
2363 size_type __sz = __str.size();
2364 if (__pos > __sz)
2365 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002366 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002367}
2368
2369template <class _CharT, class _Traits, class _Allocator>
Marshall Clow42a87db2016-10-03 23:40:48 +00002370template <class _Tp>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002371 typename enable_if
2372 <
2373 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2374 basic_string<_CharT, _Traits, _Allocator>&
2375 >::type
2376basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002377{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002378 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002379 size_type __sz = __sv.size();
2380 if (__pos > __sz)
2381 this->__throw_out_of_range();
2382 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2383}
2384
2385template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002386basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002387basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002388{
Alp Tokerec34c482014-05-15 11:27:39 +00002389 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002390 return append(__s, traits_type::length(__s));
2391}
2392
2393// insert
2394
2395template <class _CharT, class _Traits, class _Allocator>
2396basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002397basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002398{
Alp Tokerec34c482014-05-15 11:27:39 +00002399 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002400 size_type __sz = size();
2401 if (__pos > __sz)
2402 this->__throw_out_of_range();
2403 size_type __cap = capacity();
2404 if (__cap - __sz >= __n)
2405 {
2406 if (__n)
2407 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002408 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002409 size_type __n_move = __sz - __pos;
2410 if (__n_move != 0)
2411 {
2412 if (__p + __pos <= __s && __s < __p + __sz)
2413 __s += __n;
2414 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2415 }
2416 traits_type::move(__p + __pos, __s, __n);
2417 __sz += __n;
2418 __set_size(__sz);
2419 traits_type::assign(__p[__sz], value_type());
2420 }
2421 }
2422 else
2423 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2424 return *this;
2425}
2426
2427template <class _CharT, class _Traits, class _Allocator>
2428basic_string<_CharT, _Traits, _Allocator>&
2429basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2430{
2431 size_type __sz = size();
2432 if (__pos > __sz)
2433 this->__throw_out_of_range();
2434 if (__n)
2435 {
2436 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002437 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002438 if (__cap - __sz >= __n)
2439 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002440 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002441 size_type __n_move = __sz - __pos;
2442 if (__n_move != 0)
2443 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2444 }
2445 else
2446 {
2447 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002448 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449 }
2450 traits_type::assign(__p + __pos, __n, __c);
2451 __sz += __n;
2452 __set_size(__sz);
2453 traits_type::assign(__p[__sz], value_type());
2454 }
2455 return *this;
2456}
2457
2458template <class _CharT, class _Traits, class _Allocator>
2459template<class _InputIterator>
2460typename enable_if
2461<
Marshall Clowdf9db312016-01-13 21:54:34 +00002462 __is_exactly_input_iterator<_InputIterator>::value
2463 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2464 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002465>::type
2466basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2467{
Howard Hinnant499cea12013-08-23 17:37:05 +00002468#if _LIBCPP_DEBUG_LEVEL >= 2
2469 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2470 "string::insert(iterator, range) called with an iterator not"
2471 " referring to this string");
2472#endif
Marshall Clowd979eed2016-09-05 01:54:30 +00002473 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002474 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002475}
2476
2477template <class _CharT, class _Traits, class _Allocator>
2478template<class _ForwardIterator>
2479typename enable_if
2480<
Marshall Clowdf9db312016-01-13 21:54:34 +00002481 __is_forward_iterator<_ForwardIterator>::value
2482 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002483 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2484>::type
2485basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2486{
Howard Hinnant499cea12013-08-23 17:37:05 +00002487#if _LIBCPP_DEBUG_LEVEL >= 2
2488 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2489 "string::insert(iterator, range) called with an iterator not"
2490 " referring to this string");
2491#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002492 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002493 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002494 if (__n)
2495 {
Eric Fiselier622c7d52017-04-15 06:49:02 +00002496 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2497 _CharRef __tmp_char = *__first;
2498 if (__ptr_in_range(_VSTD::addressof(__tmp_char), data(), data() + size()))
Marshall Clowd979eed2016-09-05 01:54:30 +00002499 {
2500 const basic_string __temp(__first, __last, __alloc());
2501 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2502 }
2503
2504 size_type __sz = size();
2505 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002506 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002507 if (__cap - __sz >= __n)
2508 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002509 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002510 size_type __n_move = __sz - __ip;
2511 if (__n_move != 0)
2512 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2513 }
2514 else
2515 {
2516 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002517 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002518 }
2519 __sz += __n;
2520 __set_size(__sz);
2521 traits_type::assign(__p[__sz], value_type());
2522 for (__p += __ip; __first != __last; ++__p, ++__first)
2523 traits_type::assign(*__p, *__first);
2524 }
2525 return begin() + __ip;
2526}
2527
2528template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002529inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002530basic_string<_CharT, _Traits, _Allocator>&
2531basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2532{
2533 return insert(__pos1, __str.data(), __str.size());
2534}
2535
2536template <class _CharT, class _Traits, class _Allocator>
2537basic_string<_CharT, _Traits, _Allocator>&
2538basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2539 size_type __pos2, size_type __n)
2540{
2541 size_type __str_sz = __str.size();
2542 if (__pos2 > __str_sz)
2543 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002544 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002545}
2546
2547template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002548template <class _Tp>
2549typename enable_if
2550<
2551 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
Marshall Clowf1729d92017-11-15 20:02:27 +00002552 basic_string<_CharT, _Traits, _Allocator>&
Marshall Clow6ac8de02016-09-24 22:45:42 +00002553>::type
2554basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002555 size_type __pos2, size_type __n)
2556{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002557 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002558 size_type __str_sz = __sv.size();
2559 if (__pos2 > __str_sz)
2560 this->__throw_out_of_range();
2561 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2562}
2563
2564template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002565basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002566basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002567{
Alp Tokerec34c482014-05-15 11:27:39 +00002568 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569 return insert(__pos, __s, traits_type::length(__s));
2570}
2571
2572template <class _CharT, class _Traits, class _Allocator>
2573typename basic_string<_CharT, _Traits, _Allocator>::iterator
2574basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2575{
2576 size_type __ip = static_cast<size_type>(__pos - begin());
2577 size_type __sz = size();
2578 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002579 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002580 if (__cap == __sz)
2581 {
2582 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002583 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002584 }
2585 else
2586 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002587 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002588 size_type __n_move = __sz - __ip;
2589 if (__n_move != 0)
2590 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2591 }
2592 traits_type::assign(__p[__ip], __c);
2593 traits_type::assign(__p[++__sz], value_type());
2594 __set_size(__sz);
2595 return begin() + static_cast<difference_type>(__ip);
2596}
2597
2598template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002599inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002600typename basic_string<_CharT, _Traits, _Allocator>::iterator
2601basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2602{
Howard Hinnant499cea12013-08-23 17:37:05 +00002603#if _LIBCPP_DEBUG_LEVEL >= 2
2604 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2605 "string::insert(iterator, n, value) called with an iterator not"
2606 " referring to this string");
2607#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002608 difference_type __p = __pos - begin();
2609 insert(static_cast<size_type>(__p), __n, __c);
2610 return begin() + __p;
2611}
2612
2613// replace
2614
2615template <class _CharT, class _Traits, class _Allocator>
2616basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002617basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2)
Eric Fiselier3b7c1342017-03-09 01:54:13 +00002618 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002619{
Alp Tokerec34c482014-05-15 11:27:39 +00002620 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002621 size_type __sz = size();
2622 if (__pos > __sz)
2623 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002624 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002625 size_type __cap = capacity();
2626 if (__cap - __sz + __n1 >= __n2)
2627 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002628 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002629 if (__n1 != __n2)
2630 {
2631 size_type __n_move = __sz - __pos - __n1;
2632 if (__n_move != 0)
2633 {
2634 if (__n1 > __n2)
2635 {
2636 traits_type::move(__p + __pos, __s, __n2);
2637 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2638 goto __finish;
2639 }
2640 if (__p + __pos < __s && __s < __p + __sz)
2641 {
2642 if (__p + __pos + __n1 <= __s)
2643 __s += __n2 - __n1;
2644 else // __p + __pos < __s < __p + __pos + __n1
2645 {
2646 traits_type::move(__p + __pos, __s, __n1);
2647 __pos += __n1;
2648 __s += __n2;
2649 __n2 -= __n1;
2650 __n1 = 0;
2651 }
2652 }
2653 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2654 }
2655 }
2656 traits_type::move(__p + __pos, __s, __n2);
2657__finish:
Eric Fiselier3b7c1342017-03-09 01:54:13 +00002658// __sz += __n2 - __n1; in this and the below function below can cause unsigned integer overflow,
2659// but this is a safe operation, so we disable the check.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002660 __sz += __n2 - __n1;
2661 __set_size(__sz);
2662 __invalidate_iterators_past(__sz);
2663 traits_type::assign(__p[__sz], value_type());
2664 }
2665 else
2666 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2667 return *this;
2668}
2669
2670template <class _CharT, class _Traits, class _Allocator>
2671basic_string<_CharT, _Traits, _Allocator>&
2672basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiselier3b7c1342017-03-09 01:54:13 +00002673 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002674{
2675 size_type __sz = size();
2676 if (__pos > __sz)
2677 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002678 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002679 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002680 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681 if (__cap - __sz + __n1 >= __n2)
2682 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002683 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002684 if (__n1 != __n2)
2685 {
2686 size_type __n_move = __sz - __pos - __n1;
2687 if (__n_move != 0)
2688 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2689 }
2690 }
2691 else
2692 {
2693 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002694 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002695 }
2696 traits_type::assign(__p + __pos, __n2, __c);
2697 __sz += __n2 - __n1;
2698 __set_size(__sz);
2699 __invalidate_iterators_past(__sz);
2700 traits_type::assign(__p[__sz], value_type());
2701 return *this;
2702}
2703
2704template <class _CharT, class _Traits, class _Allocator>
2705template<class _InputIterator>
2706typename enable_if
2707<
2708 __is_input_iterator<_InputIterator>::value,
2709 basic_string<_CharT, _Traits, _Allocator>&
2710>::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002711basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002712 _InputIterator __j1, _InputIterator __j2)
2713{
Marshall Clowd979eed2016-09-05 01:54:30 +00002714 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002715 return this->replace(__i1, __i2, __temp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002716}
2717
2718template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002719inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002720basic_string<_CharT, _Traits, _Allocator>&
2721basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2722{
2723 return replace(__pos1, __n1, __str.data(), __str.size());
2724}
2725
2726template <class _CharT, class _Traits, class _Allocator>
2727basic_string<_CharT, _Traits, _Allocator>&
2728basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
2729 size_type __pos2, size_type __n2)
2730{
2731 size_type __str_sz = __str.size();
2732 if (__pos2 > __str_sz)
2733 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002734 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002735}
2736
2737template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002738template <class _Tp>
2739typename enable_if
2740<
Marshall Clowf1729d92017-11-15 20:02:27 +00002741 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2742 basic_string<_CharT, _Traits, _Allocator>&
Marshall Clow6ac8de02016-09-24 22:45:42 +00002743>::type
2744basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002745 size_type __pos2, size_type __n2)
2746{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002747 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002748 size_type __str_sz = __sv.size();
2749 if (__pos2 > __str_sz)
2750 this->__throw_out_of_range();
2751 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
2752}
2753
2754template <class _CharT, class _Traits, class _Allocator>
2755basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002756basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002757{
Alp Tokerec34c482014-05-15 11:27:39 +00002758 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002759 return replace(__pos, __n1, __s, traits_type::length(__s));
2760}
2761
2762template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002763inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002764basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002765basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002766{
2767 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
2768 __str.data(), __str.size());
2769}
2770
2771template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002772inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002773basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002774basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002775{
2776 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
2777}
2778
2779template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002780inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002781basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002782basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002783{
2784 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
2785}
2786
2787template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002788inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002789basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002790basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002791{
2792 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
2793}
2794
2795// erase
2796
2797template <class _CharT, class _Traits, class _Allocator>
2798basic_string<_CharT, _Traits, _Allocator>&
2799basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
2800{
2801 size_type __sz = size();
2802 if (__pos > __sz)
2803 this->__throw_out_of_range();
2804 if (__n)
2805 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002806 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002807 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002808 size_type __n_move = __sz - __pos - __n;
2809 if (__n_move != 0)
2810 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
2811 __sz -= __n;
2812 __set_size(__sz);
2813 __invalidate_iterators_past(__sz);
2814 traits_type::assign(__p[__sz], value_type());
2815 }
2816 return *this;
2817}
2818
2819template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002820inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002821typename basic_string<_CharT, _Traits, _Allocator>::iterator
2822basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
2823{
Howard Hinnant499cea12013-08-23 17:37:05 +00002824#if _LIBCPP_DEBUG_LEVEL >= 2
2825 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2826 "string::erase(iterator) called with an iterator not"
2827 " referring to this string");
2828#endif
2829 _LIBCPP_ASSERT(__pos != end(),
2830 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002831 iterator __b = begin();
2832 size_type __r = static_cast<size_type>(__pos - __b);
2833 erase(__r, 1);
Howard Hinnantec3773c2011-12-01 20:21:04 +00002834 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002835}
2836
2837template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002838inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002839typename basic_string<_CharT, _Traits, _Allocator>::iterator
2840basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
2841{
Howard Hinnant499cea12013-08-23 17:37:05 +00002842#if _LIBCPP_DEBUG_LEVEL >= 2
2843 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2844 "string::erase(iterator, iterator) called with an iterator not"
2845 " referring to this string");
2846#endif
2847 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002848 iterator __b = begin();
2849 size_type __r = static_cast<size_type>(__first - __b);
2850 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnantec3773c2011-12-01 20:21:04 +00002851 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002852}
2853
2854template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002855inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002856void
2857basic_string<_CharT, _Traits, _Allocator>::pop_back()
2858{
Howard Hinnant499cea12013-08-23 17:37:05 +00002859 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002860 size_type __sz;
2861 if (__is_long())
2862 {
2863 __sz = __get_long_size() - 1;
2864 __set_long_size(__sz);
2865 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
2866 }
2867 else
2868 {
2869 __sz = __get_short_size() - 1;
2870 __set_short_size(__sz);
2871 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
2872 }
2873 __invalidate_iterators_past(__sz);
2874}
2875
2876template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002877inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002878void
Howard Hinnanta6119a82011-05-29 19:57:12 +00002879basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002880{
2881 __invalidate_all_iterators();
2882 if (__is_long())
2883 {
2884 traits_type::assign(*__get_long_pointer(), value_type());
2885 __set_long_size(0);
2886 }
2887 else
2888 {
2889 traits_type::assign(*__get_short_pointer(), value_type());
2890 __set_short_size(0);
2891 }
2892}
2893
2894template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002895inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002896void
2897basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
2898{
2899 if (__is_long())
2900 {
2901 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
2902 __set_long_size(__pos);
2903 }
2904 else
2905 {
2906 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
2907 __set_short_size(__pos);
2908 }
2909 __invalidate_iterators_past(__pos);
2910}
2911
2912template <class _CharT, class _Traits, class _Allocator>
2913void
2914basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
2915{
2916 size_type __sz = size();
2917 if (__n > __sz)
2918 append(__n - __sz, __c);
2919 else
2920 __erase_to_end(__n);
2921}
2922
2923template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002924inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002925typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00002926basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002927{
Howard Hinnante32b5e22010-11-17 17:55:08 +00002928 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiselier5ccf0432017-10-17 13:16:01 +00002929#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow09f85502013-10-31 17:23:08 +00002930 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002931#else
Marshall Clow09f85502013-10-31 17:23:08 +00002932 return __m - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002933#endif
2934}
2935
2936template <class _CharT, class _Traits, class _Allocator>
2937void
2938basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
2939{
2940 if (__res_arg > max_size())
2941 this->__throw_length_error();
2942 size_type __cap = capacity();
2943 size_type __sz = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002944 __res_arg = _VSTD::max(__res_arg, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002945 __res_arg = __recommend(__res_arg);
2946 if (__res_arg != __cap)
2947 {
2948 pointer __new_data, __p;
2949 bool __was_long, __now_long;
2950 if (__res_arg == __min_cap - 1)
2951 {
2952 __was_long = true;
2953 __now_long = false;
2954 __new_data = __get_short_pointer();
2955 __p = __get_long_pointer();
2956 }
2957 else
2958 {
2959 if (__res_arg > __cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002960 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002961 else
2962 {
2963 #ifndef _LIBCPP_NO_EXCEPTIONS
2964 try
2965 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002966 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnante32b5e22010-11-17 17:55:08 +00002967 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002968 #ifndef _LIBCPP_NO_EXCEPTIONS
2969 }
2970 catch (...)
2971 {
2972 return;
2973 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002974 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002975 if (__new_data == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002976 return;
Howard Hinnant324bb032010-08-22 00:02:43 +00002977 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002978 }
2979 __now_long = true;
2980 __was_long = __is_long();
2981 __p = __get_pointer();
2982 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002983 traits_type::copy(_VSTD::__to_raw_pointer(__new_data),
2984 _VSTD::__to_raw_pointer(__p), size()+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002985 if (__was_long)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002986 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002987 if (__now_long)
2988 {
2989 __set_long_cap(__res_arg+1);
2990 __set_long_size(__sz);
2991 __set_long_pointer(__new_data);
2992 }
2993 else
2994 __set_short_size(__sz);
2995 __invalidate_all_iterators();
2996 }
2997}
2998
2999template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003000inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003001typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003002basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003003{
Howard Hinnant499cea12013-08-23 17:37:05 +00003004 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003005 return *(data() + __pos);
3006}
3007
3008template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003009inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003010typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003011basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003012{
Howard Hinnant499cea12013-08-23 17:37:05 +00003013 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003014 return *(__get_pointer() + __pos);
3015}
3016
3017template <class _CharT, class _Traits, class _Allocator>
3018typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3019basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3020{
3021 if (__n >= size())
3022 this->__throw_out_of_range();
3023 return (*this)[__n];
3024}
3025
3026template <class _CharT, class _Traits, class _Allocator>
3027typename basic_string<_CharT, _Traits, _Allocator>::reference
3028basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3029{
3030 if (__n >= size())
3031 this->__throw_out_of_range();
3032 return (*this)[__n];
3033}
3034
3035template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003036inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003037typename basic_string<_CharT, _Traits, _Allocator>::reference
3038basic_string<_CharT, _Traits, _Allocator>::front()
3039{
Howard Hinnant499cea12013-08-23 17:37:05 +00003040 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003041 return *__get_pointer();
3042}
3043
3044template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003045inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003046typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3047basic_string<_CharT, _Traits, _Allocator>::front() const
3048{
Howard Hinnant499cea12013-08-23 17:37:05 +00003049 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003050 return *data();
3051}
3052
3053template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003054inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003055typename basic_string<_CharT, _Traits, _Allocator>::reference
3056basic_string<_CharT, _Traits, _Allocator>::back()
3057{
Howard Hinnant499cea12013-08-23 17:37:05 +00003058 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003059 return *(__get_pointer() + size() - 1);
3060}
3061
3062template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003063inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003064typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3065basic_string<_CharT, _Traits, _Allocator>::back() const
3066{
Howard Hinnant499cea12013-08-23 17:37:05 +00003067 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003068 return *(data() + size() - 1);
3069}
3070
3071template <class _CharT, class _Traits, class _Allocator>
3072typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003073basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003074{
3075 size_type __sz = size();
3076 if (__pos > __sz)
3077 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003078 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003079 traits_type::copy(__s, data() + __pos, __rlen);
3080 return __rlen;
3081}
3082
3083template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003084inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003085basic_string<_CharT, _Traits, _Allocator>
3086basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3087{
3088 return basic_string(*this, __pos, __n, __alloc());
3089}
3090
3091template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003092inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003093void
3094basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00003095#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00003096 _NOEXCEPT_DEBUG
Marshall Clow7d914d12015-07-13 20:04:56 +00003097#else
Eric Fiselier47257c42016-12-28 05:53:01 +00003098 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00003099 __is_nothrow_swappable<allocator_type>::value)
3100#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003101{
Howard Hinnant499cea12013-08-23 17:37:05 +00003102#if _LIBCPP_DEBUG_LEVEL >= 2
3103 if (!__is_long())
3104 __get_db()->__invalidate_all(this);
3105 if (!__str.__is_long())
3106 __get_db()->__invalidate_all(&__str);
3107 __get_db()->swap(this, &__str);
3108#endif
Eric Fiselier47257c42016-12-28 05:53:01 +00003109 _LIBCPP_ASSERT(
3110 __alloc_traits::propagate_on_container_swap::value ||
3111 __alloc_traits::is_always_equal::value ||
3112 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnant0949eed2011-06-30 21:18:19 +00003113 _VSTD::swap(__r_.first(), __str.__r_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00003114 __swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003115}
3116
3117// find
3118
3119template <class _Traits>
3120struct _LIBCPP_HIDDEN __traits_eq
3121{
3122 typedef typename _Traits::char_type char_type;
Howard Hinnanta6119a82011-05-29 19:57:12 +00003123 _LIBCPP_INLINE_VISIBILITY
3124 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3125 {return _Traits::eq(__x, __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003126};
3127
3128template<class _CharT, class _Traits, class _Allocator>
3129typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003130basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003131 size_type __pos,
3132 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003133{
Alp Tokerec34c482014-05-15 11:27:39 +00003134 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003135 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003136 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003137}
3138
3139template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003140inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003141typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003142basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3143 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003144{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003145 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003146 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003147}
3148
3149template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003150inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003151typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003152basic_string<_CharT, _Traits, _Allocator>::find(__self_view __sv,
3153 size_type __pos) const _NOEXCEPT
3154{
3155 return __str_find<value_type, size_type, traits_type, npos>
3156 (data(), size(), __sv.data(), __pos, __sv.size());
3157}
3158
3159template<class _CharT, class _Traits, class _Allocator>
3160inline _LIBCPP_INLINE_VISIBILITY
3161typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003162basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003163 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003164{
Alp Tokerec34c482014-05-15 11:27:39 +00003165 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003166 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003167 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003168}
3169
3170template<class _CharT, class _Traits, class _Allocator>
3171typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003172basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3173 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003174{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003175 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003176 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003177}
3178
3179// rfind
3180
3181template<class _CharT, class _Traits, class _Allocator>
3182typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003183basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003184 size_type __pos,
3185 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003186{
Alp Tokerec34c482014-05-15 11:27:39 +00003187 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003188 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003189 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003190}
3191
3192template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003193inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003194typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003195basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3196 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003197{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003198 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003199 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003200}
3201
3202template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003203inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003204typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003205basic_string<_CharT, _Traits, _Allocator>::rfind(__self_view __sv,
3206 size_type __pos) const _NOEXCEPT
3207{
3208 return __str_rfind<value_type, size_type, traits_type, npos>
3209 (data(), size(), __sv.data(), __pos, __sv.size());
3210}
3211
3212template<class _CharT, class _Traits, class _Allocator>
3213inline _LIBCPP_INLINE_VISIBILITY
3214typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003215basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003216 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003217{
Alp Tokerec34c482014-05-15 11:27:39 +00003218 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003219 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003220 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003221}
3222
3223template<class _CharT, class _Traits, class _Allocator>
3224typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003225basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3226 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003227{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003228 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003229 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003230}
3231
3232// find_first_of
3233
3234template<class _CharT, class _Traits, class _Allocator>
3235typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003236basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003237 size_type __pos,
3238 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003239{
Alp Tokerec34c482014-05-15 11:27:39 +00003240 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003241 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003242 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003243}
3244
3245template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003246inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003247typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003248basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3249 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003250{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003251 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003252 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003253}
3254
3255template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003256inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003257typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003258basic_string<_CharT, _Traits, _Allocator>::find_first_of(__self_view __sv,
3259 size_type __pos) const _NOEXCEPT
3260{
3261 return __str_find_first_of<value_type, size_type, traits_type, npos>
3262 (data(), size(), __sv.data(), __pos, __sv.size());
3263}
3264
3265template<class _CharT, class _Traits, class _Allocator>
3266inline _LIBCPP_INLINE_VISIBILITY
3267typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003268basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003269 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003270{
Alp Tokerec34c482014-05-15 11:27:39 +00003271 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003272 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003273 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003274}
3275
3276template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003277inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003278typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003279basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3280 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003281{
3282 return find(__c, __pos);
3283}
3284
3285// find_last_of
3286
3287template<class _CharT, class _Traits, class _Allocator>
3288typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003289basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003290 size_type __pos,
3291 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003292{
Alp Tokerec34c482014-05-15 11:27:39 +00003293 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003294 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003295 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003296}
3297
3298template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003299inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003300typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003301basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3302 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003303{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003304 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003305 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003306}
3307
3308template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003309inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003310typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003311basic_string<_CharT, _Traits, _Allocator>::find_last_of(__self_view __sv,
3312 size_type __pos) const _NOEXCEPT
3313{
3314 return __str_find_last_of<value_type, size_type, traits_type, npos>
3315 (data(), size(), __sv.data(), __pos, __sv.size());
3316}
3317
3318template<class _CharT, class _Traits, class _Allocator>
3319inline _LIBCPP_INLINE_VISIBILITY
3320typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003321basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003322 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003323{
Alp Tokerec34c482014-05-15 11:27:39 +00003324 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003325 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003326 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003327}
3328
3329template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003330inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003331typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003332basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3333 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003334{
3335 return rfind(__c, __pos);
3336}
3337
3338// find_first_not_of
3339
3340template<class _CharT, class _Traits, class _Allocator>
3341typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003342basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003343 size_type __pos,
3344 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003345{
Alp Tokerec34c482014-05-15 11:27:39 +00003346 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003347 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003348 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003349}
3350
3351template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003352inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003353typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003354basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3355 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003356{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003357 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003358 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003359}
3360
3361template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003362inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003363typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003364basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(__self_view __sv,
3365 size_type __pos) const _NOEXCEPT
3366{
3367 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3368 (data(), size(), __sv.data(), __pos, __sv.size());
3369}
3370
3371template<class _CharT, class _Traits, class _Allocator>
3372inline _LIBCPP_INLINE_VISIBILITY
3373typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003374basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003375 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003376{
Alp Tokerec34c482014-05-15 11:27:39 +00003377 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003378 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003379 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003380}
3381
3382template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003383inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003384typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003385basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3386 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003387{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003388 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003389 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003390}
3391
3392// find_last_not_of
3393
3394template<class _CharT, class _Traits, class _Allocator>
3395typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003396basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003397 size_type __pos,
3398 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003399{
Alp Tokerec34c482014-05-15 11:27:39 +00003400 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003401 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003402 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003403}
3404
3405template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003406inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003407typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003408basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3409 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003410{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003411 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003412 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003413}
3414
3415template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003416inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003417typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003418basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(__self_view __sv,
3419 size_type __pos) const _NOEXCEPT
3420{
3421 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3422 (data(), size(), __sv.data(), __pos, __sv.size());
3423}
3424
3425template<class _CharT, class _Traits, class _Allocator>
3426inline _LIBCPP_INLINE_VISIBILITY
3427typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003428basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003429 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003430{
Alp Tokerec34c482014-05-15 11:27:39 +00003431 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003432 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003433 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003434}
3435
3436template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003437inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003438typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003439basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3440 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003441{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003442 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003443 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003444}
3445
3446// compare
3447
3448template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003449inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003450int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003451basic_string<_CharT, _Traits, _Allocator>::compare(__self_view __sv) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003452{
Howard Hinnantfa06d752011-07-24 21:45:06 +00003453 size_t __lhs_sz = size();
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003454 size_t __rhs_sz = __sv.size();
3455 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantfa06d752011-07-24 21:45:06 +00003456 _VSTD::min(__lhs_sz, __rhs_sz));
3457 if (__result != 0)
3458 return __result;
3459 if (__lhs_sz < __rhs_sz)
3460 return -1;
3461 if (__lhs_sz > __rhs_sz)
3462 return 1;
3463 return 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003464}
3465
3466template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003467inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003468int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003469basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003470{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003471 return compare(__self_view(__str));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003472}
3473
3474template <class _CharT, class _Traits, class _Allocator>
3475int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003476basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3477 size_type __n1,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003478 const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003479 size_type __n2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003480{
Alp Tokerec34c482014-05-15 11:27:39 +00003481 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003482 size_type __sz = size();
3483 if (__pos1 > __sz || __n2 == npos)
3484 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003485 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3486 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003487 if (__r == 0)
3488 {
3489 if (__rlen < __n2)
3490 __r = -1;
3491 else if (__rlen > __n2)
3492 __r = 1;
3493 }
3494 return __r;
3495}
3496
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003497template <class _CharT, class _Traits, class _Allocator>
3498inline _LIBCPP_INLINE_VISIBILITY
3499int
3500basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3501 size_type __n1,
3502 __self_view __sv) const
3503{
3504 return compare(__pos1, __n1, __sv.data(), __sv.size());
3505}
3506
3507template <class _CharT, class _Traits, class _Allocator>
3508inline _LIBCPP_INLINE_VISIBILITY
3509int
3510basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3511 size_type __n1,
3512 const basic_string& __str) const
3513{
3514 return compare(__pos1, __n1, __str.data(), __str.size());
3515}
3516
3517template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00003518template <class _Tp>
3519typename enable_if
3520<
Marshall Clowf1729d92017-11-15 20:02:27 +00003521 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3522 int
Marshall Clow6ac8de02016-09-24 22:45:42 +00003523>::type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003524basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3525 size_type __n1,
Marshall Clow6ac8de02016-09-24 22:45:42 +00003526 const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003527 size_type __pos2,
3528 size_type __n2) const
3529{
Marshall Clow6ac8de02016-09-24 22:45:42 +00003530 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003531 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3532}
3533
3534template <class _CharT, class _Traits, class _Allocator>
3535int
3536basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3537 size_type __n1,
3538 const basic_string& __str,
3539 size_type __pos2,
3540 size_type __n2) const
3541{
3542 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3543}
3544
3545template <class _CharT, class _Traits, class _Allocator>
3546int
3547basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3548{
3549 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3550 return compare(0, npos, __s, traits_type::length(__s));
3551}
3552
3553template <class _CharT, class _Traits, class _Allocator>
3554int
3555basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3556 size_type __n1,
3557 const value_type* __s) const
3558{
3559 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3560 return compare(__pos1, __n1, __s, traits_type::length(__s));
3561}
3562
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003563// __invariants
3564
3565template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003566inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003567bool
3568basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3569{
3570 if (size() > capacity())
3571 return false;
3572 if (capacity() < __min_cap - 1)
3573 return false;
3574 if (data() == 0)
3575 return false;
3576 if (data()[size()] != value_type(0))
3577 return false;
3578 return true;
3579}
3580
Vedant Kumar2b588cb2018-03-08 21:15:26 +00003581// __clear_and_shrink
3582
3583template<class _CharT, class _Traits, class _Allocator>
3584inline _LIBCPP_INLINE_VISIBILITY
3585void
3586basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink()
3587{
3588 clear();
3589 if(__is_long())
3590 {
3591 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3592 __set_long_cap(0);
3593 __set_short_size(0);
3594 }
3595}
3596
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003597// operator==
3598
3599template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003600inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003601bool
3602operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003603 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003604{
Howard Hinnant08dd2532013-04-22 23:55:13 +00003605 size_t __lhs_sz = __lhs.size();
3606 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3607 __rhs.data(),
3608 __lhs_sz) == 0;
3609}
3610
3611template<class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003612inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant08dd2532013-04-22 23:55:13 +00003613bool
3614operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3615 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3616{
3617 size_t __lhs_sz = __lhs.size();
3618 if (__lhs_sz != __rhs.size())
3619 return false;
3620 const char* __lp = __lhs.data();
3621 const char* __rp = __rhs.data();
3622 if (__lhs.__is_long())
3623 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3624 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3625 if (*__lp != *__rp)
3626 return false;
3627 return true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003628}
3629
3630template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003631inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003632bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003633operator==(const _CharT* __lhs,
3634 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003635{
Eric Fiselier4f241822015-08-28 03:02:37 +00003636 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3637 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3638 size_t __lhs_len = _Traits::length(__lhs);
3639 if (__lhs_len != __rhs.size()) return false;
3640 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003641}
3642
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003643template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003644inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003645bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003646operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3647 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003648{
Eric Fiselier4f241822015-08-28 03:02:37 +00003649 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3650 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3651 size_t __rhs_len = _Traits::length(__rhs);
3652 if (__rhs_len != __lhs.size()) return false;
3653 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003654}
3655
Howard Hinnant324bb032010-08-22 00:02:43 +00003656template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003657inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003658bool
3659operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003660 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003661{
3662 return !(__lhs == __rhs);
3663}
3664
3665template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003666inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003667bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003668operator!=(const _CharT* __lhs,
3669 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003670{
3671 return !(__lhs == __rhs);
3672}
3673
3674template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003675inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003676bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003677operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3678 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003679{
3680 return !(__lhs == __rhs);
3681}
3682
3683// operator<
3684
3685template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003686inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003687bool
3688operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003689 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003690{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003691 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003692}
3693
3694template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003695inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003696bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003697operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3698 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003699{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003700 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003701}
3702
3703template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003704inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003705bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003706operator< (const _CharT* __lhs,
3707 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003708{
3709 return __rhs.compare(__lhs) > 0;
3710}
3711
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003712// operator>
3713
3714template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003715inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003716bool
3717operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003718 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003719{
3720 return __rhs < __lhs;
3721}
3722
3723template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003724inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003725bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003726operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3727 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003728{
3729 return __rhs < __lhs;
3730}
3731
3732template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003733inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003734bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003735operator> (const _CharT* __lhs,
3736 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003737{
3738 return __rhs < __lhs;
3739}
3740
3741// operator<=
3742
3743template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003744inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003745bool
3746operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003747 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003748{
3749 return !(__rhs < __lhs);
3750}
3751
3752template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003753inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003754bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003755operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3756 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003757{
3758 return !(__rhs < __lhs);
3759}
3760
3761template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003762inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003763bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003764operator<=(const _CharT* __lhs,
3765 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003766{
3767 return !(__rhs < __lhs);
3768}
3769
3770// operator>=
3771
3772template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003773inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003774bool
3775operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003776 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003777{
3778 return !(__lhs < __rhs);
3779}
3780
3781template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003782inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003783bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003784operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3785 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003786{
3787 return !(__lhs < __rhs);
3788}
3789
3790template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003791inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003792bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003793operator>=(const _CharT* __lhs,
3794 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003795{
3796 return !(__lhs < __rhs);
3797}
3798
3799// operator +
3800
3801template<class _CharT, class _Traits, class _Allocator>
3802basic_string<_CharT, _Traits, _Allocator>
3803operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3804 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3805{
3806 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3807 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3808 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3809 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3810 __r.append(__rhs.data(), __rhs_sz);
3811 return __r;
3812}
3813
3814template<class _CharT, class _Traits, class _Allocator>
3815basic_string<_CharT, _Traits, _Allocator>
3816operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3817{
3818 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3819 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
3820 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3821 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
3822 __r.append(__rhs.data(), __rhs_sz);
3823 return __r;
3824}
3825
3826template<class _CharT, class _Traits, class _Allocator>
3827basic_string<_CharT, _Traits, _Allocator>
3828operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3829{
3830 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3831 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3832 __r.__init(&__lhs, 1, 1 + __rhs_sz);
3833 __r.append(__rhs.data(), __rhs_sz);
3834 return __r;
3835}
3836
3837template<class _CharT, class _Traits, class _Allocator>
3838basic_string<_CharT, _Traits, _Allocator>
3839operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3840{
3841 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3842 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3843 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
3844 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3845 __r.append(__rhs, __rhs_sz);
3846 return __r;
3847}
3848
3849template<class _CharT, class _Traits, class _Allocator>
3850basic_string<_CharT, _Traits, _Allocator>
3851operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
3852{
3853 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3854 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3855 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
3856 __r.push_back(__rhs);
3857 return __r;
3858}
3859
Eric Fiselier3e928972017-04-19 00:28:44 +00003860#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003861
3862template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003863inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003864basic_string<_CharT, _Traits, _Allocator>
3865operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3866{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003867 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003868}
3869
3870template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003871inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003872basic_string<_CharT, _Traits, _Allocator>
3873operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3874{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003875 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003876}
3877
3878template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003879inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003880basic_string<_CharT, _Traits, _Allocator>
3881operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3882{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003883 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003884}
3885
3886template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003887inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003888basic_string<_CharT, _Traits, _Allocator>
3889operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3890{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003891 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003892}
3893
3894template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003895inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003896basic_string<_CharT, _Traits, _Allocator>
3897operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3898{
3899 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003900 return _VSTD::move(__rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003901}
3902
3903template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003904inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003905basic_string<_CharT, _Traits, _Allocator>
3906operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
3907{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003908 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003909}
3910
3911template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003912inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003913basic_string<_CharT, _Traits, _Allocator>
3914operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
3915{
3916 __lhs.push_back(__rhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003917 return _VSTD::move(__lhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003918}
3919
Eric Fiselier3e928972017-04-19 00:28:44 +00003920#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003921
3922// swap
3923
3924template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003925inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003926void
Howard Hinnanta6119a82011-05-29 19:57:12 +00003927swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00003928 basic_string<_CharT, _Traits, _Allocator>& __rhs)
3929 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003930{
3931 __lhs.swap(__rhs);
3932}
3933
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003934#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
3935
3936typedef basic_string<char16_t> u16string;
3937typedef basic_string<char32_t> u32string;
3938
Howard Hinnant324bb032010-08-22 00:02:43 +00003939#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003940
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003941_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
3942_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
3943_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
3944_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
3945_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003946
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003947_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
3948_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
3949_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003950
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003951_LIBCPP_FUNC_VIS string to_string(int __val);
3952_LIBCPP_FUNC_VIS string to_string(unsigned __val);
3953_LIBCPP_FUNC_VIS string to_string(long __val);
3954_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
3955_LIBCPP_FUNC_VIS string to_string(long long __val);
3956_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
3957_LIBCPP_FUNC_VIS string to_string(float __val);
3958_LIBCPP_FUNC_VIS string to_string(double __val);
3959_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003960
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003961_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
3962_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
3963_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
3964_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
3965_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003966
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003967_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = 0);
3968_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
3969_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003970
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003971_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
3972_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
3973_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
3974_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
3975_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
3976_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
3977_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
3978_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
3979_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003980
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003981template<class _CharT, class _Traits, class _Allocator>
3982 const typename basic_string<_CharT, _Traits, _Allocator>::size_type
3983 basic_string<_CharT, _Traits, _Allocator>::npos;
3984
3985template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +00003986struct _LIBCPP_TEMPLATE_VIS hash<basic_string<_CharT, _Traits, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003987 : public unary_function<basic_string<_CharT, _Traits, _Allocator>, size_t>
3988{
3989 size_t
Howard Hinnanta6119a82011-05-29 19:57:12 +00003990 operator()(const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003991};
3992
3993template<class _CharT, class _Traits, class _Allocator>
3994size_t
3995hash<basic_string<_CharT, _Traits, _Allocator> >::operator()(
Howard Hinnanta6119a82011-05-29 19:57:12 +00003996 const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003997{
Sean Huntaffd9e52011-07-29 23:31:56 +00003998 return __do_string_hash(__val.data(), __val.data() + __val.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003999}
4000
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004001template<class _CharT, class _Traits, class _Allocator>
4002basic_ostream<_CharT, _Traits>&
4003operator<<(basic_ostream<_CharT, _Traits>& __os,
4004 const basic_string<_CharT, _Traits, _Allocator>& __str);
4005
4006template<class _CharT, class _Traits, class _Allocator>
4007basic_istream<_CharT, _Traits>&
4008operator>>(basic_istream<_CharT, _Traits>& __is,
4009 basic_string<_CharT, _Traits, _Allocator>& __str);
4010
4011template<class _CharT, class _Traits, class _Allocator>
4012basic_istream<_CharT, _Traits>&
4013getline(basic_istream<_CharT, _Traits>& __is,
4014 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4015
4016template<class _CharT, class _Traits, class _Allocator>
4017inline _LIBCPP_INLINE_VISIBILITY
4018basic_istream<_CharT, _Traits>&
4019getline(basic_istream<_CharT, _Traits>& __is,
4020 basic_string<_CharT, _Traits, _Allocator>& __str);
4021
Eric Fiselier3e928972017-04-19 00:28:44 +00004022#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004023
4024template<class _CharT, class _Traits, class _Allocator>
4025inline _LIBCPP_INLINE_VISIBILITY
4026basic_istream<_CharT, _Traits>&
4027getline(basic_istream<_CharT, _Traits>&& __is,
4028 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4029
4030template<class _CharT, class _Traits, class _Allocator>
4031inline _LIBCPP_INLINE_VISIBILITY
4032basic_istream<_CharT, _Traits>&
4033getline(basic_istream<_CharT, _Traits>&& __is,
4034 basic_string<_CharT, _Traits, _Allocator>& __str);
4035
Eric Fiselier3e928972017-04-19 00:28:44 +00004036#endif // _LIBCPP_CXX03_LANG
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004037
Howard Hinnant499cea12013-08-23 17:37:05 +00004038#if _LIBCPP_DEBUG_LEVEL >= 2
4039
4040template<class _CharT, class _Traits, class _Allocator>
4041bool
4042basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4043{
4044 return this->data() <= _VSTD::__to_raw_pointer(__i->base()) &&
4045 _VSTD::__to_raw_pointer(__i->base()) < this->data() + this->size();
4046}
4047
4048template<class _CharT, class _Traits, class _Allocator>
4049bool
4050basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4051{
4052 return this->data() < _VSTD::__to_raw_pointer(__i->base()) &&
4053 _VSTD::__to_raw_pointer(__i->base()) <= this->data() + this->size();
4054}
4055
4056template<class _CharT, class _Traits, class _Allocator>
4057bool
4058basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4059{
4060 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
4061 return this->data() <= __p && __p <= this->data() + this->size();
4062}
4063
4064template<class _CharT, class _Traits, class _Allocator>
4065bool
4066basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4067{
4068 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
4069 return this->data() <= __p && __p < this->data() + this->size();
4070}
4071
4072#endif // _LIBCPP_DEBUG_LEVEL >= 2
4073
Shoaib Meenai68506702017-06-29 02:52:46 +00004074_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<char>)
4075_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<wchar_t>)
Shoaib Meenai68506702017-06-29 02:52:46 +00004076
Marshall Clow15234322013-07-23 17:05:24 +00004077#if _LIBCPP_STD_VER > 11
4078// Literal suffixes for basic_string [basic.string.literals]
Marshall Clow8d9dd7a2013-10-05 21:18:32 +00004079inline namespace literals
Marshall Clow15234322013-07-23 17:05:24 +00004080{
4081 inline namespace string_literals
4082 {
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004083 inline _LIBCPP_INLINE_VISIBILITY
4084 basic_string<char> operator "" s( const char *__str, size_t __len )
4085 {
4086 return basic_string<char> (__str, __len);
4087 }
Marshall Clow15234322013-07-23 17:05:24 +00004088
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004089 inline _LIBCPP_INLINE_VISIBILITY
4090 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4091 {
4092 return basic_string<wchar_t> (__str, __len);
4093 }
Marshall Clow15234322013-07-23 17:05:24 +00004094
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004095 inline _LIBCPP_INLINE_VISIBILITY
4096 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4097 {
4098 return basic_string<char16_t> (__str, __len);
4099 }
Marshall Clow15234322013-07-23 17:05:24 +00004100
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004101 inline _LIBCPP_INLINE_VISIBILITY
4102 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4103 {
4104 return basic_string<char32_t> (__str, __len);
4105 }
Marshall Clow15234322013-07-23 17:05:24 +00004106 }
4107}
4108#endif
4109
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004110_LIBCPP_END_NAMESPACE_STD
4111
Eric Fiselier018a3d52017-05-31 22:07:49 +00004112_LIBCPP_POP_MACROS
4113
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004114#endif // _LIBCPP_STRING