blob: e7142b2b28e4671edf949c83ba635a18b37c3956 [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
304 bool __invariants() const;
305};
306
307template<class charT, class traits, class Allocator>
308basic_string<charT, traits, Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000309operator+(const basic_string<charT, traits, Allocator>& lhs,
310 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311
312template<class charT, class traits, class Allocator>
313basic_string<charT, traits, Allocator>
314operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
315
316template<class charT, class traits, class Allocator>
317basic_string<charT, traits, Allocator>
318operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
319
320template<class charT, class traits, class Allocator>
321basic_string<charT, traits, Allocator>
322operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
323
324template<class charT, class traits, class Allocator>
325basic_string<charT, traits, Allocator>
326operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
327
328template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000329bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000330 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000331
332template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000333bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334
335template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000336bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337
Howard Hinnant324bb032010-08-22 00:02:43 +0000338template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000339bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000340 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000341
342template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000343bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000344
345template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000346bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347
348template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000349bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000350 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351
352template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000353bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354
355template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000356bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000357
358template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000359bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000360 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361
362template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000363bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364
365template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000366bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000367
368template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000369bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000370 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371
372template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000373bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374
375template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000376bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377
378template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000379bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000380 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381
382template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000383bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384
385template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000386bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387
388template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000389void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000390 basic_string<charT, traits, Allocator>& rhs)
391 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392
393template<class charT, class traits, class Allocator>
394basic_istream<charT, traits>&
395operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
396
397template<class charT, class traits, class Allocator>
398basic_ostream<charT, traits>&
399operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
400
401template<class charT, class traits, class Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000402basic_istream<charT, traits>&
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000403getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
404 charT delim);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000405
406template<class charT, class traits, class Allocator>
407basic_istream<charT, traits>&
408getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
409
410typedef basic_string<char> string;
411typedef basic_string<wchar_t> wstring;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000412typedef basic_string<char16_t> u16string;
413typedef basic_string<char32_t> u32string;
414
415int stoi (const string& str, size_t* idx = 0, int base = 10);
416long stol (const string& str, size_t* idx = 0, int base = 10);
417unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
418long long stoll (const string& str, size_t* idx = 0, int base = 10);
419unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10);
420
421float stof (const string& str, size_t* idx = 0);
422double stod (const string& str, size_t* idx = 0);
423long double stold(const string& str, size_t* idx = 0);
424
425string to_string(int val);
426string to_string(unsigned val);
427string to_string(long val);
428string to_string(unsigned long val);
429string to_string(long long val);
430string to_string(unsigned long long val);
431string to_string(float val);
432string to_string(double val);
433string to_string(long double val);
434
435int stoi (const wstring& str, size_t* idx = 0, int base = 10);
436long stol (const wstring& str, size_t* idx = 0, int base = 10);
437unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
438long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
439unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10);
440
441float stof (const wstring& str, size_t* idx = 0);
442double stod (const wstring& str, size_t* idx = 0);
443long double stold(const wstring& str, size_t* idx = 0);
444
445wstring to_wstring(int val);
446wstring to_wstring(unsigned val);
447wstring to_wstring(long val);
448wstring to_wstring(unsigned long val);
449wstring to_wstring(long long val);
450wstring to_wstring(unsigned long long val);
451wstring to_wstring(float val);
452wstring to_wstring(double val);
453wstring to_wstring(long double val);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000454
455template <> struct hash<string>;
456template <> struct hash<u16string>;
457template <> struct hash<u32string>;
458template <> struct hash<wstring>;
459
Marshall Clow15234322013-07-23 17:05:24 +0000460basic_string<char> operator "" s( const char *str, size_t len ); // C++14
461basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
462basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
463basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
464
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465} // std
466
467*/
468
469#include <__config>
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000470#include <string_view>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471#include <iosfwd>
472#include <cstring>
Howard Hinnantadff4892010-05-24 17:49:41 +0000473#include <cstdio> // For EOF.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474#include <cwchar>
475#include <algorithm>
476#include <iterator>
477#include <utility>
478#include <memory>
479#include <stdexcept>
480#include <type_traits>
481#include <initializer_list>
482#include <__functional_base>
483#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
484#include <cstdint>
485#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000486
Eric Fiselierb9536102014-08-10 23:53:08 +0000487#include <__debug>
488
Howard Hinnant08e17472011-10-17 20:05:10 +0000489#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000490#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000491#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492
Eric Fiselier018a3d52017-05-31 22:07:49 +0000493_LIBCPP_PUSH_MACROS
494#include <__undef_macros>
495
496
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000497_LIBCPP_BEGIN_NAMESPACE_STD
498
499// fpos
500
501template <class _StateT>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000502class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000503{
504private:
505 _StateT __st_;
506 streamoff __off_;
507public:
508 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
509
510 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
511
512 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
513 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
514
515 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
516 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
517 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
518 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
519};
520
521template <class _StateT>
522inline _LIBCPP_INLINE_VISIBILITY
523streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
524 {return streamoff(__x) - streamoff(__y);}
525
526template <class _StateT>
527inline _LIBCPP_INLINE_VISIBILITY
528bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
529 {return streamoff(__x) == streamoff(__y);}
530
531template <class _StateT>
532inline _LIBCPP_INLINE_VISIBILITY
533bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
534 {return streamoff(__x) != streamoff(__y);}
535
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000536// basic_string
537
538template<class _CharT, class _Traits, class _Allocator>
539basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000540operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
541 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000542
543template<class _CharT, class _Traits, class _Allocator>
544basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000545operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000546
547template<class _CharT, class _Traits, class _Allocator>
548basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000549operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000550
551template<class _CharT, class _Traits, class _Allocator>
552basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000553operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554
555template<class _CharT, class _Traits, class _Allocator>
556basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000557operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000558
Shoaib Meenai487562f2017-07-29 02:54:41 +0000559_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
560
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000561template <bool>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000562class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000563{
564protected:
Marshall Clow14c09a22016-08-25 15:09:01 +0000565 _LIBCPP_NORETURN void __throw_length_error() const;
566 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000567};
568
569template <bool __b>
570void
571__basic_string_common<__b>::__throw_length_error() const
572{
Marshall Clow14c09a22016-08-25 15:09:01 +0000573 _VSTD::__throw_length_error("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574}
575
576template <bool __b>
577void
578__basic_string_common<__b>::__throw_out_of_range() const
579{
Marshall Clow14c09a22016-08-25 15:09:01 +0000580 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000581}
582
Eric Fiselier833d6442016-09-15 22:27:07 +0000583_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584
Marshall Clowdf9db312016-01-13 21:54:34 +0000585#ifdef _LIBCPP_NO_EXCEPTIONS
586template <class _Iter>
587struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
588#elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
589template <class _Iter>
590struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
591#else
592template <class _Iter, bool = __is_forward_iterator<_Iter>::value>
593struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
594 noexcept(++(declval<_Iter&>())) &&
595 is_nothrow_assignable<_Iter&, _Iter>::value &&
596 noexcept(declval<_Iter>() == declval<_Iter>()) &&
597 noexcept(*declval<_Iter>())
598)) {};
599
600template <class _Iter>
601struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
602#endif
603
604
605template <class _Iter>
606struct __libcpp_string_gets_noexcept_iterator
607 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
608
Marshall Clow6ac8de02016-09-24 22:45:42 +0000609template <class _CharT, class _Traits, class _Tp>
610struct __can_be_converted_to_string_view : public _LIBCPP_BOOL_CONSTANT(
Marshall Clowf1729d92017-11-15 20:02:27 +0000611 ( is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
Marshall Clow6ac8de02016-09-24 22:45:42 +0000612 !is_convertible<const _Tp&, const _CharT*>::value)) {};
613
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000614#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000615
616template <class _CharT, size_t = sizeof(_CharT)>
617struct __padding
618{
619 unsigned char __xx[sizeof(_CharT)-1];
620};
621
622template <class _CharT>
623struct __padding<_CharT, 1>
624{
625};
626
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000627#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000628
Howard Hinnant324bb032010-08-22 00:02:43 +0000629template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000630class _LIBCPP_TEMPLATE_VIS basic_string
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631 : private __basic_string_common<true>
632{
633public:
634 typedef basic_string __self;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000635 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636 typedef _Traits traits_type;
Marshall Clow2d4c3fa2017-03-15 18:41:11 +0000637 typedef _CharT value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638 typedef _Allocator allocator_type;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000639 typedef allocator_traits<allocator_type> __alloc_traits;
640 typedef typename __alloc_traits::size_type size_type;
641 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000642 typedef value_type& reference;
643 typedef const value_type& const_reference;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000644 typedef typename __alloc_traits::pointer pointer;
645 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646
Howard Hinnant499cea12013-08-23 17:37:05 +0000647 static_assert(is_pod<value_type>::value, "Character type of basic_string must be a POD");
Marshall Clow2d4c3fa2017-03-15 18:41:11 +0000648 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant499cea12013-08-23 17:37:05 +0000649 "traits_type::char_type must be the same type as CharT");
650 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
651 "Allocator::value_type must be same type as value_type");
652#if defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000653 typedef pointer iterator;
654 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000655#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656 typedef __wrap_iter<pointer> iterator;
657 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000658#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000659 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
660 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661
662private:
Howard Hinnant15467182013-04-30 21:44:48 +0000663
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000664#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000665
666 struct __long
667 {
668 pointer __data_;
669 size_type __size_;
670 size_type __cap_;
671 };
672
Eric Fiselier5ccf0432017-10-17 13:16:01 +0000673#ifdef _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000674 static const size_type __short_mask = 0x01;
675 static const size_type __long_mask = 0x1ul;
Howard Hinnant15467182013-04-30 21:44:48 +0000676#else // _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000677 static const size_type __short_mask = 0x80;
678 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant15467182013-04-30 21:44:48 +0000679#endif // _LIBCPP_BIG_ENDIAN
680
681 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
682 (sizeof(__long) - 1)/sizeof(value_type) : 2};
683
684 struct __short
685 {
686 value_type __data_[__min_cap];
687 struct
688 : __padding<value_type>
689 {
690 unsigned char __size_;
691 };
692 };
693
694#else
695
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000696 struct __long
697 {
698 size_type __cap_;
699 size_type __size_;
700 pointer __data_;
701 };
702
Eric Fiselier5ccf0432017-10-17 13:16:01 +0000703#ifdef _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000704 static const size_type __short_mask = 0x80;
705 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant324bb032010-08-22 00:02:43 +0000706#else // _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000707 static const size_type __short_mask = 0x01;
708 static const size_type __long_mask = 0x1ul;
Howard Hinnant324bb032010-08-22 00:02:43 +0000709#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000711 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
712 (sizeof(__long) - 1)/sizeof(value_type) : 2};
713
714 struct __short
715 {
716 union
717 {
718 unsigned char __size_;
Howard Hinnant9c0df142012-10-30 19:06:59 +0000719 value_type __lx;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000720 };
721 value_type __data_[__min_cap];
722 };
723
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000724#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000725
Howard Hinnant499cea12013-08-23 17:37:05 +0000726 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727
Howard Hinnant499cea12013-08-23 17:37:05 +0000728 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000729
730 struct __raw
731 {
732 size_type __words[__n_words];
733 };
734
735 struct __rep
736 {
737 union
738 {
739 __long __l;
740 __short __s;
741 __raw __r;
742 };
743 };
744
745 __compressed_pair<__rep, allocator_type> __r_;
746
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000747public:
748 static const size_type npos = -1;
749
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000750 _LIBCPP_INLINE_VISIBILITY basic_string()
751 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000752
753 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
754#if _LIBCPP_STD_VER <= 14
755 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
756#else
757 _NOEXCEPT;
758#endif
759
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000760 basic_string(const basic_string& __str);
761 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clow7b193f72015-06-03 19:56:43 +0000762
Eric Fiselier3e928972017-04-19 00:28:44 +0000763#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant9f193f22011-01-26 00:06:59 +0000764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000765 basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +0000766#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000767 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000768#else
769 _NOEXCEPT;
770#endif
771
Howard Hinnant9f193f22011-01-26 00:06:59 +0000772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000773 basic_string(basic_string&& __str, const allocator_type& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000774#endif // _LIBCPP_CXX03_LANG
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000775 _LIBCPP_INLINE_VISIBILITY basic_string(const _CharT* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000776 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000777 basic_string(const _CharT* __s, const _Allocator& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000778 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000779 basic_string(const _CharT* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000780 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000781 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000782 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000783 basic_string(size_type __n, _CharT __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000784 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000785 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000786 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000787 const _Allocator& __a = _Allocator());
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000788 _LIBCPP_INLINE_VISIBILITY
789 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000790 const _Allocator& __a = _Allocator());
Marshall Clowdb7fa112016-11-14 18:22:19 +0000791 template<class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000792 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000793 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Marshall Clowdb7fa112016-11-14 18:22:19 +0000794 const allocator_type& __a = allocator_type(),
795 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type* = 0);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000796 _LIBCPP_INLINE_VISIBILITY explicit
797 basic_string(__self_view __sv);
798 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000799 basic_string(__self_view __sv, const _Allocator& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000800 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000802 basic_string(_InputIterator __first, _InputIterator __last);
803 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000805 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000806#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000807 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000808 basic_string(initializer_list<_CharT> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000809 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000810 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000811#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000812
Eric Fiselier51eb1d52016-10-31 03:42:50 +0000813 inline ~basic_string();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000814
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000815 _LIBCPP_INLINE_VISIBILITY
816 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
817
Howard Hinnante32b5e22010-11-17 17:55:08 +0000818 basic_string& operator=(const basic_string& __str);
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000819
820#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier37b2be92017-01-17 22:10:32 +0000821 template <class = void>
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000822#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000823 _LIBCPP_INLINE_VISIBILITY
824 basic_string& operator=(__self_view __sv) {return assign(__sv);}
Eric Fiselier3e928972017-04-19 00:28:44 +0000825#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000827 basic_string& operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +0000828 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselier3e928972017-04-19 00:28:44 +0000829 _LIBCPP_INLINE_VISIBILITY
830 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000831#endif
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000832 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000833 basic_string& operator=(value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000834
Howard Hinnant499cea12013-08-23 17:37:05 +0000835#if _LIBCPP_DEBUG_LEVEL >= 2
836 _LIBCPP_INLINE_VISIBILITY
837 iterator begin() _NOEXCEPT
838 {return iterator(this, __get_pointer());}
839 _LIBCPP_INLINE_VISIBILITY
840 const_iterator begin() const _NOEXCEPT
841 {return const_iterator(this, __get_pointer());}
842 _LIBCPP_INLINE_VISIBILITY
843 iterator end() _NOEXCEPT
844 {return iterator(this, __get_pointer() + size());}
845 _LIBCPP_INLINE_VISIBILITY
846 const_iterator end() const _NOEXCEPT
847 {return const_iterator(this, __get_pointer() + size());}
848#else
Howard Hinnanta6119a82011-05-29 19:57:12 +0000849 _LIBCPP_INLINE_VISIBILITY
850 iterator begin() _NOEXCEPT
851 {return iterator(__get_pointer());}
852 _LIBCPP_INLINE_VISIBILITY
853 const_iterator begin() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000854 {return const_iterator(__get_pointer());}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000855 _LIBCPP_INLINE_VISIBILITY
856 iterator end() _NOEXCEPT
857 {return iterator(__get_pointer() + size());}
858 _LIBCPP_INLINE_VISIBILITY
859 const_iterator end() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000860 {return const_iterator(__get_pointer() + size());}
Howard Hinnant499cea12013-08-23 17:37:05 +0000861#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnanta6119a82011-05-29 19:57:12 +0000862 _LIBCPP_INLINE_VISIBILITY
863 reverse_iterator rbegin() _NOEXCEPT
864 {return reverse_iterator(end());}
865 _LIBCPP_INLINE_VISIBILITY
866 const_reverse_iterator rbegin() const _NOEXCEPT
867 {return const_reverse_iterator(end());}
868 _LIBCPP_INLINE_VISIBILITY
869 reverse_iterator rend() _NOEXCEPT
870 {return reverse_iterator(begin());}
871 _LIBCPP_INLINE_VISIBILITY
872 const_reverse_iterator rend() const _NOEXCEPT
873 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000874
Howard Hinnanta6119a82011-05-29 19:57:12 +0000875 _LIBCPP_INLINE_VISIBILITY
876 const_iterator cbegin() const _NOEXCEPT
877 {return begin();}
878 _LIBCPP_INLINE_VISIBILITY
879 const_iterator cend() const _NOEXCEPT
880 {return end();}
881 _LIBCPP_INLINE_VISIBILITY
882 const_reverse_iterator crbegin() const _NOEXCEPT
883 {return rbegin();}
884 _LIBCPP_INLINE_VISIBILITY
885 const_reverse_iterator crend() const _NOEXCEPT
886 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000887
Howard Hinnanta6119a82011-05-29 19:57:12 +0000888 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000890 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
891 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
892 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +0000893 {return (__is_long() ? __get_long_cap()
894 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000895
896 void resize(size_type __n, value_type __c);
897 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
898
Eric Fiselier59e24fe2017-06-01 02:29:37 +0000899 void reserve(size_type __res_arg = 0);
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000900 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000901 void shrink_to_fit() _NOEXCEPT {reserve();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +0000903 void clear() _NOEXCEPT;
Marshall Clowf1729d92017-11-15 20:02:27 +0000904 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
905 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000907 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
908 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909
910 const_reference at(size_type __n) const;
911 reference at(size_type __n);
912
913 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000914 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(__self_view __sv) {return append(__sv);}
915 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000916 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselier3e928972017-04-19 00:28:44 +0000917#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000918 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Eric Fiselier3e928972017-04-19 00:28:44 +0000919#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000920
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000922 basic_string& append(const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000923 _LIBCPP_INLINE_VISIBILITY
924 basic_string& append(__self_view __sv) { return append(__sv.data(), __sv.size()); }
Marshall Clowa93b5e22014-03-04 19:17:19 +0000925 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +0000926 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000927 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
928 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +0000929 <
930 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
931 basic_string&
932 >::type
933 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000934 basic_string& append(const value_type* __s, size_type __n);
935 basic_string& append(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936 basic_string& append(size_type __n, value_type __c);
Eric Fiselier026d38e2016-10-31 02:46:25 +0000937 template <class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000938 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
939 basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000941 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
942 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000943 <
Marshall Clowdf9db312016-01-13 21:54:34 +0000944 __is_exactly_input_iterator<_InputIterator>::value
945 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000946 basic_string&
947 >::type
Eric Fiselier026d38e2016-10-31 02:46:25 +0000948 _LIBCPP_INLINE_VISIBILITY
949 append(_InputIterator __first, _InputIterator __last) {
950 const basic_string __temp (__first, __last, __alloc());
951 append(__temp.data(), __temp.size());
952 return *this;
953 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000954 template<class _ForwardIterator>
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_forward_iterator<_ForwardIterator>::value
959 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::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(_ForwardIterator __first, _ForwardIterator __last) {
964 return __append_forward_unsafe(__first, __last);
965 }
966
Eric Fiselier3e928972017-04-19 00:28:44 +0000967#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000969 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Eric Fiselier3e928972017-04-19 00:28:44 +0000970#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971
972 void push_back(value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974 void pop_back();
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000975 _LIBCPP_INLINE_VISIBILITY reference front();
976 _LIBCPP_INLINE_VISIBILITY const_reference front() const;
977 _LIBCPP_INLINE_VISIBILITY reference back();
978 _LIBCPP_INLINE_VISIBILITY const_reference back() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000980 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000981 basic_string& assign(__self_view __sv) { return assign(__sv.data(), __sv.size()); }
982 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf40ec902016-03-09 18:08:29 +0000983 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselier3e928972017-04-19 00:28:44 +0000984#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta6119a82011-05-29 19:57:12 +0000985 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier59e24fe2017-06-01 02:29:37 +0000986 basic_string& assign(basic_string&& __str)
Marshall Clow7ed093b2015-10-05 16:17:34 +0000987 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselier59e24fe2017-06-01 02:29:37 +0000988 {*this = _VSTD::move(__str); return *this;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000989#endif
Marshall Clowa93b5e22014-03-04 19:17:19 +0000990 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +0000991 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000992 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
993 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +0000994 <
995 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
996 basic_string&
997 >::type
Eric Fiselier59e24fe2017-06-01 02:29:37 +0000998 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000999 basic_string& assign(const value_type* __s, size_type __n);
1000 basic_string& assign(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001001 basic_string& assign(size_type __n, value_type __c);
1002 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001003 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1004 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001005 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001006 __is_exactly_input_iterator<_InputIterator>::value
1007 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001008 basic_string&
1009 >::type
1010 assign(_InputIterator __first, _InputIterator __last);
1011 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001012 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1013 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001014 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001015 __is_forward_iterator<_ForwardIterator>::value
1016 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001017 basic_string&
1018 >::type
1019 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselier3e928972017-04-19 00:28:44 +00001020#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001022 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001023#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001024
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001026 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001027 _LIBCPP_INLINE_VISIBILITY
1028 basic_string& insert(size_type __pos1, __self_view __sv) { return insert(__pos1, __sv.data(), __sv.size()); }
Marshall Clow6ac8de02016-09-24 22:45:42 +00001029 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001030 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1031 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001032 <
1033 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1034 basic_string&
1035 >::type
1036 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clowa93b5e22014-03-04 19:17:19 +00001037 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001038 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1039 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001040 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1041 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001043 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1044 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001045 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1046 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001047 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001048 __is_exactly_input_iterator<_InputIterator>::value
1049 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050 iterator
1051 >::type
1052 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1053 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001054 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1055 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001056 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001057 __is_forward_iterator<_ForwardIterator>::value
1058 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059 iterator
1060 >::type
1061 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselier3e928972017-04-19 00:28:44 +00001062#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001063 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001064 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1065 {return insert(__pos, __il.begin(), __il.end());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001066#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067
1068 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001069 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001070 iterator erase(const_iterator __pos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001071 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001072 iterator erase(const_iterator __first, const_iterator __last);
1073
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001074 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001076 _LIBCPP_INLINE_VISIBILITY
1077 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 +00001078 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 +00001079 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001080 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1081 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001082 <
1083 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1084 basic_string&
1085 >::type
1086 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001087 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1088 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001089 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001090 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001091 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001092 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001093 basic_string& replace(const_iterator __i1, const_iterator __i2, __self_view __sv) { return replace(__i1 - begin(), __i2 - __i1, __sv); }
1094 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001095 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001096 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001097 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001098 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001099 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001100 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001101 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1102 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001103 <
1104 __is_input_iterator<_InputIterator>::value,
1105 basic_string&
1106 >::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001107 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselier3e928972017-04-19 00:28:44 +00001108#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001109 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001110 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001111 {return replace(__i1, __i2, __il.begin(), __il.end());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001112#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001113
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001114 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001115 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001116 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1117
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001119 void swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00001120#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00001121 _NOEXCEPT_DEBUG;
Marshall Clow7d914d12015-07-13 20:04:56 +00001122#else
Eric Fiselier47257c42016-12-28 05:53:01 +00001123 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00001124 __is_nothrow_swappable<allocator_type>::value);
1125#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001126
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001128 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001130 const value_type* data() const _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
Eric Fiselier10bebe22017-11-20 20:23:27 +00001131#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowf532a702016-03-08 15:44:30 +00001132 _LIBCPP_INLINE_VISIBILITY
1133 value_type* data() _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
1134#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001135
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001137 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001138
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001140 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001141 _LIBCPP_INLINE_VISIBILITY
1142 size_type find(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001143 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001145 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001146 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001147
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001149 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001150 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0b9db072017-09-07 04:19:32 +00001151 size_type rfind(__self_view __sv, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001152 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001154 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001155 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001156
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001157 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001158 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001159 _LIBCPP_INLINE_VISIBILITY
1160 size_type find_first_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001161 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001163 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001165 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001166
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001168 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001169 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0b9db072017-09-07 04:19:32 +00001170 size_type find_last_of(__self_view __sv, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001171 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001173 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001175 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001176
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001178 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001179 _LIBCPP_INLINE_VISIBILITY
1180 size_type find_first_not_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001181 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 +00001182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001183 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001184 _LIBCPP_INLINE_VISIBILITY
1185 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1186
1187 _LIBCPP_INLINE_VISIBILITY
1188 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001189 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0b9db072017-09-07 04:19:32 +00001190 size_type find_last_not_of(__self_view __sv, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001191 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 +00001192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001193 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001194 _LIBCPP_INLINE_VISIBILITY
1195 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1196
1197 _LIBCPP_INLINE_VISIBILITY
1198 int compare(const basic_string& __str) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001199 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001200 int compare(__self_view __sv) const _NOEXCEPT;
1201 _LIBCPP_INLINE_VISIBILITY
1202 int compare(size_type __pos1, size_type __n1, __self_view __sv) const;
1203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001204 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clowa93b5e22014-03-04 19:17:19 +00001205 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 +00001206 template <class _Tp>
Eric Fiselier9dbc0532016-10-14 05:29:46 +00001207 inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow6ac8de02016-09-24 22:45:42 +00001208 typename enable_if
1209 <
1210 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1211 int
1212 >::type
1213 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 +00001214 int compare(const value_type* __s) const _NOEXCEPT;
1215 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1216 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001217
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001218 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnant08dd2532013-04-22 23:55:13 +00001219
1220 _LIBCPP_INLINE_VISIBILITY
1221 bool __is_long() const _NOEXCEPT
1222 {return bool(__r_.first().__s.__size_ & __short_mask);}
1223
Howard Hinnant499cea12013-08-23 17:37:05 +00001224#if _LIBCPP_DEBUG_LEVEL >= 2
1225
1226 bool __dereferenceable(const const_iterator* __i) const;
1227 bool __decrementable(const const_iterator* __i) const;
1228 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1229 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1230
1231#endif // _LIBCPP_DEBUG_LEVEL >= 2
1232
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233private:
Howard Hinnanta6119a82011-05-29 19:57:12 +00001234 _LIBCPP_INLINE_VISIBILITY
1235 allocator_type& __alloc() _NOEXCEPT
1236 {return __r_.second();}
1237 _LIBCPP_INLINE_VISIBILITY
1238 const allocator_type& __alloc() const _NOEXCEPT
1239 {return __r_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001240
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001241#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001242
Howard Hinnanta6119a82011-05-29 19:57:12 +00001243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001244 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001245# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001246 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant15467182013-04-30 21:44:48 +00001247# else
1248 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1249# endif
1250
Howard Hinnanta6119a82011-05-29 19:57:12 +00001251 _LIBCPP_INLINE_VISIBILITY
1252 size_type __get_short_size() const _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001253# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001254 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant15467182013-04-30 21:44:48 +00001255# else
1256 {return __r_.first().__s.__size_;}
1257# endif
1258
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001259#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001260
1261 _LIBCPP_INLINE_VISIBILITY
1262 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001263# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant15467182013-04-30 21:44:48 +00001264 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1265# else
1266 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1267# endif
1268
1269 _LIBCPP_INLINE_VISIBILITY
1270 size_type __get_short_size() const _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001271# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant15467182013-04-30 21:44:48 +00001272 {return __r_.first().__s.__size_;}
1273# else
1274 {return __r_.first().__s.__size_ >> 1;}
1275# endif
1276
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001277#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001278
Howard Hinnanta6119a82011-05-29 19:57:12 +00001279 _LIBCPP_INLINE_VISIBILITY
1280 void __set_long_size(size_type __s) _NOEXCEPT
1281 {__r_.first().__l.__size_ = __s;}
1282 _LIBCPP_INLINE_VISIBILITY
1283 size_type __get_long_size() const _NOEXCEPT
1284 {return __r_.first().__l.__size_;}
1285 _LIBCPP_INLINE_VISIBILITY
1286 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001287 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1288
Howard Hinnanta6119a82011-05-29 19:57:12 +00001289 _LIBCPP_INLINE_VISIBILITY
1290 void __set_long_cap(size_type __s) _NOEXCEPT
1291 {__r_.first().__l.__cap_ = __long_mask | __s;}
1292 _LIBCPP_INLINE_VISIBILITY
1293 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnantec3773c2011-12-01 20:21:04 +00001294 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295
Howard Hinnanta6119a82011-05-29 19:57:12 +00001296 _LIBCPP_INLINE_VISIBILITY
1297 void __set_long_pointer(pointer __p) _NOEXCEPT
1298 {__r_.first().__l.__data_ = __p;}
1299 _LIBCPP_INLINE_VISIBILITY
1300 pointer __get_long_pointer() _NOEXCEPT
1301 {return __r_.first().__l.__data_;}
1302 _LIBCPP_INLINE_VISIBILITY
1303 const_pointer __get_long_pointer() const _NOEXCEPT
1304 {return __r_.first().__l.__data_;}
1305 _LIBCPP_INLINE_VISIBILITY
1306 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001307 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001308 _LIBCPP_INLINE_VISIBILITY
1309 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001310 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001311 _LIBCPP_INLINE_VISIBILITY
1312 pointer __get_pointer() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001313 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001314 _LIBCPP_INLINE_VISIBILITY
1315 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001316 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1317
Howard Hinnanta6119a82011-05-29 19:57:12 +00001318 _LIBCPP_INLINE_VISIBILITY
1319 void __zero() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001320 {
1321 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1322 for (unsigned __i = 0; __i < __n_words; ++__i)
1323 __a[__i] = 0;
1324 }
1325
1326 template <size_type __a> static
Howard Hinnanta6119a82011-05-29 19:57:12 +00001327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00001328 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +00001329 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001330 enum {__alignment = 16};
Howard Hinnanta6119a82011-05-29 19:57:12 +00001331 static _LIBCPP_INLINE_VISIBILITY
1332 size_type __recommend(size_type __s) _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +00001333 {return (__s < __min_cap ? static_cast<size_type>(__min_cap) :
Howard Hinnant7f764502013-08-14 18:00:20 +00001334 __align_it<sizeof(value_type) < __alignment ?
Howard Hinnanta6119a82011-05-29 19:57:12 +00001335 __alignment/sizeof(value_type) : 1 > (__s+1)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001336
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001337 inline
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001338 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001339 inline
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001340 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001341 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001342 void __init(size_type __n, value_type __c);
Howard Hinnant324bb032010-08-22 00:02:43 +00001343
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344 template <class _InputIterator>
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001345 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001346 typename enable_if
1347 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001348 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001349 void
1350 >::type
1351 __init(_InputIterator __first, _InputIterator __last);
1352
1353 template <class _ForwardIterator>
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001354 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001355 typename enable_if
1356 <
1357 __is_forward_iterator<_ForwardIterator>::value,
1358 void
1359 >::type
1360 __init(_ForwardIterator __first, _ForwardIterator __last);
1361
1362 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant324bb032010-08-22 00:02:43 +00001363 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001364 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1365 size_type __n_copy, size_type __n_del,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001366 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001367
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001369 void __erase_to_end(size_type __pos);
1370
Howard Hinnante32b5e22010-11-17 17:55:08 +00001371 _LIBCPP_INLINE_VISIBILITY
1372 void __copy_assign_alloc(const basic_string& __str)
1373 {__copy_assign_alloc(__str, integral_constant<bool,
1374 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1375
1376 _LIBCPP_INLINE_VISIBILITY
1377 void __copy_assign_alloc(const basic_string& __str, true_type)
1378 {
Marshall Clow9247fd22017-01-31 03:40:52 +00001379 if (__alloc() == __str.__alloc())
1380 __alloc() = __str.__alloc();
1381 else
Howard Hinnante32b5e22010-11-17 17:55:08 +00001382 {
Marshall Clow9247fd22017-01-31 03:40:52 +00001383 if (!__str.__is_long())
1384 {
1385 clear();
1386 shrink_to_fit();
1387 __alloc() = __str.__alloc();
1388 }
1389 else
1390 {
1391 allocator_type __a = __str.__alloc();
1392 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
1393 clear();
1394 shrink_to_fit();
1395 __alloc() = _VSTD::move(__a);
1396 __set_long_pointer(__p);
1397 __set_long_cap(__str.__get_long_cap());
1398 __set_long_size(__str.size());
1399 }
Howard Hinnante32b5e22010-11-17 17:55:08 +00001400 }
Howard Hinnante32b5e22010-11-17 17:55:08 +00001401 }
1402
1403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001404 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnante32b5e22010-11-17 17:55:08 +00001405 {}
1406
Eric Fiselier3e928972017-04-19 00:28:44 +00001407#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001408 _LIBCPP_INLINE_VISIBILITY
Marshall Clowaf961ed2015-08-18 18:57:00 +00001409 void __move_assign(basic_string& __str, false_type)
1410 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001412 void __move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001413#if _LIBCPP_STD_VER > 14
1414 _NOEXCEPT;
1415#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001416 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001417#endif
Marshall Clowaf961ed2015-08-18 18:57:00 +00001418#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00001419
1420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001421 void
Howard Hinnant9cbee432011-09-02 20:42:31 +00001422 __move_assign_alloc(basic_string& __str)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001423 _NOEXCEPT_(
1424 !__alloc_traits::propagate_on_container_move_assignment::value ||
1425 is_nothrow_move_assignable<allocator_type>::value)
1426 {__move_assign_alloc(__str, integral_constant<bool,
1427 __alloc_traits::propagate_on_container_move_assignment::value>());}
1428
1429 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00001430 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001431 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1432 {
1433 __alloc() = _VSTD::move(__c.__alloc());
1434 }
1435
1436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001437 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001438 _NOEXCEPT
1439 {}
1440
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001441 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1442 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443
1444 friend basic_string operator+<>(const basic_string&, const basic_string&);
1445 friend basic_string operator+<>(const value_type*, const basic_string&);
1446 friend basic_string operator+<>(value_type, const basic_string&);
1447 friend basic_string operator+<>(const basic_string&, const value_type*);
1448 friend basic_string operator+<>(const basic_string&, value_type);
1449};
1450
1451template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001452inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453void
1454basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1455{
Howard Hinnant499cea12013-08-23 17:37:05 +00001456#if _LIBCPP_DEBUG_LEVEL >= 2
1457 __get_db()->__invalidate_all(this);
1458#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459}
1460
1461template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001462inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463void
Howard Hinnantec3773c2011-12-01 20:21:04 +00001464basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Howard Hinnant499cea12013-08-23 17:37:05 +00001465#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantec3773c2011-12-01 20:21:04 +00001466 __pos
1467#endif
1468 )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469{
Howard Hinnant499cea12013-08-23 17:37:05 +00001470#if _LIBCPP_DEBUG_LEVEL >= 2
1471 __c_node* __c = __get_db()->__find_c_and_lock(this);
1472 if (__c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001474 const_pointer __new_last = __get_pointer() + __pos;
1475 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001476 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001477 --__p;
1478 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1479 if (__i->base() > __new_last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001480 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001481 (*__p)->__c_ = nullptr;
1482 if (--__c->end_ != __p)
1483 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001484 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001486 __get_db()->unlock();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001488#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001489}
1490
1491template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001492inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001493basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowc912c0c2015-06-04 02:05:41 +00001494 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001495{
Howard Hinnant499cea12013-08-23 17:37:05 +00001496#if _LIBCPP_DEBUG_LEVEL >= 2
1497 __get_db()->__insert_c(this);
1498#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001499 __zero();
1500}
1501
1502template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001503inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiselier692177d2015-07-18 20:40:46 +00001505#if _LIBCPP_STD_VER <= 14
1506 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1507#else
1508 _NOEXCEPT
1509#endif
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001510: __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511{
Howard Hinnant499cea12013-08-23 17:37:05 +00001512#if _LIBCPP_DEBUG_LEVEL >= 2
1513 __get_db()->__insert_c(this);
1514#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515 __zero();
1516}
1517
1518template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier6dbed462016-09-16 00:00:48 +00001519void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1520 size_type __sz,
1521 size_type __reserve)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001522{
1523 if (__reserve > max_size())
1524 this->__throw_length_error();
1525 pointer __p;
1526 if (__reserve < __min_cap)
1527 {
1528 __set_short_size(__sz);
1529 __p = __get_short_pointer();
1530 }
1531 else
1532 {
1533 size_type __cap = __recommend(__reserve);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001534 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535 __set_long_pointer(__p);
1536 __set_long_cap(__cap+1);
1537 __set_long_size(__sz);
1538 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001539 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001540 traits_type::assign(__p[__sz], value_type());
1541}
1542
1543template <class _CharT, class _Traits, class _Allocator>
1544void
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001545basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001546{
1547 if (__sz > max_size())
1548 this->__throw_length_error();
1549 pointer __p;
1550 if (__sz < __min_cap)
1551 {
1552 __set_short_size(__sz);
1553 __p = __get_short_pointer();
1554 }
1555 else
1556 {
1557 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001558 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001559 __set_long_pointer(__p);
1560 __set_long_cap(__cap+1);
1561 __set_long_size(__sz);
1562 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001563 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564 traits_type::assign(__p[__sz], value_type());
1565}
1566
1567template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001568inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001569basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001570{
Howard Hinnant499cea12013-08-23 17:37:05 +00001571 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001572 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001573#if _LIBCPP_DEBUG_LEVEL >= 2
1574 __get_db()->__insert_c(this);
1575#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001576}
1577
1578template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001579inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001580basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001581 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001582{
Howard Hinnant499cea12013-08-23 17:37:05 +00001583 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001585#if _LIBCPP_DEBUG_LEVEL >= 2
1586 __get_db()->__insert_c(this);
1587#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001588}
1589
1590template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001591inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001592basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593{
Howard Hinnant499cea12013-08-23 17:37:05 +00001594 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00001596#if _LIBCPP_DEBUG_LEVEL >= 2
1597 __get_db()->__insert_c(this);
1598#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599}
1600
1601template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001602inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001603basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001604 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001605{
Howard Hinnant499cea12013-08-23 17:37:05 +00001606 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00001608#if _LIBCPP_DEBUG_LEVEL >= 2
1609 __get_db()->__insert_c(this);
1610#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611}
1612
1613template <class _CharT, class _Traits, class _Allocator>
1614basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001615 : __r_(__second_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001616{
1617 if (!__str.__is_long())
1618 __r_.first().__r = __str.__r_.first().__r;
1619 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001620 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001621#if _LIBCPP_DEBUG_LEVEL >= 2
1622 __get_db()->__insert_c(this);
1623#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624}
1625
1626template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001627basic_string<_CharT, _Traits, _Allocator>::basic_string(
1628 const basic_string& __str, const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001629 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001630{
1631 if (!__str.__is_long())
1632 __r_.first().__r = __str.__r_.first().__r;
1633 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001634 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001635#if _LIBCPP_DEBUG_LEVEL >= 2
1636 __get_db()->__insert_c(this);
1637#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001638}
1639
Eric Fiselier3e928972017-04-19 00:28:44 +00001640#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641
1642template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001643inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001644basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +00001645#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001646 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow7b193f72015-06-03 19:56:43 +00001647#else
1648 _NOEXCEPT
1649#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001650 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001651{
1652 __str.__zero();
Howard Hinnant499cea12013-08-23 17:37:05 +00001653#if _LIBCPP_DEBUG_LEVEL >= 2
1654 __get_db()->__insert_c(this);
1655 if (__is_long())
1656 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001657#endif
1658}
1659
1660template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001661inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001662basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001663 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001664{
Marshall Clowd5549cc2014-07-17 15:32:20 +00001665 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001666 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd5549cc2014-07-17 15:32:20 +00001667 else
1668 {
1669 __r_.first().__r = __str.__r_.first().__r;
1670 __str.__zero();
1671 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001672#if _LIBCPP_DEBUG_LEVEL >= 2
1673 __get_db()->__insert_c(this);
1674 if (__is_long())
1675 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676#endif
1677}
1678
Eric Fiselier3e928972017-04-19 00:28:44 +00001679#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001680
1681template <class _CharT, class _Traits, class _Allocator>
1682void
1683basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1684{
1685 if (__n > max_size())
1686 this->__throw_length_error();
1687 pointer __p;
1688 if (__n < __min_cap)
1689 {
1690 __set_short_size(__n);
1691 __p = __get_short_pointer();
1692 }
1693 else
1694 {
1695 size_type __cap = __recommend(__n);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001696 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001697 __set_long_pointer(__p);
1698 __set_long_cap(__cap+1);
1699 __set_long_size(__n);
1700 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001701 traits_type::assign(_VSTD::__to_raw_pointer(__p), __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001702 traits_type::assign(__p[__n], value_type());
1703}
1704
1705template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001706inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001707basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001708{
1709 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00001710#if _LIBCPP_DEBUG_LEVEL >= 2
1711 __get_db()->__insert_c(this);
1712#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001713}
1714
1715template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001716inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001717basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001718 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001719{
1720 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00001721#if _LIBCPP_DEBUG_LEVEL >= 2
1722 __get_db()->__insert_c(this);
1723#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724}
1725
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001726template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001727basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
1728 size_type __pos, size_type __n,
1729 const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001730 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001731{
1732 size_type __str_sz = __str.size();
1733 if (__pos > __str_sz)
1734 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001735 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Howard Hinnant499cea12013-08-23 17:37:05 +00001736#if _LIBCPP_DEBUG_LEVEL >= 2
1737 __get_db()->__insert_c(this);
1738#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739}
1740
1741template <class _CharT, class _Traits, class _Allocator>
Marshall Clowf4f7d8f2016-04-07 18:13:41 +00001742inline _LIBCPP_INLINE_VISIBILITY
1743basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001744 const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001745 : __r_(__second_tag(), __a)
Marshall Clowf4f7d8f2016-04-07 18:13:41 +00001746{
1747 size_type __str_sz = __str.size();
1748 if (__pos > __str_sz)
1749 this->__throw_out_of_range();
1750 __init(__str.data() + __pos, __str_sz - __pos);
1751#if _LIBCPP_DEBUG_LEVEL >= 2
1752 __get_db()->__insert_c(this);
1753#endif
1754}
1755
1756template <class _CharT, class _Traits, class _Allocator>
Marshall Clowdb7fa112016-11-14 18:22:19 +00001757template <class _Tp>
1758basic_string<_CharT, _Traits, _Allocator>::basic_string(
1759 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a,
Marshall Clowf1729d92017-11-15 20:02:27 +00001760 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type *)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001761 : __r_(__second_tag(), __a)
Marshall Clowdb7fa112016-11-14 18:22:19 +00001762{
Marshall Clowf1729d92017-11-15 20:02:27 +00001763 __self_view __sv = __self_view(__t).substr(__pos, __n);
Marshall Clowdb7fa112016-11-14 18:22:19 +00001764 __init(__sv.data(), __sv.size());
1765#if _LIBCPP_DEBUG_LEVEL >= 2
1766 __get_db()->__insert_c(this);
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001767#endif
Marshall Clowdb7fa112016-11-14 18:22:19 +00001768}
1769
1770template <class _CharT, class _Traits, class _Allocator>
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001771inline _LIBCPP_INLINE_VISIBILITY
1772basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv)
1773{
1774 __init(__sv.data(), __sv.size());
1775#if _LIBCPP_DEBUG_LEVEL >= 2
1776 __get_db()->__insert_c(this);
1777#endif
1778}
1779
1780template <class _CharT, class _Traits, class _Allocator>
1781inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001782basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001783 : __r_(__second_tag(), __a)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001784{
1785 __init(__sv.data(), __sv.size());
1786#if _LIBCPP_DEBUG_LEVEL >= 2
1787 __get_db()->__insert_c(this);
1788#endif
1789}
1790
1791template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001792template <class _InputIterator>
1793typename enable_if
1794<
Marshall Clowdf9db312016-01-13 21:54:34 +00001795 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796 void
1797>::type
1798basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
1799{
1800 __zero();
1801#ifndef _LIBCPP_NO_EXCEPTIONS
1802 try
1803 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001804#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805 for (; __first != __last; ++__first)
1806 push_back(*__first);
1807#ifndef _LIBCPP_NO_EXCEPTIONS
1808 }
1809 catch (...)
1810 {
1811 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001812 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001813 throw;
1814 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001815#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001816}
1817
1818template <class _CharT, class _Traits, class _Allocator>
1819template <class _ForwardIterator>
1820typename enable_if
1821<
1822 __is_forward_iterator<_ForwardIterator>::value,
1823 void
1824>::type
1825basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
1826{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001827 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001828 if (__sz > max_size())
1829 this->__throw_length_error();
1830 pointer __p;
1831 if (__sz < __min_cap)
1832 {
1833 __set_short_size(__sz);
1834 __p = __get_short_pointer();
1835 }
1836 else
1837 {
1838 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001839 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840 __set_long_pointer(__p);
1841 __set_long_cap(__cap+1);
1842 __set_long_size(__sz);
1843 }
Eric Fiselierb9919752014-10-27 19:28:20 +00001844 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001845 traits_type::assign(*__p, *__first);
1846 traits_type::assign(*__p, value_type());
1847}
1848
1849template <class _CharT, class _Traits, class _Allocator>
1850template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001851inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
1853{
1854 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001855#if _LIBCPP_DEBUG_LEVEL >= 2
1856 __get_db()->__insert_c(this);
1857#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001858}
1859
1860template <class _CharT, class _Traits, class _Allocator>
1861template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001862inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
1864 const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001865 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001866{
1867 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001868#if _LIBCPP_DEBUG_LEVEL >= 2
1869 __get_db()->__insert_c(this);
1870#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001871}
1872
Eric Fiselier3e928972017-04-19 00:28:44 +00001873#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00001874
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001875template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001876inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001877basic_string<_CharT, _Traits, _Allocator>::basic_string(
1878 initializer_list<_CharT> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001879{
1880 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001881#if _LIBCPP_DEBUG_LEVEL >= 2
1882 __get_db()->__insert_c(this);
1883#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001884}
1885
1886template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001887inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001888
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001889basic_string<_CharT, _Traits, _Allocator>::basic_string(
1890 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001891 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001892{
1893 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001894#if _LIBCPP_DEBUG_LEVEL >= 2
1895 __get_db()->__insert_c(this);
1896#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001897}
1898
Eric Fiselier3e928972017-04-19 00:28:44 +00001899#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00001900
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001901template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001902basic_string<_CharT, _Traits, _Allocator>::~basic_string()
1903{
Howard Hinnant499cea12013-08-23 17:37:05 +00001904#if _LIBCPP_DEBUG_LEVEL >= 2
1905 __get_db()->__erase_c(this);
1906#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001907 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001908 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001909}
1910
1911template <class _CharT, class _Traits, class _Allocator>
1912void
1913basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
1914 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001915 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 +00001916{
1917 size_type __ms = max_size();
1918 if (__delta_cap > __ms - __old_cap - 1)
1919 this->__throw_length_error();
1920 pointer __old_p = __get_pointer();
1921 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00001922 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001923 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001924 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001925 __invalidate_all_iterators();
1926 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001927 traits_type::copy(_VSTD::__to_raw_pointer(__p),
1928 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001929 if (__n_add != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001930 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1932 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001933 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
1934 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001935 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001936 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937 __set_long_pointer(__p);
1938 __set_long_cap(__cap+1);
1939 __old_sz = __n_copy + __n_add + __sec_cp_sz;
1940 __set_long_size(__old_sz);
1941 traits_type::assign(__p[__old_sz], value_type());
1942}
1943
1944template <class _CharT, class _Traits, class _Allocator>
1945void
1946basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1947 size_type __n_copy, size_type __n_del, size_type __n_add)
1948{
1949 size_type __ms = max_size();
Marshall Clowecc8d7b2013-11-06 14:24:38 +00001950 if (__delta_cap > __ms - __old_cap)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001951 this->__throw_length_error();
1952 pointer __old_p = __get_pointer();
1953 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00001954 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001955 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001956 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001957 __invalidate_all_iterators();
1958 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001959 traits_type::copy(_VSTD::__to_raw_pointer(__p),
1960 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001961 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1962 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001963 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
1964 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del,
1965 __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001966 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001967 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968 __set_long_pointer(__p);
1969 __set_long_cap(__cap+1);
1970}
1971
1972// assign
1973
1974template <class _CharT, class _Traits, class _Allocator>
1975basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001976basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001977{
Alp Tokerec34c482014-05-15 11:27:39 +00001978 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979 size_type __cap = capacity();
1980 if (__cap >= __n)
1981 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001982 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001983 traits_type::move(__p, __s, __n);
1984 traits_type::assign(__p[__n], value_type());
1985 __set_size(__n);
1986 __invalidate_iterators_past(__n);
1987 }
1988 else
1989 {
1990 size_type __sz = size();
1991 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
1992 }
1993 return *this;
1994}
1995
1996template <class _CharT, class _Traits, class _Allocator>
1997basic_string<_CharT, _Traits, _Allocator>&
1998basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
1999{
2000 size_type __cap = capacity();
2001 if (__cap < __n)
2002 {
2003 size_type __sz = size();
2004 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2005 }
2006 else
2007 __invalidate_iterators_past(__n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002008 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002009 traits_type::assign(__p, __n, __c);
2010 traits_type::assign(__p[__n], value_type());
2011 __set_size(__n);
2012 return *this;
2013}
2014
2015template <class _CharT, class _Traits, class _Allocator>
2016basic_string<_CharT, _Traits, _Allocator>&
2017basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2018{
2019 pointer __p;
2020 if (__is_long())
2021 {
2022 __p = __get_long_pointer();
2023 __set_long_size(1);
2024 }
2025 else
2026 {
2027 __p = __get_short_pointer();
2028 __set_short_size(1);
2029 }
2030 traits_type::assign(*__p, __c);
2031 traits_type::assign(*++__p, value_type());
2032 __invalidate_iterators_past(1);
2033 return *this;
2034}
2035
2036template <class _CharT, class _Traits, class _Allocator>
Howard Hinnante32b5e22010-11-17 17:55:08 +00002037basic_string<_CharT, _Traits, _Allocator>&
2038basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2039{
2040 if (this != &__str)
2041 {
2042 __copy_assign_alloc(__str);
Marshall Clowf40ec902016-03-09 18:08:29 +00002043 assign(__str.data(), __str.size());
Howard Hinnante32b5e22010-11-17 17:55:08 +00002044 }
2045 return *this;
2046}
2047
Eric Fiselier3e928972017-04-19 00:28:44 +00002048#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante32b5e22010-11-17 17:55:08 +00002049
2050template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002051inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002052void
2053basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002054 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002055{
2056 if (__alloc() != __str.__alloc())
2057 assign(__str);
2058 else
2059 __move_assign(__str, true_type());
2060}
2061
2062template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002063inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002064void
2065basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002066#if _LIBCPP_STD_VER > 14
2067 _NOEXCEPT
2068#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00002069 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002070#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00002071{
2072 clear();
2073 shrink_to_fit();
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00002074 __r_.first() = __str.__r_.first();
2075 __move_assign_alloc(__str);
Howard Hinnante32b5e22010-11-17 17:55:08 +00002076 __str.__zero();
2077}
2078
2079template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002080inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002081basic_string<_CharT, _Traits, _Allocator>&
2082basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002083 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnante32b5e22010-11-17 17:55:08 +00002084{
2085 __move_assign(__str, integral_constant<bool,
2086 __alloc_traits::propagate_on_container_move_assignment::value>());
2087 return *this;
2088}
2089
2090#endif
2091
2092template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002093template<class _InputIterator>
2094typename enable_if
2095<
Marshall Clowdf9db312016-01-13 21:54:34 +00002096 __is_exactly_input_iterator <_InputIterator>::value
2097 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002098 basic_string<_CharT, _Traits, _Allocator>&
2099>::type
2100basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2101{
Marshall Clowd979eed2016-09-05 01:54:30 +00002102 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002103 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002104 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002105}
2106
2107template <class _CharT, class _Traits, class _Allocator>
2108template<class _ForwardIterator>
2109typename enable_if
2110<
Marshall Clowdf9db312016-01-13 21:54:34 +00002111 __is_forward_iterator<_ForwardIterator>::value
2112 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002113 basic_string<_CharT, _Traits, _Allocator>&
2114>::type
2115basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2116{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002117 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118 size_type __cap = capacity();
2119 if (__cap < __n)
2120 {
2121 size_type __sz = size();
2122 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2123 }
2124 else
2125 __invalidate_iterators_past(__n);
2126 pointer __p = __get_pointer();
2127 for (; __first != __last; ++__first, ++__p)
2128 traits_type::assign(*__p, *__first);
2129 traits_type::assign(*__p, value_type());
2130 __set_size(__n);
2131 return *this;
2132}
2133
2134template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002135basic_string<_CharT, _Traits, _Allocator>&
2136basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2137{
2138 size_type __sz = __str.size();
2139 if (__pos > __sz)
2140 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002141 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002142}
2143
2144template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002145template <class _Tp>
2146typename enable_if
2147<
2148 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
Marshall Clowf1729d92017-11-15 20:02:27 +00002149 basic_string<_CharT, _Traits, _Allocator>&
Marshall Clow6ac8de02016-09-24 22:45:42 +00002150>::type
2151basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002152{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002153 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002154 size_type __sz = __sv.size();
2155 if (__pos > __sz)
2156 this->__throw_out_of_range();
2157 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2158}
2159
2160
2161template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002163basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002164{
Alp Tokerec34c482014-05-15 11:27:39 +00002165 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166 return assign(__s, traits_type::length(__s));
2167}
2168
2169// append
2170
2171template <class _CharT, class _Traits, class _Allocator>
2172basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002173basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002174{
Alp Tokerec34c482014-05-15 11:27:39 +00002175 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002176 size_type __cap = capacity();
2177 size_type __sz = size();
2178 if (__cap - __sz >= __n)
2179 {
2180 if (__n)
2181 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002182 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002183 traits_type::copy(__p + __sz, __s, __n);
2184 __sz += __n;
2185 __set_size(__sz);
2186 traits_type::assign(__p[__sz], value_type());
2187 }
2188 }
2189 else
2190 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2191 return *this;
2192}
2193
2194template <class _CharT, class _Traits, class _Allocator>
2195basic_string<_CharT, _Traits, _Allocator>&
2196basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2197{
2198 if (__n)
2199 {
2200 size_type __cap = capacity();
2201 size_type __sz = size();
2202 if (__cap - __sz < __n)
2203 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2204 pointer __p = __get_pointer();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002205 traits_type::assign(_VSTD::__to_raw_pointer(__p) + __sz, __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002206 __sz += __n;
2207 __set_size(__sz);
2208 traits_type::assign(__p[__sz], value_type());
2209 }
2210 return *this;
2211}
2212
2213template <class _CharT, class _Traits, class _Allocator>
2214void
2215basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2216{
Howard Hinnant15467182013-04-30 21:44:48 +00002217 bool __is_short = !__is_long();
2218 size_type __cap;
2219 size_type __sz;
2220 if (__is_short)
2221 {
2222 __cap = __min_cap - 1;
2223 __sz = __get_short_size();
2224 }
2225 else
2226 {
2227 __cap = __get_long_cap() - 1;
2228 __sz = __get_long_size();
2229 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002230 if (__sz == __cap)
Howard Hinnant15467182013-04-30 21:44:48 +00002231 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002232 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant15467182013-04-30 21:44:48 +00002233 __is_short = !__is_long();
2234 }
2235 pointer __p;
2236 if (__is_short)
2237 {
2238 __p = __get_short_pointer() + __sz;
2239 __set_short_size(__sz+1);
2240 }
2241 else
2242 {
2243 __p = __get_long_pointer() + __sz;
2244 __set_long_size(__sz+1);
2245 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002246 traits_type::assign(*__p, __c);
2247 traits_type::assign(*++__p, value_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002248}
2249
Marshall Clowac655ef2016-09-07 03:32:06 +00002250template <class _Tp>
Marshall Clowd979eed2016-09-05 01:54:30 +00002251bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2252{
2253 return __first <= __p && __p < __last;
2254}
2255
Marshall Clowac655ef2016-09-07 03:32:06 +00002256template <class _Tp1, class _Tp2>
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00002257bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clowac655ef2016-09-07 03:32:06 +00002258{
2259 return false;
2260}
2261
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002262template <class _CharT, class _Traits, class _Allocator>
2263template<class _ForwardIterator>
Eric Fiselier026d38e2016-10-31 02:46:25 +00002264basic_string<_CharT, _Traits, _Allocator>&
2265basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2266 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002267{
Eric Fiselier026d38e2016-10-31 02:46:25 +00002268 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2269 "function requires a ForwardIterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002270 size_type __sz = size();
2271 size_type __cap = capacity();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002272 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002273 if (__n)
2274 {
Eric Fiselier622c7d52017-04-15 06:49:02 +00002275 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2276 _CharRef __tmp_ref = *__first;
2277 if (__ptr_in_range(_VSTD::addressof(__tmp_ref), data(), data() + size()))
Marshall Clowd979eed2016-09-05 01:54:30 +00002278 {
2279 const basic_string __temp (__first, __last, __alloc());
2280 append(__temp.data(), __temp.size());
2281 }
2282 else
2283 {
2284 if (__cap - __sz < __n)
2285 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2286 pointer __p = __get_pointer() + __sz;
2287 for (; __first != __last; ++__p, ++__first)
2288 traits_type::assign(*__p, *__first);
2289 traits_type::assign(*__p, value_type());
2290 __set_size(__sz + __n);
2291 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002292 }
2293 return *this;
2294}
2295
2296template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002297inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002298basic_string<_CharT, _Traits, _Allocator>&
2299basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2300{
2301 return append(__str.data(), __str.size());
2302}
2303
2304template <class _CharT, class _Traits, class _Allocator>
2305basic_string<_CharT, _Traits, _Allocator>&
2306basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2307{
2308 size_type __sz = __str.size();
2309 if (__pos > __sz)
2310 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002311 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002312}
2313
2314template <class _CharT, class _Traits, class _Allocator>
Marshall Clow42a87db2016-10-03 23:40:48 +00002315template <class _Tp>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002316 typename enable_if
2317 <
2318 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2319 basic_string<_CharT, _Traits, _Allocator>&
2320 >::type
2321basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002322{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002323 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002324 size_type __sz = __sv.size();
2325 if (__pos > __sz)
2326 this->__throw_out_of_range();
2327 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2328}
2329
2330template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002331basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002332basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002333{
Alp Tokerec34c482014-05-15 11:27:39 +00002334 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002335 return append(__s, traits_type::length(__s));
2336}
2337
2338// insert
2339
2340template <class _CharT, class _Traits, class _Allocator>
2341basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002342basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002343{
Alp Tokerec34c482014-05-15 11:27:39 +00002344 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002345 size_type __sz = size();
2346 if (__pos > __sz)
2347 this->__throw_out_of_range();
2348 size_type __cap = capacity();
2349 if (__cap - __sz >= __n)
2350 {
2351 if (__n)
2352 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002353 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354 size_type __n_move = __sz - __pos;
2355 if (__n_move != 0)
2356 {
2357 if (__p + __pos <= __s && __s < __p + __sz)
2358 __s += __n;
2359 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2360 }
2361 traits_type::move(__p + __pos, __s, __n);
2362 __sz += __n;
2363 __set_size(__sz);
2364 traits_type::assign(__p[__sz], value_type());
2365 }
2366 }
2367 else
2368 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2369 return *this;
2370}
2371
2372template <class _CharT, class _Traits, class _Allocator>
2373basic_string<_CharT, _Traits, _Allocator>&
2374basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2375{
2376 size_type __sz = size();
2377 if (__pos > __sz)
2378 this->__throw_out_of_range();
2379 if (__n)
2380 {
2381 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002382 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002383 if (__cap - __sz >= __n)
2384 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002385 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002386 size_type __n_move = __sz - __pos;
2387 if (__n_move != 0)
2388 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2389 }
2390 else
2391 {
2392 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002393 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002394 }
2395 traits_type::assign(__p + __pos, __n, __c);
2396 __sz += __n;
2397 __set_size(__sz);
2398 traits_type::assign(__p[__sz], value_type());
2399 }
2400 return *this;
2401}
2402
2403template <class _CharT, class _Traits, class _Allocator>
2404template<class _InputIterator>
2405typename enable_if
2406<
Marshall Clowdf9db312016-01-13 21:54:34 +00002407 __is_exactly_input_iterator<_InputIterator>::value
2408 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2409 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002410>::type
2411basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2412{
Howard Hinnant499cea12013-08-23 17:37:05 +00002413#if _LIBCPP_DEBUG_LEVEL >= 2
2414 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2415 "string::insert(iterator, range) called with an iterator not"
2416 " referring to this string");
2417#endif
Marshall Clowd979eed2016-09-05 01:54:30 +00002418 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002419 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002420}
2421
2422template <class _CharT, class _Traits, class _Allocator>
2423template<class _ForwardIterator>
2424typename enable_if
2425<
Marshall Clowdf9db312016-01-13 21:54:34 +00002426 __is_forward_iterator<_ForwardIterator>::value
2427 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002428 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2429>::type
2430basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2431{
Howard Hinnant499cea12013-08-23 17:37:05 +00002432#if _LIBCPP_DEBUG_LEVEL >= 2
2433 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2434 "string::insert(iterator, range) called with an iterator not"
2435 " referring to this string");
2436#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002437 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002438 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002439 if (__n)
2440 {
Eric Fiselier622c7d52017-04-15 06:49:02 +00002441 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2442 _CharRef __tmp_char = *__first;
2443 if (__ptr_in_range(_VSTD::addressof(__tmp_char), data(), data() + size()))
Marshall Clowd979eed2016-09-05 01:54:30 +00002444 {
2445 const basic_string __temp(__first, __last, __alloc());
2446 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2447 }
2448
2449 size_type __sz = size();
2450 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002451 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002452 if (__cap - __sz >= __n)
2453 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002454 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002455 size_type __n_move = __sz - __ip;
2456 if (__n_move != 0)
2457 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2458 }
2459 else
2460 {
2461 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002462 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002463 }
2464 __sz += __n;
2465 __set_size(__sz);
2466 traits_type::assign(__p[__sz], value_type());
2467 for (__p += __ip; __first != __last; ++__p, ++__first)
2468 traits_type::assign(*__p, *__first);
2469 }
2470 return begin() + __ip;
2471}
2472
2473template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002474inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002475basic_string<_CharT, _Traits, _Allocator>&
2476basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2477{
2478 return insert(__pos1, __str.data(), __str.size());
2479}
2480
2481template <class _CharT, class _Traits, class _Allocator>
2482basic_string<_CharT, _Traits, _Allocator>&
2483basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2484 size_type __pos2, size_type __n)
2485{
2486 size_type __str_sz = __str.size();
2487 if (__pos2 > __str_sz)
2488 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002489 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002490}
2491
2492template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002493template <class _Tp>
2494typename enable_if
2495<
2496 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
Marshall Clowf1729d92017-11-15 20:02:27 +00002497 basic_string<_CharT, _Traits, _Allocator>&
Marshall Clow6ac8de02016-09-24 22:45:42 +00002498>::type
2499basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002500 size_type __pos2, size_type __n)
2501{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002502 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002503 size_type __str_sz = __sv.size();
2504 if (__pos2 > __str_sz)
2505 this->__throw_out_of_range();
2506 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2507}
2508
2509template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002510basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002511basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002512{
Alp Tokerec34c482014-05-15 11:27:39 +00002513 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002514 return insert(__pos, __s, traits_type::length(__s));
2515}
2516
2517template <class _CharT, class _Traits, class _Allocator>
2518typename basic_string<_CharT, _Traits, _Allocator>::iterator
2519basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2520{
2521 size_type __ip = static_cast<size_type>(__pos - begin());
2522 size_type __sz = size();
2523 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002524 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002525 if (__cap == __sz)
2526 {
2527 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002528 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002529 }
2530 else
2531 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002532 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002533 size_type __n_move = __sz - __ip;
2534 if (__n_move != 0)
2535 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2536 }
2537 traits_type::assign(__p[__ip], __c);
2538 traits_type::assign(__p[++__sz], value_type());
2539 __set_size(__sz);
2540 return begin() + static_cast<difference_type>(__ip);
2541}
2542
2543template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002544inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002545typename basic_string<_CharT, _Traits, _Allocator>::iterator
2546basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2547{
Howard Hinnant499cea12013-08-23 17:37:05 +00002548#if _LIBCPP_DEBUG_LEVEL >= 2
2549 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2550 "string::insert(iterator, n, value) called with an iterator not"
2551 " referring to this string");
2552#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002553 difference_type __p = __pos - begin();
2554 insert(static_cast<size_type>(__p), __n, __c);
2555 return begin() + __p;
2556}
2557
2558// replace
2559
2560template <class _CharT, class _Traits, class _Allocator>
2561basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002562basic_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 +00002563 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002564{
Alp Tokerec34c482014-05-15 11:27:39 +00002565 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002566 size_type __sz = size();
2567 if (__pos > __sz)
2568 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002569 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002570 size_type __cap = capacity();
2571 if (__cap - __sz + __n1 >= __n2)
2572 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002573 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574 if (__n1 != __n2)
2575 {
2576 size_type __n_move = __sz - __pos - __n1;
2577 if (__n_move != 0)
2578 {
2579 if (__n1 > __n2)
2580 {
2581 traits_type::move(__p + __pos, __s, __n2);
2582 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2583 goto __finish;
2584 }
2585 if (__p + __pos < __s && __s < __p + __sz)
2586 {
2587 if (__p + __pos + __n1 <= __s)
2588 __s += __n2 - __n1;
2589 else // __p + __pos < __s < __p + __pos + __n1
2590 {
2591 traits_type::move(__p + __pos, __s, __n1);
2592 __pos += __n1;
2593 __s += __n2;
2594 __n2 -= __n1;
2595 __n1 = 0;
2596 }
2597 }
2598 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2599 }
2600 }
2601 traits_type::move(__p + __pos, __s, __n2);
2602__finish:
Eric Fiselier3b7c1342017-03-09 01:54:13 +00002603// __sz += __n2 - __n1; in this and the below function below can cause unsigned integer overflow,
2604// but this is a safe operation, so we disable the check.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002605 __sz += __n2 - __n1;
2606 __set_size(__sz);
2607 __invalidate_iterators_past(__sz);
2608 traits_type::assign(__p[__sz], value_type());
2609 }
2610 else
2611 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2612 return *this;
2613}
2614
2615template <class _CharT, class _Traits, class _Allocator>
2616basic_string<_CharT, _Traits, _Allocator>&
2617basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiselier3b7c1342017-03-09 01:54:13 +00002618 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002619{
2620 size_type __sz = size();
2621 if (__pos > __sz)
2622 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002623 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002624 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002625 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002626 if (__cap - __sz + __n1 >= __n2)
2627 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002628 __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 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2634 }
2635 }
2636 else
2637 {
2638 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002639 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002640 }
2641 traits_type::assign(__p + __pos, __n2, __c);
2642 __sz += __n2 - __n1;
2643 __set_size(__sz);
2644 __invalidate_iterators_past(__sz);
2645 traits_type::assign(__p[__sz], value_type());
2646 return *this;
2647}
2648
2649template <class _CharT, class _Traits, class _Allocator>
2650template<class _InputIterator>
2651typename enable_if
2652<
2653 __is_input_iterator<_InputIterator>::value,
2654 basic_string<_CharT, _Traits, _Allocator>&
2655>::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002656basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002657 _InputIterator __j1, _InputIterator __j2)
2658{
Marshall Clowd979eed2016-09-05 01:54:30 +00002659 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002660 return this->replace(__i1, __i2, __temp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002661}
2662
2663template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002664inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002665basic_string<_CharT, _Traits, _Allocator>&
2666basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2667{
2668 return replace(__pos1, __n1, __str.data(), __str.size());
2669}
2670
2671template <class _CharT, class _Traits, class _Allocator>
2672basic_string<_CharT, _Traits, _Allocator>&
2673basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
2674 size_type __pos2, size_type __n2)
2675{
2676 size_type __str_sz = __str.size();
2677 if (__pos2 > __str_sz)
2678 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002679 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002680}
2681
2682template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002683template <class _Tp>
2684typename enable_if
2685<
Marshall Clowf1729d92017-11-15 20:02:27 +00002686 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2687 basic_string<_CharT, _Traits, _Allocator>&
Marshall Clow6ac8de02016-09-24 22:45:42 +00002688>::type
2689basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002690 size_type __pos2, size_type __n2)
2691{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002692 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002693 size_type __str_sz = __sv.size();
2694 if (__pos2 > __str_sz)
2695 this->__throw_out_of_range();
2696 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
2697}
2698
2699template <class _CharT, class _Traits, class _Allocator>
2700basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002701basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702{
Alp Tokerec34c482014-05-15 11:27:39 +00002703 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002704 return replace(__pos, __n1, __s, traits_type::length(__s));
2705}
2706
2707template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002708inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002709basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002710basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711{
2712 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
2713 __str.data(), __str.size());
2714}
2715
2716template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002717inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002718basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002719basic_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 +00002720{
2721 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
2722}
2723
2724template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002725inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002726basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002727basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002728{
2729 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
2730}
2731
2732template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002733inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002734basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002735basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002736{
2737 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
2738}
2739
2740// erase
2741
2742template <class _CharT, class _Traits, class _Allocator>
2743basic_string<_CharT, _Traits, _Allocator>&
2744basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
2745{
2746 size_type __sz = size();
2747 if (__pos > __sz)
2748 this->__throw_out_of_range();
2749 if (__n)
2750 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002751 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002752 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002753 size_type __n_move = __sz - __pos - __n;
2754 if (__n_move != 0)
2755 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
2756 __sz -= __n;
2757 __set_size(__sz);
2758 __invalidate_iterators_past(__sz);
2759 traits_type::assign(__p[__sz], value_type());
2760 }
2761 return *this;
2762}
2763
2764template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002765inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002766typename basic_string<_CharT, _Traits, _Allocator>::iterator
2767basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
2768{
Howard Hinnant499cea12013-08-23 17:37:05 +00002769#if _LIBCPP_DEBUG_LEVEL >= 2
2770 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2771 "string::erase(iterator) called with an iterator not"
2772 " referring to this string");
2773#endif
2774 _LIBCPP_ASSERT(__pos != end(),
2775 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002776 iterator __b = begin();
2777 size_type __r = static_cast<size_type>(__pos - __b);
2778 erase(__r, 1);
Howard Hinnantec3773c2011-12-01 20:21:04 +00002779 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002780}
2781
2782template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002783inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002784typename basic_string<_CharT, _Traits, _Allocator>::iterator
2785basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
2786{
Howard Hinnant499cea12013-08-23 17:37:05 +00002787#if _LIBCPP_DEBUG_LEVEL >= 2
2788 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2789 "string::erase(iterator, iterator) called with an iterator not"
2790 " referring to this string");
2791#endif
2792 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002793 iterator __b = begin();
2794 size_type __r = static_cast<size_type>(__first - __b);
2795 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnantec3773c2011-12-01 20:21:04 +00002796 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002797}
2798
2799template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002800inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002801void
2802basic_string<_CharT, _Traits, _Allocator>::pop_back()
2803{
Howard Hinnant499cea12013-08-23 17:37:05 +00002804 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002805 size_type __sz;
2806 if (__is_long())
2807 {
2808 __sz = __get_long_size() - 1;
2809 __set_long_size(__sz);
2810 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
2811 }
2812 else
2813 {
2814 __sz = __get_short_size() - 1;
2815 __set_short_size(__sz);
2816 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
2817 }
2818 __invalidate_iterators_past(__sz);
2819}
2820
2821template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002822inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002823void
Howard Hinnanta6119a82011-05-29 19:57:12 +00002824basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002825{
2826 __invalidate_all_iterators();
2827 if (__is_long())
2828 {
2829 traits_type::assign(*__get_long_pointer(), value_type());
2830 __set_long_size(0);
2831 }
2832 else
2833 {
2834 traits_type::assign(*__get_short_pointer(), value_type());
2835 __set_short_size(0);
2836 }
2837}
2838
2839template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002840inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002841void
2842basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
2843{
2844 if (__is_long())
2845 {
2846 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
2847 __set_long_size(__pos);
2848 }
2849 else
2850 {
2851 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
2852 __set_short_size(__pos);
2853 }
2854 __invalidate_iterators_past(__pos);
2855}
2856
2857template <class _CharT, class _Traits, class _Allocator>
2858void
2859basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
2860{
2861 size_type __sz = size();
2862 if (__n > __sz)
2863 append(__n - __sz, __c);
2864 else
2865 __erase_to_end(__n);
2866}
2867
2868template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002869inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002870typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00002871basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002872{
Howard Hinnante32b5e22010-11-17 17:55:08 +00002873 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiselier5ccf0432017-10-17 13:16:01 +00002874#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow09f85502013-10-31 17:23:08 +00002875 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002876#else
Marshall Clow09f85502013-10-31 17:23:08 +00002877 return __m - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002878#endif
2879}
2880
2881template <class _CharT, class _Traits, class _Allocator>
2882void
2883basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
2884{
2885 if (__res_arg > max_size())
2886 this->__throw_length_error();
2887 size_type __cap = capacity();
2888 size_type __sz = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002889 __res_arg = _VSTD::max(__res_arg, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002890 __res_arg = __recommend(__res_arg);
2891 if (__res_arg != __cap)
2892 {
2893 pointer __new_data, __p;
2894 bool __was_long, __now_long;
2895 if (__res_arg == __min_cap - 1)
2896 {
2897 __was_long = true;
2898 __now_long = false;
2899 __new_data = __get_short_pointer();
2900 __p = __get_long_pointer();
2901 }
2902 else
2903 {
2904 if (__res_arg > __cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002905 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002906 else
2907 {
2908 #ifndef _LIBCPP_NO_EXCEPTIONS
2909 try
2910 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002911 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnante32b5e22010-11-17 17:55:08 +00002912 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002913 #ifndef _LIBCPP_NO_EXCEPTIONS
2914 }
2915 catch (...)
2916 {
2917 return;
2918 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002919 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002920 if (__new_data == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002921 return;
Howard Hinnant324bb032010-08-22 00:02:43 +00002922 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002923 }
2924 __now_long = true;
2925 __was_long = __is_long();
2926 __p = __get_pointer();
2927 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002928 traits_type::copy(_VSTD::__to_raw_pointer(__new_data),
2929 _VSTD::__to_raw_pointer(__p), size()+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002930 if (__was_long)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002931 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002932 if (__now_long)
2933 {
2934 __set_long_cap(__res_arg+1);
2935 __set_long_size(__sz);
2936 __set_long_pointer(__new_data);
2937 }
2938 else
2939 __set_short_size(__sz);
2940 __invalidate_all_iterators();
2941 }
2942}
2943
2944template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002945inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002946typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002947basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002948{
Howard Hinnant499cea12013-08-23 17:37:05 +00002949 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002950 return *(data() + __pos);
2951}
2952
2953template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002954inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002955typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002956basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002957{
Howard Hinnant499cea12013-08-23 17:37:05 +00002958 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002959 return *(__get_pointer() + __pos);
2960}
2961
2962template <class _CharT, class _Traits, class _Allocator>
2963typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2964basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
2965{
2966 if (__n >= size())
2967 this->__throw_out_of_range();
2968 return (*this)[__n];
2969}
2970
2971template <class _CharT, class _Traits, class _Allocator>
2972typename basic_string<_CharT, _Traits, _Allocator>::reference
2973basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
2974{
2975 if (__n >= size())
2976 this->__throw_out_of_range();
2977 return (*this)[__n];
2978}
2979
2980template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002981inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002982typename basic_string<_CharT, _Traits, _Allocator>::reference
2983basic_string<_CharT, _Traits, _Allocator>::front()
2984{
Howard Hinnant499cea12013-08-23 17:37:05 +00002985 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002986 return *__get_pointer();
2987}
2988
2989template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002990inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002991typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2992basic_string<_CharT, _Traits, _Allocator>::front() const
2993{
Howard Hinnant499cea12013-08-23 17:37:05 +00002994 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002995 return *data();
2996}
2997
2998template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002999inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003000typename basic_string<_CharT, _Traits, _Allocator>::reference
3001basic_string<_CharT, _Traits, _Allocator>::back()
3002{
Howard Hinnant499cea12013-08-23 17:37:05 +00003003 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003004 return *(__get_pointer() + size() - 1);
3005}
3006
3007template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003008inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003009typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3010basic_string<_CharT, _Traits, _Allocator>::back() const
3011{
Howard Hinnant499cea12013-08-23 17:37:05 +00003012 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003013 return *(data() + size() - 1);
3014}
3015
3016template <class _CharT, class _Traits, class _Allocator>
3017typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003018basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003019{
3020 size_type __sz = size();
3021 if (__pos > __sz)
3022 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003023 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003024 traits_type::copy(__s, data() + __pos, __rlen);
3025 return __rlen;
3026}
3027
3028template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003029inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003030basic_string<_CharT, _Traits, _Allocator>
3031basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3032{
3033 return basic_string(*this, __pos, __n, __alloc());
3034}
3035
3036template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003037inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003038void
3039basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00003040#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00003041 _NOEXCEPT_DEBUG
Marshall Clow7d914d12015-07-13 20:04:56 +00003042#else
Eric Fiselier47257c42016-12-28 05:53:01 +00003043 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00003044 __is_nothrow_swappable<allocator_type>::value)
3045#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003046{
Howard Hinnant499cea12013-08-23 17:37:05 +00003047#if _LIBCPP_DEBUG_LEVEL >= 2
3048 if (!__is_long())
3049 __get_db()->__invalidate_all(this);
3050 if (!__str.__is_long())
3051 __get_db()->__invalidate_all(&__str);
3052 __get_db()->swap(this, &__str);
3053#endif
Eric Fiselier47257c42016-12-28 05:53:01 +00003054 _LIBCPP_ASSERT(
3055 __alloc_traits::propagate_on_container_swap::value ||
3056 __alloc_traits::is_always_equal::value ||
3057 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnant0949eed2011-06-30 21:18:19 +00003058 _VSTD::swap(__r_.first(), __str.__r_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00003059 __swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003060}
3061
3062// find
3063
3064template <class _Traits>
3065struct _LIBCPP_HIDDEN __traits_eq
3066{
3067 typedef typename _Traits::char_type char_type;
Howard Hinnanta6119a82011-05-29 19:57:12 +00003068 _LIBCPP_INLINE_VISIBILITY
3069 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3070 {return _Traits::eq(__x, __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003071};
3072
3073template<class _CharT, class _Traits, class _Allocator>
3074typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003075basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003076 size_type __pos,
3077 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003078{
Alp Tokerec34c482014-05-15 11:27:39 +00003079 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003080 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003081 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003082}
3083
3084template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003085inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003086typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003087basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3088 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003089{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003090 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003091 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003092}
3093
3094template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003095inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003096typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003097basic_string<_CharT, _Traits, _Allocator>::find(__self_view __sv,
3098 size_type __pos) const _NOEXCEPT
3099{
3100 return __str_find<value_type, size_type, traits_type, npos>
3101 (data(), size(), __sv.data(), __pos, __sv.size());
3102}
3103
3104template<class _CharT, class _Traits, class _Allocator>
3105inline _LIBCPP_INLINE_VISIBILITY
3106typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003107basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003108 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003109{
Alp Tokerec34c482014-05-15 11:27:39 +00003110 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003111 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003112 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003113}
3114
3115template<class _CharT, class _Traits, class _Allocator>
3116typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003117basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3118 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003119{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003120 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003121 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003122}
3123
3124// rfind
3125
3126template<class _CharT, class _Traits, class _Allocator>
3127typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003128basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003129 size_type __pos,
3130 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003131{
Alp Tokerec34c482014-05-15 11:27:39 +00003132 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003133 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003134 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003135}
3136
3137template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003138inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003139typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003140basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3141 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003142{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003143 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003144 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003145}
3146
3147template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003148inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003149typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003150basic_string<_CharT, _Traits, _Allocator>::rfind(__self_view __sv,
3151 size_type __pos) const _NOEXCEPT
3152{
3153 return __str_rfind<value_type, size_type, traits_type, npos>
3154 (data(), size(), __sv.data(), __pos, __sv.size());
3155}
3156
3157template<class _CharT, class _Traits, class _Allocator>
3158inline _LIBCPP_INLINE_VISIBILITY
3159typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003160basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003161 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003162{
Alp Tokerec34c482014-05-15 11:27:39 +00003163 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003164 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003165 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003166}
3167
3168template<class _CharT, class _Traits, class _Allocator>
3169typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003170basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3171 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003172{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003173 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003174 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003175}
3176
3177// find_first_of
3178
3179template<class _CharT, class _Traits, class _Allocator>
3180typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003181basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003182 size_type __pos,
3183 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003184{
Alp Tokerec34c482014-05-15 11:27:39 +00003185 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003186 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003187 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003188}
3189
3190template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003191inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003192typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003193basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3194 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003195{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003196 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003197 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003198}
3199
3200template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003201inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003202typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003203basic_string<_CharT, _Traits, _Allocator>::find_first_of(__self_view __sv,
3204 size_type __pos) const _NOEXCEPT
3205{
3206 return __str_find_first_of<value_type, size_type, traits_type, npos>
3207 (data(), size(), __sv.data(), __pos, __sv.size());
3208}
3209
3210template<class _CharT, class _Traits, class _Allocator>
3211inline _LIBCPP_INLINE_VISIBILITY
3212typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003213basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003214 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003215{
Alp Tokerec34c482014-05-15 11:27:39 +00003216 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003217 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003218 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003219}
3220
3221template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003222inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003223typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003224basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3225 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003226{
3227 return find(__c, __pos);
3228}
3229
3230// find_last_of
3231
3232template<class _CharT, class _Traits, class _Allocator>
3233typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003234basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003235 size_type __pos,
3236 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003237{
Alp Tokerec34c482014-05-15 11:27:39 +00003238 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003239 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003240 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003241}
3242
3243template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003244inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003245typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003246basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3247 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003248{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003249 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003250 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003251}
3252
3253template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003254inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003255typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003256basic_string<_CharT, _Traits, _Allocator>::find_last_of(__self_view __sv,
3257 size_type __pos) const _NOEXCEPT
3258{
3259 return __str_find_last_of<value_type, size_type, traits_type, npos>
3260 (data(), size(), __sv.data(), __pos, __sv.size());
3261}
3262
3263template<class _CharT, class _Traits, class _Allocator>
3264inline _LIBCPP_INLINE_VISIBILITY
3265typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003266basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003267 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003268{
Alp Tokerec34c482014-05-15 11:27:39 +00003269 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003270 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003271 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003272}
3273
3274template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003275inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003276typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003277basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3278 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003279{
3280 return rfind(__c, __pos);
3281}
3282
3283// find_first_not_of
3284
3285template<class _CharT, class _Traits, class _Allocator>
3286typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003287basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003288 size_type __pos,
3289 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003290{
Alp Tokerec34c482014-05-15 11:27:39 +00003291 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003292 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003293 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003294}
3295
3296template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003297inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003298typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003299basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3300 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003301{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003302 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003303 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003304}
3305
3306template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003307inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003308typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003309basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(__self_view __sv,
3310 size_type __pos) const _NOEXCEPT
3311{
3312 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3313 (data(), size(), __sv.data(), __pos, __sv.size());
3314}
3315
3316template<class _CharT, class _Traits, class _Allocator>
3317inline _LIBCPP_INLINE_VISIBILITY
3318typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003319basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003320 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003321{
Alp Tokerec34c482014-05-15 11:27:39 +00003322 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003323 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003324 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003325}
3326
3327template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003328inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003329typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003330basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3331 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003332{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003333 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003334 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003335}
3336
3337// find_last_not_of
3338
3339template<class _CharT, class _Traits, class _Allocator>
3340typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003341basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003342 size_type __pos,
3343 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003344{
Alp Tokerec34c482014-05-15 11:27:39 +00003345 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003346 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003347 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003348}
3349
3350template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003351inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003352typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003353basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3354 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003355{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003356 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003357 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003358}
3359
3360template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003361inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003362typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003363basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(__self_view __sv,
3364 size_type __pos) const _NOEXCEPT
3365{
3366 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3367 (data(), size(), __sv.data(), __pos, __sv.size());
3368}
3369
3370template<class _CharT, class _Traits, class _Allocator>
3371inline _LIBCPP_INLINE_VISIBILITY
3372typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003373basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003374 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003375{
Alp Tokerec34c482014-05-15 11:27:39 +00003376 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003377 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003378 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003379}
3380
3381template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003382inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003383typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003384basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3385 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003386{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003387 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003388 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003389}
3390
3391// compare
3392
3393template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003394inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003395int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003396basic_string<_CharT, _Traits, _Allocator>::compare(__self_view __sv) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003397{
Howard Hinnantfa06d752011-07-24 21:45:06 +00003398 size_t __lhs_sz = size();
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003399 size_t __rhs_sz = __sv.size();
3400 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantfa06d752011-07-24 21:45:06 +00003401 _VSTD::min(__lhs_sz, __rhs_sz));
3402 if (__result != 0)
3403 return __result;
3404 if (__lhs_sz < __rhs_sz)
3405 return -1;
3406 if (__lhs_sz > __rhs_sz)
3407 return 1;
3408 return 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003409}
3410
3411template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003412inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003413int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003414basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003415{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003416 return compare(__self_view(__str));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003417}
3418
3419template <class _CharT, class _Traits, class _Allocator>
3420int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003421basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3422 size_type __n1,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003423 const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003424 size_type __n2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003425{
Alp Tokerec34c482014-05-15 11:27:39 +00003426 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003427 size_type __sz = size();
3428 if (__pos1 > __sz || __n2 == npos)
3429 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003430 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3431 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003432 if (__r == 0)
3433 {
3434 if (__rlen < __n2)
3435 __r = -1;
3436 else if (__rlen > __n2)
3437 __r = 1;
3438 }
3439 return __r;
3440}
3441
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003442template <class _CharT, class _Traits, class _Allocator>
3443inline _LIBCPP_INLINE_VISIBILITY
3444int
3445basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3446 size_type __n1,
3447 __self_view __sv) const
3448{
3449 return compare(__pos1, __n1, __sv.data(), __sv.size());
3450}
3451
3452template <class _CharT, class _Traits, class _Allocator>
3453inline _LIBCPP_INLINE_VISIBILITY
3454int
3455basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3456 size_type __n1,
3457 const basic_string& __str) const
3458{
3459 return compare(__pos1, __n1, __str.data(), __str.size());
3460}
3461
3462template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00003463template <class _Tp>
3464typename enable_if
3465<
Marshall Clowf1729d92017-11-15 20:02:27 +00003466 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3467 int
Marshall Clow6ac8de02016-09-24 22:45:42 +00003468>::type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003469basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3470 size_type __n1,
Marshall Clow6ac8de02016-09-24 22:45:42 +00003471 const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003472 size_type __pos2,
3473 size_type __n2) const
3474{
Marshall Clow6ac8de02016-09-24 22:45:42 +00003475 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003476 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3477}
3478
3479template <class _CharT, class _Traits, class _Allocator>
3480int
3481basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3482 size_type __n1,
3483 const basic_string& __str,
3484 size_type __pos2,
3485 size_type __n2) const
3486{
3487 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3488}
3489
3490template <class _CharT, class _Traits, class _Allocator>
3491int
3492basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3493{
3494 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3495 return compare(0, npos, __s, traits_type::length(__s));
3496}
3497
3498template <class _CharT, class _Traits, class _Allocator>
3499int
3500basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3501 size_type __n1,
3502 const value_type* __s) const
3503{
3504 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3505 return compare(__pos1, __n1, __s, traits_type::length(__s));
3506}
3507
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003508// __invariants
3509
3510template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003511inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003512bool
3513basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3514{
3515 if (size() > capacity())
3516 return false;
3517 if (capacity() < __min_cap - 1)
3518 return false;
3519 if (data() == 0)
3520 return false;
3521 if (data()[size()] != value_type(0))
3522 return false;
3523 return true;
3524}
3525
3526// operator==
3527
3528template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003529inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003530bool
3531operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003532 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003533{
Howard Hinnant08dd2532013-04-22 23:55:13 +00003534 size_t __lhs_sz = __lhs.size();
3535 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3536 __rhs.data(),
3537 __lhs_sz) == 0;
3538}
3539
3540template<class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003541inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant08dd2532013-04-22 23:55:13 +00003542bool
3543operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3544 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3545{
3546 size_t __lhs_sz = __lhs.size();
3547 if (__lhs_sz != __rhs.size())
3548 return false;
3549 const char* __lp = __lhs.data();
3550 const char* __rp = __rhs.data();
3551 if (__lhs.__is_long())
3552 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3553 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3554 if (*__lp != *__rp)
3555 return false;
3556 return true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003557}
3558
3559template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003560inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003561bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003562operator==(const _CharT* __lhs,
3563 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003564{
Eric Fiselier4f241822015-08-28 03:02:37 +00003565 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3566 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3567 size_t __lhs_len = _Traits::length(__lhs);
3568 if (__lhs_len != __rhs.size()) return false;
3569 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003570}
3571
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003572template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003573inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003574bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003575operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3576 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003577{
Eric Fiselier4f241822015-08-28 03:02:37 +00003578 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3579 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3580 size_t __rhs_len = _Traits::length(__rhs);
3581 if (__rhs_len != __lhs.size()) return false;
3582 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003583}
3584
Howard Hinnant324bb032010-08-22 00:02:43 +00003585template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003586inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003587bool
3588operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003589 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003590{
3591 return !(__lhs == __rhs);
3592}
3593
3594template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003595inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003596bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003597operator!=(const _CharT* __lhs,
3598 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003599{
3600 return !(__lhs == __rhs);
3601}
3602
3603template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003604inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003605bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003606operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3607 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003608{
3609 return !(__lhs == __rhs);
3610}
3611
3612// operator<
3613
3614template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003615inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003616bool
3617operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003618 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003619{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003620 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003621}
3622
3623template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003624inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003625bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003626operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3627 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003628{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003629 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003630}
3631
3632template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003633inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003634bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003635operator< (const _CharT* __lhs,
3636 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003637{
3638 return __rhs.compare(__lhs) > 0;
3639}
3640
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003641// operator>
3642
3643template<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
3646operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003647 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003648{
3649 return __rhs < __lhs;
3650}
3651
3652template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003653inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003654bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003655operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3656 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003657{
3658 return __rhs < __lhs;
3659}
3660
3661template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003662inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003663bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003664operator> (const _CharT* __lhs,
3665 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003666{
3667 return __rhs < __lhs;
3668}
3669
3670// operator<=
3671
3672template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003673inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003674bool
3675operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003676 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003677{
3678 return !(__rhs < __lhs);
3679}
3680
3681template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003682inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003683bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003684operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3685 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003686{
3687 return !(__rhs < __lhs);
3688}
3689
3690template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003691inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003692bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003693operator<=(const _CharT* __lhs,
3694 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003695{
3696 return !(__rhs < __lhs);
3697}
3698
3699// operator>=
3700
3701template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003702inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003703bool
3704operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003705 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003706{
3707 return !(__lhs < __rhs);
3708}
3709
3710template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003711inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003712bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003713operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3714 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003715{
3716 return !(__lhs < __rhs);
3717}
3718
3719template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003720inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003721bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003722operator>=(const _CharT* __lhs,
3723 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003724{
3725 return !(__lhs < __rhs);
3726}
3727
3728// operator +
3729
3730template<class _CharT, class _Traits, class _Allocator>
3731basic_string<_CharT, _Traits, _Allocator>
3732operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3733 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3734{
3735 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3736 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3737 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3738 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3739 __r.append(__rhs.data(), __rhs_sz);
3740 return __r;
3741}
3742
3743template<class _CharT, class _Traits, class _Allocator>
3744basic_string<_CharT, _Traits, _Allocator>
3745operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3746{
3747 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3748 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
3749 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3750 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
3751 __r.append(__rhs.data(), __rhs_sz);
3752 return __r;
3753}
3754
3755template<class _CharT, class _Traits, class _Allocator>
3756basic_string<_CharT, _Traits, _Allocator>
3757operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3758{
3759 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3760 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3761 __r.__init(&__lhs, 1, 1 + __rhs_sz);
3762 __r.append(__rhs.data(), __rhs_sz);
3763 return __r;
3764}
3765
3766template<class _CharT, class _Traits, class _Allocator>
3767basic_string<_CharT, _Traits, _Allocator>
3768operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3769{
3770 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3771 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3772 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
3773 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3774 __r.append(__rhs, __rhs_sz);
3775 return __r;
3776}
3777
3778template<class _CharT, class _Traits, class _Allocator>
3779basic_string<_CharT, _Traits, _Allocator>
3780operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
3781{
3782 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3783 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3784 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
3785 __r.push_back(__rhs);
3786 return __r;
3787}
3788
Eric Fiselier3e928972017-04-19 00:28:44 +00003789#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003790
3791template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003792inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003793basic_string<_CharT, _Traits, _Allocator>
3794operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3795{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003796 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003797}
3798
3799template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003800inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003801basic_string<_CharT, _Traits, _Allocator>
3802operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3803{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003804 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003805}
3806
3807template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003808inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003809basic_string<_CharT, _Traits, _Allocator>
3810operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3811{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003812 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003813}
3814
3815template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003816inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003817basic_string<_CharT, _Traits, _Allocator>
3818operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3819{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003820 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003821}
3822
3823template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003824inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003825basic_string<_CharT, _Traits, _Allocator>
3826operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3827{
3828 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003829 return _VSTD::move(__rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003830}
3831
3832template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003833inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003834basic_string<_CharT, _Traits, _Allocator>
3835operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
3836{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003837 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003838}
3839
3840template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003841inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003842basic_string<_CharT, _Traits, _Allocator>
3843operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
3844{
3845 __lhs.push_back(__rhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003846 return _VSTD::move(__lhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003847}
3848
Eric Fiselier3e928972017-04-19 00:28:44 +00003849#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003850
3851// swap
3852
3853template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003854inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003855void
Howard Hinnanta6119a82011-05-29 19:57:12 +00003856swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00003857 basic_string<_CharT, _Traits, _Allocator>& __rhs)
3858 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003859{
3860 __lhs.swap(__rhs);
3861}
3862
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003863#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
3864
3865typedef basic_string<char16_t> u16string;
3866typedef basic_string<char32_t> u32string;
3867
Howard Hinnant324bb032010-08-22 00:02:43 +00003868#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003869
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003870_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
3871_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
3872_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
3873_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
3874_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003875
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003876_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
3877_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
3878_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003879
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003880_LIBCPP_FUNC_VIS string to_string(int __val);
3881_LIBCPP_FUNC_VIS string to_string(unsigned __val);
3882_LIBCPP_FUNC_VIS string to_string(long __val);
3883_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
3884_LIBCPP_FUNC_VIS string to_string(long long __val);
3885_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
3886_LIBCPP_FUNC_VIS string to_string(float __val);
3887_LIBCPP_FUNC_VIS string to_string(double __val);
3888_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003889
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003890_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
3891_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
3892_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
3893_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
3894_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003895
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003896_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = 0);
3897_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
3898_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003899
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003900_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
3901_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
3902_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
3903_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
3904_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
3905_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
3906_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
3907_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
3908_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003909
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003910template<class _CharT, class _Traits, class _Allocator>
3911 const typename basic_string<_CharT, _Traits, _Allocator>::size_type
3912 basic_string<_CharT, _Traits, _Allocator>::npos;
3913
3914template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +00003915struct _LIBCPP_TEMPLATE_VIS hash<basic_string<_CharT, _Traits, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003916 : public unary_function<basic_string<_CharT, _Traits, _Allocator>, size_t>
3917{
3918 size_t
Howard Hinnanta6119a82011-05-29 19:57:12 +00003919 operator()(const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003920};
3921
3922template<class _CharT, class _Traits, class _Allocator>
3923size_t
3924hash<basic_string<_CharT, _Traits, _Allocator> >::operator()(
Howard Hinnanta6119a82011-05-29 19:57:12 +00003925 const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003926{
Sean Huntaffd9e52011-07-29 23:31:56 +00003927 return __do_string_hash(__val.data(), __val.data() + __val.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003928}
3929
Howard Hinnant464aa5c2011-07-18 15:51:59 +00003930template<class _CharT, class _Traits, class _Allocator>
3931basic_ostream<_CharT, _Traits>&
3932operator<<(basic_ostream<_CharT, _Traits>& __os,
3933 const basic_string<_CharT, _Traits, _Allocator>& __str);
3934
3935template<class _CharT, class _Traits, class _Allocator>
3936basic_istream<_CharT, _Traits>&
3937operator>>(basic_istream<_CharT, _Traits>& __is,
3938 basic_string<_CharT, _Traits, _Allocator>& __str);
3939
3940template<class _CharT, class _Traits, class _Allocator>
3941basic_istream<_CharT, _Traits>&
3942getline(basic_istream<_CharT, _Traits>& __is,
3943 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3944
3945template<class _CharT, class _Traits, class _Allocator>
3946inline _LIBCPP_INLINE_VISIBILITY
3947basic_istream<_CharT, _Traits>&
3948getline(basic_istream<_CharT, _Traits>& __is,
3949 basic_string<_CharT, _Traits, _Allocator>& __str);
3950
Eric Fiselier3e928972017-04-19 00:28:44 +00003951#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant464aa5c2011-07-18 15:51:59 +00003952
3953template<class _CharT, class _Traits, class _Allocator>
3954inline _LIBCPP_INLINE_VISIBILITY
3955basic_istream<_CharT, _Traits>&
3956getline(basic_istream<_CharT, _Traits>&& __is,
3957 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3958
3959template<class _CharT, class _Traits, class _Allocator>
3960inline _LIBCPP_INLINE_VISIBILITY
3961basic_istream<_CharT, _Traits>&
3962getline(basic_istream<_CharT, _Traits>&& __is,
3963 basic_string<_CharT, _Traits, _Allocator>& __str);
3964
Eric Fiselier3e928972017-04-19 00:28:44 +00003965#endif // _LIBCPP_CXX03_LANG
Howard Hinnant464aa5c2011-07-18 15:51:59 +00003966
Howard Hinnant499cea12013-08-23 17:37:05 +00003967#if _LIBCPP_DEBUG_LEVEL >= 2
3968
3969template<class _CharT, class _Traits, class _Allocator>
3970bool
3971basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
3972{
3973 return this->data() <= _VSTD::__to_raw_pointer(__i->base()) &&
3974 _VSTD::__to_raw_pointer(__i->base()) < this->data() + this->size();
3975}
3976
3977template<class _CharT, class _Traits, class _Allocator>
3978bool
3979basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
3980{
3981 return this->data() < _VSTD::__to_raw_pointer(__i->base()) &&
3982 _VSTD::__to_raw_pointer(__i->base()) <= this->data() + this->size();
3983}
3984
3985template<class _CharT, class _Traits, class _Allocator>
3986bool
3987basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
3988{
3989 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
3990 return this->data() <= __p && __p <= this->data() + this->size();
3991}
3992
3993template<class _CharT, class _Traits, class _Allocator>
3994bool
3995basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
3996{
3997 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
3998 return this->data() <= __p && __p < this->data() + this->size();
3999}
4000
4001#endif // _LIBCPP_DEBUG_LEVEL >= 2
4002
Shoaib Meenai68506702017-06-29 02:52:46 +00004003_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<char>)
4004_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<wchar_t>)
Shoaib Meenai68506702017-06-29 02:52:46 +00004005
Marshall Clow15234322013-07-23 17:05:24 +00004006#if _LIBCPP_STD_VER > 11
4007// Literal suffixes for basic_string [basic.string.literals]
Marshall Clow8d9dd7a2013-10-05 21:18:32 +00004008inline namespace literals
Marshall Clow15234322013-07-23 17:05:24 +00004009{
4010 inline namespace string_literals
4011 {
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004012 inline _LIBCPP_INLINE_VISIBILITY
4013 basic_string<char> operator "" s( const char *__str, size_t __len )
4014 {
4015 return basic_string<char> (__str, __len);
4016 }
Marshall Clow15234322013-07-23 17:05:24 +00004017
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004018 inline _LIBCPP_INLINE_VISIBILITY
4019 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4020 {
4021 return basic_string<wchar_t> (__str, __len);
4022 }
Marshall Clow15234322013-07-23 17:05:24 +00004023
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004024 inline _LIBCPP_INLINE_VISIBILITY
4025 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4026 {
4027 return basic_string<char16_t> (__str, __len);
4028 }
Marshall Clow15234322013-07-23 17:05:24 +00004029
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004030 inline _LIBCPP_INLINE_VISIBILITY
4031 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4032 {
4033 return basic_string<char32_t> (__str, __len);
4034 }
Marshall Clow15234322013-07-23 17:05:24 +00004035 }
4036}
4037#endif
4038
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004039_LIBCPP_END_NAMESPACE_STD
4040
Eric Fiselier018a3d52017-05-31 22:07:49 +00004041_LIBCPP_POP_MACROS
4042
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004043#endif // _LIBCPP_STRING